var ratio = 0; var screen = window.screen; var ua = navigator.userAgent.toLowerCase(); var flag = true; function getRatio() { if (window.devicePixelRatio !== undefined) { ratio = window.devicePixelRatio; } else if (~ua.indexOf("msie")) { if (screen.deviceXDPI && screen.logicalXDPI) { //IE ratio = screen.deviceXDPI / screen.logicalXDPI; } } else if ( window.outerWidth !== undefined && window.innerWidth !== undefined ) { ratio = window.outerWidth / window.innerWidth; } if (ratio == 1) { $(".ratio-suggest").remove(); } else if (ratio > 1) { appendDom("放大", detectZoom()); } else { appendDom("缩小", detectZoom()); } } getRatio(); function detectZoom() { var 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; } function appendDom(txt, num) { if (flag) { $(".ratio-suggest").remove(); $("body").append(`
您的浏览器目前处于${txt}状态,页面可能会出现错位现象,建议100%大小显示。当前页面缩放比例${num}%
不再显示
`); } } window.onresize = function () { getRatio(); }; $(".ratio-suggest").on("click", ".close", function () { $(".ratio-suggest").remove(); flag = false; });