How to detect page zoom level in all modern browsers?

后端 未结 28 1763
慢半拍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:58

    This may or may not help anyone, but I had a page I could not get to center correctly no matter what Css tricks I tried so I wrote a JQuery file call Center Page:

    The problem occurred with zoom level of the browser, the page would shift based upon if you were 100%, 125%, 150%, etc.

    The code below is in a JQuery file called centerpage.js.

    From my page I had to link to JQuery and this file to get it work, even though my master page already had a link to JQuery.

    <title>Home Page.</title>
    <script src="Scripts/jquery-1.7.1.min.js"></script>
    <script src="Scripts/centerpage.js"></script>
    

    centerpage.js:

    // centering page element
    function centerPage() {
        // get body element
        var body = document.body;
    
        // if the body element exists
        if (body != null) {
            // get the clientWidth
            var clientWidth = body.clientWidth;
    
            // request data for centering
            var windowWidth = document.documentElement.clientWidth;
            var left = (windowWidth - bodyWidth) / 2;
    
            // this is a hack, but it works for me a better method is to determine the 
            // scale but for now it works for my needs
            if (left > 84) {
                // the zoom level is most likely around 150 or higher
                $('#MainBody').removeClass('body').addClass('body150');
            } else if (left < 100) {
                // the zoom level is most likely around 110 - 140
                $('#MainBody').removeClass('body').addClass('body125');
            }
        }
    }
    
    
    // CONTROLLING EVENTS IN jQuery
    $(document).ready(function() {
        // center the page
        centerPage();
    });
    

    Also if you want to center a panel:

    // centering panel
    function centerPanel($panelControl) {
        // if the panel control exists
        if ($panelControl && $panelControl.length) {
            // request data for centering
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;
            var panelHeight = $panelControl.height();
            var panelWidth = $panelControl.width();
    
            // centering
            $panelControl.css({
                'position': 'absolute',
                'top': (windowHeight - panelHeight) / 2,
                'left': (windowWidth - panelWidth) / 2
            });
    
            // only need force for IE6
            $('#backgroundPanel').css('height', windowHeight);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:59

    You can try

    var browserZoomLevel = Math.round(window.devicePixelRatio * 100);
    

    This will give you browser zoom percentage level on non-retina displays. For high DPI/retina displays, it would yield different values (e.g., 200 for Chrome and Safari, 140 for Firefox).

    To catch zoom event you can use

    $(window).resize(function() { 
    // your code 
    });
    
    0 讨论(0)
  • 2020-11-21 06:00

    Your calculations are still based on a number of CSS pixels. They're just a different size on the screen now. That's the point of full page zoom.

    What would you want to happen on a browser on a 192dpi device which therefore normally displayed four device pixels for each pixel in an image? At 50% zoom this device now displays one image pixel in one device pixel.

    0 讨论(0)
  • 2020-11-21 06:02

    On mobile devices (with Chrome for Android or Opera Mobile) you can detect zoom by window.visualViewport.scale. https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API

    Detect on Safari: document.documentElement.clientWidth / window.innerWidth (return 1 if no zooming on device).

    0 讨论(0)
  • 2020-11-21 06:02

    This is for Chrome, in the wake of user800583 answer ...

    I spent a few hours on this problem and have not found a better approach, but :

    • There are 16 'zoomLevel' and not 10
    • When Chrome is fullscreen/maximized the ratio is window.outerWidth/window.innerWidth, and when it is not, the ratio seems to be (window.outerWidth-16)/window.innerWidth, however the 1st case can be approached by the 2nd one.

    So I came to the following ...

    But this approach has limitations : for example if you play the accordion with the application window (rapidly enlarge and reduce the width of the window) then you will get gaps between zoom levels although the zoom has not changed (may be outerWidth and innerWidth are not exactly updated in the same time).

    var snap = function (r, snaps)
    {
        var i;
        for (i=0; i < 16; i++) { if ( r < snaps[i] ) return i; }
    };
    var w, l, r;
    w = window.outerWidth, l = window.innerWidth;
    return snap((w - 16) / l,
                [ 0.29, 0.42, 0.58, 0.71, 0.83, 0.95, 1.05, 1.18, 1.38, 1.63, 1.88, 2.25, 2.75, 3.5, 4.5, 100 ],
    );
    

    And if you want the factor :

    var snap = function (r, snaps, ratios)
    {
        var i;
        for (i=0; i < 16; i++) { if ( r < snaps[i] ) return eval(ratios[i]); }
    };
    var w, l, r;
    w = window.outerWidth, l = window.innerWidth;
    return snap((w - 16) / l,
                [ 0.29, 0.42, 0.58, 0.71, 0.83, 0.95, 1.05, 1.18, 1.38, 1.63, 1.88, 2.25, 2.75, 3.5, 4.5, 100 ],
                [ 0.25, '1/3', 0.5, '2/3', 0.75, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5 ]
    );
    
    0 讨论(0)
  • 2020-11-21 06:03

    Basically, we have:

    • window.devicePixelRatio which takes into account both browser-level zoom* as well as system zoom/pixel density.
      * — on Mac/Safari zoom level is not taken into account
    • media queries
    • vw/vh CSS units
    • resize event, which is triggered upon zoom level change, cause effective size of the window changes

    that should be enough for normal UX. If you need to detect zoom level that might be a sign of bad UI design.

    Pitch-zoom is harder to track and is not considered currently.

    0 讨论(0)
提交回复
热议问题