| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- export default defineNuxtPlugin((nuxtApp) => {
- if (import.meta.client) {
- // 存储上一次的稳定宽度,避免重复计算
- let lastStableWidth = 0
- // 核心:获取稳定的窗口宽度(解决临时值问题)
- const getStableWidth = () => {
- // 读取布局宽度(比 innerWidth 更精准,不受滚动条/缩放影响)
- return document.documentElement.clientWidth
- }
- const setZoom = () => {
- const html = document.documentElement
- const width = getStableWidth() || window.innerWidth
- if (width === lastStableWidth && width !== 0) return
- lastStableWidth = width
- let zoom = 1
- if (width >= 768 && width < 1440) {
- zoom = width / 1920
- }
- html.style.zoom = zoom.toString()
- html.classList.add('zoom-ready')
- }
- document.documentElement.classList.remove('zoom-ready')
- setZoom()
- nuxtApp.hook('app:mounted', () => {
- requestAnimationFrame(setZoom)
- })
- window.addEventListener('resize', setZoom)
- return {
- provide: {
- refreshZoom: setZoom,
- },
- }
- }
- return {
- provide: {
- refreshZoom: () => {},
- },
- }
- })
|