Catch browser's “zoom” event in JavaScript

前端 未结 16 1896
眼角桃花
眼角桃花 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:08

    There is a nifty plugin built from yonran that can do the detection. Here is his previously answered question on StackOverflow. It works for most of the browsers. Application is as simple as this:

    window.onresize = function onresize() {
      var r = DetectZoom.ratios();
      zoomLevel.innerHTML =
        "Zoom level: " + r.zoom +
        (r.zoom !== r.devicePxPerCssPx
            ? "; device to CSS pixel ratio: " + r.devicePxPerCssPx
            : "");
    }
    

    Demo

提交回复
热议问题