数字常用计算操作
数字常用计算操作
downloadFile:base64数据导出文件,文件下载
function downloadFile(filename, data) {
let DownloadLink = document.createElement('a');
if (DownloadLink) {
document.body.appendChild(DownloadLink);
DownloadLink.style = 'display: none';
DownloadLink.download = filename;
DownloadLink.href = data;
if (document.createEvent) {
let DownloadEvt = document.createEvent('MouseEvents');
DownloadEvt.initEvent('click', true, false);
DownloadLink.dispatchEvent(DownloadEvt);
} else if (document.createEventObject) {
DownloadLink.fireEvent('onclick');
} else if (typeof DownloadLink.onclick == 'function') {
DownloadLink.onclick();
}
document.body.removeChild(DownloadLink);
}
}
基于atob和btoa的base64编码和解码
function utf8_to_b64( str ) {
return window.btoa(unescape(encodeURIComponent( str )));
}
function b64_to_utf8( str ) {
return decodeURIComponent(escape(window.atob( str )));
}
utf8_to_b64('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
浏览器相关
禁止选择和复制
['contextmenu', 'selectstart', 'copy'].forEach(function(ev){
document.addEventListener(ev, function(ev){
ev.preventDefault();
ev.returnValue = false;
})
});
body {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;
}
toFullScreen:全屏
function toFullScreen() {
let elem = document.body;
elem.webkitRequestFullScreen
? elem.webkitRequestFullScreen()
: elem.mozRequestFullScreen
? elem.mozRequestFullScreen()
: elem.msRequestFullscreen
? elem.msRequestFullscreen()
: elem.requestFullScreen
? elem.requestFullScreen()
: alert("浏览器不支持全屏");
}
exitFullscreen:退出全屏
function exitFullscreen() {
let elem = parent.document;
elem.webkitCancelFullScreen
? elem.webkitCancelFullScreen()
: elem.mozCancelFullScreen
? elem.mozCancelFullScreen()
: elem.cancelFullScreen
? elem.cancelFullScreen()
: elem.msExitFullscreen
? elem.msExitFullscreen()
: elem.exitFullscreen
? elem.exitFullscreen()
: alert("切换失败,可尝试Esc退出");
}
复制到剪贴板
https://www.zhangxinxu.com/wordpress/2021/10/js-copy-paste-clipboard/
https://gitee.com/zhangxinxu/copy-text
const copyToClipboard = (text) =>
navigator.clipboard?.writeText && navigator.clipboard.writeText(text)。
// 测试
copyToClipboard("Hello World!")。
// 只能赋值在setup中使用
import { message } from 'ant-design-vue'
import { useI18n } from '@/hooks'
export default function useCopyText() {
const { $t } = useI18n()
return (text: string) => {
try {
const data = text
const newInput = document.createElement('textarea')
newInput.value = data
document.body.appendChild(newInput)
newInput.select()
document.execCommand('Copy')
newInput.remove()
message.success($t('message.copySuccess'))
} catch (e) {
console.log(e)
message.success($t('message.copyFailed'))
}
}
}
检测黑暗模式
const isDarkMode = () =>
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches。
// 测试
console.log(isDarkMode())。
元素滚动到顶部 / 底部
const scrollToTop = (element) =>
element.scrollIntoView({ behavior: "smooth", block: "start" })
const scrollToBottom = (element) =>
element.scrollIntoView({ behavior: "smooth", block: "end" })
生成随机颜色
const generateRandomHexColor = () =>
\`#${Math.floor(Math.random() \* 0xffffff) .toString(16)}\`;