pc zoom缩放js实现代码

1.vue中可使用

window.onload = function() {
        document.body.style.zoom = "normal"; //避免zoom尺寸叠加
        let scale = document.body.clientWidth / 1904;
        document.body.style.zoom = scale;
      };
      //防抖,避免resize占用过多资源
      (function() {
        var throttle = function(type, name, obj) {
          obj = obj || window;
          var running = false;
          var func = function() {
            if (running) {
              return;
            }
            running = true;
            requestAnimationFrame(function() {
              obj.dispatchEvent(new CustomEvent(name));
              running = false;
            });
          };
          obj.addEventListener(type, func);
        };

        throttle("resize", "optimizedResize");
      })();

      window.addEventListener("optimizedResize", function() {
        document.body.style.zoom = "normal";
        let scale = document.body.clientWidth / 1904;
        document.body.style.zoom = scale;
  });

2.jquery中可用

$(window).resize(function() {
  $("body").css("zoom", $(window).width() / 1440);
});
$("body").css("zoom", $(window).width() / 1440);

你可能感兴趣的:(zoom缩放实现代码)