WebIndex/composables/function.ts

36 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-11-17 11:45:08 +08:00
export const getAssetURL = (image: string) => {
// 参数一: 相对路径
// 参数二: 当前路径的URL
2025-01-13 21:51:37 +08:00
if (import.meta.client) { return new URL(`../assets/images/${image}`, import.meta.url).href }
2024-12-01 13:09:44 +08:00
return `/_nuxt/assets/images/${image}`
2024-11-17 11:45:08 +08:00
}
export const debounce = (fn: Function, delay: number) => {
let timeoutId: NodeJS.Timeout
return (...args: any[]) => {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
fn(...args)
}, delay)
}
}
export const setMobileTopColor = () => {
2025-01-13 21:51:37 +08:00
const lightMeta = document.createElement('meta')
lightMeta.setAttribute('name', 'theme-color')
lightMeta.setAttribute('media', '(prefers-color-scheme: light)')
lightMeta.setAttribute('content', '#60a5fa')
2024-11-17 11:45:08 +08:00
2025-01-13 21:51:37 +08:00
const darkMeta = document.createElement('meta')
darkMeta.setAttribute('name', 'theme-color')
darkMeta.setAttribute('media', '(prefers-color-scheme: dark)')
darkMeta.setAttribute('content', '#60a5fa')
2024-11-17 11:45:08 +08:00
2025-01-13 21:51:37 +08:00
document.head.appendChild(lightMeta)
document.head.appendChild(darkMeta)
2024-11-17 11:45:08 +08:00
}
export const removeMobileTopColor = () => {
2025-01-13 21:51:37 +08:00
document.querySelectorAll('meta[name="theme-color"]').forEach(meta => meta.remove())
2024-11-17 11:45:08 +08:00
}