Force page zoom at 100% with JS

前端 未结 7 907
旧巷少年郎
旧巷少年郎 2020-11-30 00:51

I created a little game in Canvas, but I have a problem. Some users who have the default zoom set to something other than 100% can\'t see the entire game page.

I ha

相关标签:
7条回答
  • 2020-11-30 00:55

    The only way I found that works natively is in designing my HTML/CSS with the units "vw" and "vh" (% relative to the viewport) instead of "px". You can use it everywhere you used to put "px" (font-size, width, height, padding, margin, etc...). Very useful for a page designed to be display full screen only (no scroll) or "Kiosk-style". "vw" and "vh" are not affected by browser zoom. See: https://www.w3schools.com/cssref/css_units.asp

    0 讨论(0)
  • 2020-11-30 00:57

    i'd try both solution but the following is seems to be a bug in echarts which lead to cursor deviated.

    document.body.style.zoom = 1.25; // work but not to be expected.
    

    i wonder if there any solution for browser directly modefy zoom ratio just like what ctrl++/- effect.

    0 讨论(0)
  • 2020-11-30 01:01

    You can reset the code with this:

    $("input, textarea").focusout(function(){
        $('meta[name=viewport]').remove();
        $('head').append('<meta name="viewport" content="width=device-width, maximum-scale=1.0, user-scalable=0">');
    
        $('meta[name=viewport]').remove();
        $('head').append('<meta name="viewport" content="width=device-width, initial-scale=yes">' );
    });
    
    0 讨论(0)
  • 2020-11-30 01:03

    It is working in chrome 66 :

    document.body.style.zoom = (window.innerWidth / window.outerWidth)
    
    0 讨论(0)
  • 2020-11-30 01:06

    For mobile browsers, @Linden's answer worked the best for me on Chrome. However on mobile FF it needed some additional tweaks, I came to version that works in both browsers:

    let restore = $('meta[name=viewport]')[0];
    if (restore) {
        restore = restore.outerHTML;
    }
    $('meta[name=viewport]').remove();
    $('head').append('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">');
    if (restore) {
        setTimeout(() => {
            $('meta[name=viewport]').remove();
            $('head').append(restore);
        }, 100); // On Firefox it needs a delay > 0 to work
    }
    

    Also, the restored page viewport tag must have explicit maximum-scale to allow zooming on Firefox after resetting, so I set it initially to this:

    <meta name="viewport" content="width=device-width, maximum-scale=10">
    

    Tested on mobile Chrome 76.0 and mobile Firefox 68.1.

    0 讨论(0)
  • 2020-11-30 01:11

    You can set zoom property on page load

    document.body.style.zoom = 1.0
    

    But, zoom is not a standard property for all browsers, I recommend using transform instead.

    var scale = 'scale(1)';
    document.body.style.webkitTransform =  scale;    // Chrome, Opera, Safari
     document.body.style.msTransform =   scale;       // IE 9
     document.body.style.transform = scale;     // General
    

    http://jsfiddle.net/5RzJ8/

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