imitate browser zoom with JavaScript

前端 未结 4 1280
慢半拍i
慢半拍i 2020-12-05 02:55

How is it possible to zoom out an entire document with JavaScript ?

My goal is to imitate the built-in browser zoom and zoom the entire document to 90%.

I ha

4条回答
  •  醉话见心
    2020-12-05 03:57

    I did this with jquery, works with Firefox, Safari, Chrome and IE9+:

    window.onload = function() {
      var currFFZoom = 1;
      var currIEZoom = 100;
    
      $('#In').on('click', function() {
        if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6) { //Firefox
          var step = 0.02;
          currFFZoom += step;
          $('body').css('MozTransform', 'scale(' + currFFZoom + ')');
        } else {
          var step = 2;
          currIEZoom += step;
          $('body').css('zoom', ' ' + currIEZoom + '%');
        }
      });
    
      $('#Out').on('click', function() {
        if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6) { //Firefox
          var step = 0.02;
          currFFZoom -= step;
          $('body').css('MozTransform', 'scale(' + currFFZoom + ')');
    
        } else {
          var step = 2;
          currIEZoom -= step;
          $('body').css('zoom', ' ' + currIEZoom + '%');
        }
      });
    };
    
    
    
    

提交回复
热议问题