Skip to content

Instantly share code, notes, and snippets.

@kevingessner
Created May 17, 2013 13:09
Show Gist options
  • Select an option

  • Save kevingessner/5598913 to your computer and use it in GitHub Desktop.

Select an option

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>
@arfaRed

arfaRed commented Oct 1, 2013

Copy link
Copy Markdown

Hi,

The code doesn't work in IE 8 ?
Found http://htmldoodads.appspot.com/dimensions.html code.
But this is also not working.
I think the resize event is not triggering in IE8.
Any idea ?

Thanks,
Arf`a

@jakubfiala

Copy link
Copy Markdown

This is amazing. Thanks a lot!!

@pilavdzic

Copy link
Copy Markdown

awesome thank you, saved me hours of aggravation

@futurist

Copy link
Copy Markdown

It's not work in android chrome 55

@aliok

aliok commented Aug 2, 2017

Copy link
Copy Markdown

Thanks a lot for this method.

While this works perfectly, to use in production we have to hide that frame.

I used this one:

<iframe id="frame" style="width: 1px; height: 1px; visibility: hidden; position: fixed; top:10000px; left:10000px;"></iframe>

Firefox (and perhaps Safari too) doesn't trigger the event if display:none;, so cannot use that.

@kirilloid

Copy link
Copy Markdown

@aliok, there are multiple ways to hide an iframe: putting it into 1x1 parent with overflow: hidden, for example.
BUT I don't understand why do we need an iframe, when normal window works as well—just need to add something like distinctUntilChanged from RxJS. Just store current zoom level instead of creating an iframe.

@glaucovillasboas

Copy link
Copy Markdown

@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
Copy Markdown

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

WHY

@amit777

amit777 commented Feb 19, 2019

Copy link
Copy Markdown

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

R2D221 commented Jul 17, 2020

Copy link
Copy Markdown

@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