组乱序
const shuffleArray = (arr) => arr.sort(() => Math.random() - 0.5)
// 测试
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(shuffleArray(arr))
js复制到剪贴板
const copyToClipboard = (text) =>
navigator.clipboard?.writeText && navigator.clipboard.writeText(text)
// 测试
copyToClipboard("Hello World!")
数组去重
const getUnique = (arr) => [...new Set(arr)]
// 测试
const arr = [1, 1, 2, 3, 3, 4, 4, 5, 5];
console.log(getUnique(arr))
检查黑暗模式
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)}`
js关闭浏览器窗口
export const closeWindow = () => {
var userAgent = navigator.userAgent
if (userAgent.indexOf('Firefox') !== -1 || userAgent.indexOf('Chrome') !== -1) {
window.location.replace('about:blank')
} else {
window.opener = null
window.open('', '_self')
}
window.close()
}