Catch browser's “zoom” event in JavaScript

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

    I'd like to suggest an improvement to previous solution with tracking changes to window width. Instead of keeping your own array of event listeners you can use existing javascript event system and trigger your own event upon width change, and bind event handlers to it.

    $(window).bind('myZoomEvent', function() { ... });
    
    function pollZoomFireEvent() 
    { 
    
        if ( ... width changed ... ) {
            $(window).trigger('myZoomEvent');
        }
    }
    

    Throttle/debounce can help with reducing the rate of calls of your handler.

提交回复
热议问题