本文主要是介绍换取页面缩放比例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
换取页面缩放比例
这里获取的是页面的比例,因为我们写前端的时候一般会有最合适的比例比如80%,但是客户使用的时候会出现各种不一样的比例,从而影响观看,所以获取用户的比例,是非常重要的一部分
函数代码
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;
};
调用代码及小判断
var ratio = detectZoom ()// 打印当前缩放值
console.log(ratio)// 判断是否缩放if(ratio > 100){console.log("放大啦")
}else if(ratio < 100){console.log("缩小了")
}else{console.log("100%")
}
最后我们根据放大还是缩小弹框提示就好了
转载于:https://www.cnblogs.com/guoliping/p/11112481.html
这篇关于换取页面缩放比例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!