Catch browser's “zoom” event in JavaScript

前端 未结 16 1600
眼角桃花
眼角桃花 2020-11-22 05:45

Is it possible to detect, using JavaScript, when the user changes the zoom in a page? I simply want to catch a \"zoom\" event and respond to it (similar to window.onresize e

16条回答
  •  再見小時候
    2020-11-22 06:01

    On iOS 10 it is possible to add an event listener to the touchmove event and to detect, if the page is zoomed with the current event.

    var prevZoomFactorX;
    var prevZoomFactorY;
    element.addEventListener("touchmove", (ev) => {
      let zoomFactorX = document.documentElement.clientWidth / window.innerWidth;
      let zoomFactorY = document.documentElement.clientHeight / window.innerHeight;
      let pageHasZoom = !(zoomFactorX === 1 && zoomFactorY === 1);
    
      if(pageHasZoom) {
        // page is zoomed
        
        if(zoomFactorX !== prevZoomFactorX || zoomFactorY !== prevZoomFactorY) {
          // page is zoomed with this event
        }
      }
      prevZoomFactorX = zoomFactorX;
      prevZoomFactorY = zoomFactorY;
    });

提交回复
热议问题