class deviceZoom {
/* 获取操作系统类型 */
getSystemType() {
const agent = navigator.userAgent.toLowerCase();
const isMac = /macintosh|mac os x/i.test(navigator.userAgent);
if (isMac) return false;
// 目前只针对 win 处理,其它系统暂无该情况,需要则继续在此添加即可
if (agent.indexOf("windows") >= 0) return true;
}
/* 监听方法 */
addHandler(e, type, handler) {
if (e.addEventListener) {
element.addEventListener(type, handler, false);
} else if (e.attachEvent) {
e.attachEvent("on" + type, handler);
} else {
e["on" + type] = handler;
}
}
/* 校正浏览器缩放比例 */
correct() {
if (window.devicePixelRatio >= 1) {
document.getElementsByTagName('body')[0].style.zoom = 1 / window.devicePixelRatio;
} else {
document.getElementsByTagName('body')[0].style.zoom = 1;
}
}
/* 监听页面缩放resize */
resizeFunc () {
const that = this;
// 注意: 这个方法是解决全局有两个window.resize
that.addHandler(window, "resize", function () {
that.correct(); // 重新校正浏览器缩放比例
});
}
/* 初始化页面比例 */
init() {
const that = this;
// 判断设备,只在 win 系统下校正浏览器缩放比例
if (that.getSystemType()) {
that.correct(); // 校正浏览器缩放比例
that.resizeFunc ); // 页面缩放缩放
}
}
}
export default deviceZoom ;