<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> |
This is amazing. Thanks a lot!!
awesome thank you, saved me hours of aggravation
It's not work in android chrome 55
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.
@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.
@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.
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