How to detect page zoom level in all modern browsers?

后端 未结 28 1842
慢半拍i
慢半拍i 2020-11-21 05:27
  1. How can I detect the page zoom level in all modern browsers? While this thread tells how to do it in IE7 and IE8, I can\'t find a good cross-browser solution.

28条回答
  •  爱一瞬间的悲伤
    2020-11-21 05:51

    This has worked great for me in webkit-based browsers (Chrome, Safari):

    function isZoomed() {
        var width, mediaQuery;
    
        width = document.body.clientWidth;
        mediaQuery = '(max-width: ' + width + 'px) and (min-width: ' + width + 'px)';
    
        return !window.matchMedia(mediaQuery).matches;
    }
    

    Doesn't seem to work in Firefox though.

    This also works in WebKit:

    var zoomLevel = document.width / document.body.clientWidth;
    

提交回复
热议问题