-
-
Save heytulsiprasad/990f58d24ab6755856beb0c8c22c5901 to your computer and use it in GitHub Desktop.
Differenciate between resizing for zoom or just window resizing
This file contains hidden or 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
| //for zoom detection | |
| px_ratio = window.devicePixelRatio || window.screen.availWidth / document.documentElement.clientWidth; | |
| function isZooming(){ | |
| var newPx_ratio = window.devicePixelRatio || window.screen.availWidth / document.documentElement.clientWidth; | |
| if(newPx_ratio != px_ratio){ | |
| px_ratio = newPx_ratio; | |
| console.log("zooming"); | |
| return true; | |
| }else{ | |
| console.log("just resizing"); | |
| return false; | |
| } | |
| } | |
| window.addEventListener("resize", isZooming); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is attempting to detect whether the user is zooming in or out in the browser. It relies on the
window.devicePixelRatioproperty and the comparison of the current pixel ratio with the initial pixel ratio stored in thepx_ratiovariable. Additionally, it uses theresizeevent to trigger the detection when the window is resized.Here's a detailed explanation of the code:
Initial Pixel Ratio Calculation:
px_ratio) of the device. It first checks if thewindow.devicePixelRatioproperty is available. If not, it calculates the pixel ratio using the screen width (window.screen.availWidth) divided by the document's client width (document.documentElement.clientWidth).Zoom Detection Function (
isZooming):resizeevent occurs.newPx_ratio) using the same method as the initialization.px_ratio).px_ratiowith the new value, and returnstrue.false.Event Listener for Resize:
resizeevent on thewindow.isZoomingfunction is called.In summary, the code attempts to determine whether the user is zooming by comparing the pixel ratio before and after a resize event. If the pixel ratios are different, it assumes the user is zooming. Keep in mind that the accuracy of this approach may vary across browsers and scenarios, and it might not cover all edge cases.