Skip to content

Instantly share code, notes, and snippets.

@kevingessner
Created May 17, 2013 13:09
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kevingessner/5598913 to your computer and use it in GitHub Desktop.
Save kevingessner/5598913 to your computer and use it in GitHub Desktop.
Detect browser zoom in javascript 'zoom' event is triggered on window on every browser zoom change. No polling!
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<iframe id="frame" style="width: 250px; border: solid 1px red;" ></iframe>
<script type="text/javascript">
var elFrame = $('#frame')[0];
$(elFrame.contentWindow).resize(function() {
$(window).trigger('zoom');
});
$(window).on('zoom', function() {
console.log('zoom', window.devicePixelRatio);
});
</script>
</body>
</html>
@glaucovillasboas
Copy link

@kirilldoid because when we zomm in or zoom out, the window doesn't resize, but all other elements do. When we zoom in, all elements in window get bigger, but the window is still in the same place with the same size.

@iHateRa1n
Copy link

i spend all of this time working on zoom bullshit, and NOW i find this.

WHY

@amit777
Copy link

amit777 commented Feb 19, 2019

Hi, neat solution. This method detects when you use the keyboard "command + shift + plus" or with the menu bar. However, if I pinch zoom, for some reason the zoom event doesn't trigger.. any tips on how to catch this case as well?

@R2D221
Copy link

R2D221 commented Jul 17, 2020

@amit777 For pinch zoom you need the Visual Viewport API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment