Comments (10)
I've implemented this behavior by adding the following code into the DetectZoom
variable:
previousBrowserZoomDetectionValue:0,
previousBrowserZoomValue:0,
zoomChangeEventListeners:[],
/**
* Initializes the event detection listener.
*/
initialize: function() {
setInterval(function () {
var zoomDetectionValue;
if (screen.deviceXDPI) {
// MSIE8, MSIE9 => detect the browser zoom change (without false positive when the window is resized)
zoomDetectionValue = screen.deviceXDPI;
} else {
// other browser => detect the browser zoom change (with false positive when the window is resized)
zoomDetectionValue = document.body.offsetWidth;
}
if (DetectZoom.previousBrowserZoomDetectionValue==0) {
// first time => initialize
DetectZoom.previousBrowserZoomDetectionValue = zoomDetectionValue;
DetectZoom.previousBrowserZoomValue = DetectZoom.zoom();
}
if (zoomDetectionValue!=DetectZoom.previousBrowserZoomDetectionValue) {
// changed width or DPI => do further checks
// Implementation note: under browsers other than MSIE8 and MSIE9, there may be a window resize or a browser zoom change, so we check it.
var currentZoomValue = DetectZoom.zoom();
if (currentZoomValue!=DetectZoom.previousBrowserZoomValue) {
// changed zoom level => call event callbacks
for (var i=0; i<DetectZoom.zoomChangeEventListeners.length; i++) {
DetectZoom.zoomChangeEventListeners[i](DetectZoom.previousBrowserZoomValue, currentZoomValue);
}
DetectZoom.previousBrowserZoomDetectionValue = zoomDetectionValue;
DetectZoom.previousBrowserZoomValue = currentZoomValue;
}
}
}, 200);
},
/**
* Add a new zoom change event listener. The callback function receive two parameters:
*
* function myCallback(previousZoomValue, newZoomValue) { ...}
*
*/
addZoomChangeEventListener : function(callback) {
DetectZoom.zoomChangeEventListeners.push(callback);
}
Then I initialized the timer function:
DetectZoom.initialize();
from detect-zoom.
I don't really understand the way you check for the change.
Why use a timer?
I do agree that saving the last zoom value, comparing it on resize to the new one and firing a 'zoom' event can be a nice addition.
Will consider it
from detect-zoom.
Since calling DetectZoom.zoom()
costs a lot, I used a low cost detection value (screen.deviceXDPI
or document.body.offsetWidth
) to determine a potential zoom change (potential because the low cost detection method has false positive). This principle also could be used for caching the DetectZoom.zoom()
result.
Regarding the timer, I made a new try with a body.onresize
which is fired when the body is resized or when the zoom level changes (tested under MSIE9, Firefox17, Chrome24). So yes, the timer could be replaced by the onresize event (I don't really remember why I used a timer; will check the full code at work).
from detect-zoom.
I get what your'e saying, but resize/zoom events doesn't happen so often, so I don't feel that the cost (besides FF maybe) of zoom() is so significant.
Do you have a test case where performance could be important for detecting zoom level?
from detect-zoom.
Resize events happen continuously while the user is dragging the window's resize control. You likely at least want to throttle/debounce those.
from detect-zoom.
Yeah, that's right.
But this is the responsibility of the implementer (as I see it).
For example, where I work we implemented a 'resizeend' event exactly for
this kind of actions where on resize a timer is set for about 100-150ms and
only if there was no other resize in this time frame the 'resizeend' event
is dispatched.
On Jan 29, 2013, at 0:35, iangilman [email protected] wrote:
Resize events happen continuously while the user is dragging the window's
resize control. You likely at least want to throttle/debounce those.
—
Reply to this email directly or view it on
GitHubhttps://github.com//issues/19#issuecomment-12809433.
from detect-zoom.
Sure… I'm just saying if the zoom() call is costly (I don't personally know if it is), and you end up calling it constantly while the user drags around the resize control on a window, then you're potentially in a situation where simply adding DetectZoom to your page causes performance issues during a drag resize.
I agree that basing the zoom change event on browser events is preferable to polling with an timer. I'm just saying you want to keep your resize event fast, because even though they are called infrequently, when they are called they can come in great bunches.
In fact, fast detection is probably preferable to throttling, since the developer might want to track the zoom changes during the resize drag.
from detect-zoom.
Well, I guess we agree :)
I think I'll just add it to the documentation for now.
Anyway, in chrome and ff there is no intelligent way to catch zoom level
and it is costly... I'm still looking, maybe in the months this code was
not maintained something interesting popped up.
On Jan 29, 2013, at 1:55, iangilman [email protected] wrote:
Sure… I'm just saying if the zoom() call is costly (I don't personally know
if it is), and you end up calling it constantly while the user drags around
the resize control on a window, then you're potentially in a situation
where simply adding DetectZoom to your page causes performance issues
during a drag resize.
I agree that basing the zoom change event on browser events is preferable
to polling with an timer. I'm just saying you want to keep your resize
event fast, because even though they are called infrequently, when they are
called they can come in great bunches.
In fact, fast detection is probably preferable to throttling, since the
developer might want to track the zoom changes during the resize drag.
—
Reply to this email directly or view it on
GitHubhttps://github.com//issues/19#issuecomment-12812753.
from detect-zoom.
Violent agreement! Always the best :-)
Maybe file bugs against those browsers for better zoom detection and zoom event facilities? Can't hurt…
from detect-zoom.
Closing this for now.
from detect-zoom.
Related Issues (20)
- Pls confirm my fix for failing detection of IE8 HOT 1
- FF 19.0 document.body errors as null. HOT 8
- detectZoom not defined HOT 6
- Invalid zoom in Chrome when touch/pen/tablet input devices is plugged into a desktop/laptop computer HOT 8
- Not working on latest Webkit Nightlies HOT 12
- Doesn't seem to detect "text only" zoom in Firefox HOT 2
- Doesn't work in Chrome 27 (which is now the current release channel version) HOT 3
- Uncaught TypeError when js included in head (Chrome 27)
- READ THIS: Detect-zoom is currently unusable for desktop HOT 10
- Typo in wiki
- detect-zoom changes zoom when you resize the browser window in Chrome HOT 1
- Incorrect value for zoom inside iframe in Chrome HOT 1
- More Accurate Device Pixel Ratio Function HOT 1
- Idea check the client width of a known Element HOT 1
- Collaboration request HOT 1
- should use screen.width/window.innerWidth for chrome HOT 2
- Firefox new version change default zoom HOT 2
- Wrong .device() on iOS 10
- Polling needed or event model exists?
- typescript version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from detect-zoom.