WebIndex/composables/function.ts

35 lines
1.1 KiB
TypeScript

export const getAssetURL = (image: string) => {
// 参数一: 相对路径
// 参数二: 当前路径的URL
return new URL(`../assets/images/${image}`, import.meta.url).href
}
export const debounce = (fn: Function, delay: number) => {
let timeoutId: NodeJS.Timeout
return (...args: any[]) => {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
fn(...args)
}, delay)
}
}
export const setMobileTopColor = () => {
const lightMeta = document.createElement('meta');
lightMeta.setAttribute('name', 'theme-color');
lightMeta.setAttribute('media', '(prefers-color-scheme: light)');
lightMeta.setAttribute('content', '#60a5fa');
const darkMeta = document.createElement('meta');
darkMeta.setAttribute('name', 'theme-color');
darkMeta.setAttribute('media', '(prefers-color-scheme: dark)');
darkMeta.setAttribute('content', '#60a5fa');
document.head.appendChild(lightMeta);
document.head.appendChild(darkMeta);
}
export const removeMobileTopColor = () => {
document.querySelectorAll('meta[name="theme-color"]').forEach(meta => meta.remove());
}