Created
May 17, 2013 13:09
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
@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.
i spend all of this time working on zoom bullshit, and NOW i find this.
WHY
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?
@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
@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.