util文件
export const detectZoom = () => {
let ratio = 0,
screen = window.screen,
ua = navigator.userAgent.toLowerCase();
if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
} else if (~ua.indexOf('msie')) {
if (screen.deviceXDPI && screen.logicalXDPI) {
ratio = screen.deviceXDPI / screen.logicalXDPI;
}
} else if (
window.outerWidth !== undefined &&
window.innerWidth !== undefined
) {
ratio = window.outerWidth / window.innerWidth;
}
if (ratio) {
ratio = Math.round(ratio * 100);
}
return ratio;
};
在main.js里面引入挂载
import { detectZoom } from '@/utils/detectZoom.js';
Vue.prototype.detectZoom = detectZoom
在App.vue
methods() {
getZoomSize() {
let m = this.detectZoom()
document.body.style.zoom = 100 / Number(m);
},
debounce(fn, delay) {
const delays = delay || 500;
let timer;
return function () {
const th = this;
const args = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function () {
timer = null;
fn.apply(th, args);
}, delays);
};
},
}
mounted() {
this.getZoomSize()
window.addEventListener("resize", this.debounce(this.getZoomSize));
},