捕获浏览器的“缩放” JavaScript 中的事件
是否可以使用 JavaScript 检测用户何时更改页面的缩放比例? 我只是想捕获“缩放”事件并响应它(类似于 window.onresize 事件)。
谢谢。
Is it possible to detect, using JavaScript, when the user changes the zoom in a page?
I simply want to catch a "zoom" event and respond to it (similar to window.onresize event).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
无法主动检测是否有变焦。 我在这里找到了一个关于如何尝试实现它的很好的条目。
您也可以使用上述帖子中的工具来完成此操作。 问题是您或多或少对页面是否缩放进行了有根据的猜测。 这在某些浏览器中比其他浏览器效果更好。
如果他们在缩放时加载您的页面,则无法判断页面是否已缩放。
There's no way to actively detect if there's a zoom. I found a good entry here on how you can attempt to implement it.
You could also do it using the tools of the above post. The problem is you're more or less making educated guesses on whether or not the page has zoomed. This will work better in some browsers than other.
There's no way to tell if the page is zoomed if they load your page while zoomed.
让我们定义 px_ratio 如下:
pxratio = 物理像素与 css px 的比率。
如果任何一个缩放页面,视口像素(px与像素不同)会减小并且应该适合屏幕,因此比率(物理像素/CSS_px)必须变大。
但在窗口调整大小中,屏幕尺寸会缩小,像素也会缩小。 因此该比率将保持不变。
缩放:触发windows.resize事件--> 并更改 px_ratio
但
调整大小:触发 windows.resize 事件 --> 不改变 px_ratio
关键点是 CSS PX 和物理像素之间的区别。
https://gist.github.com/abilogos/66aba96bb0fb27ab3ed4a13245817d1e
Lets define px_ratio as below:
px ratio = ratio of physical pixel to css px.
if any one zoom The Page, the viewport pxes (px is different from pixel ) reduces and should be fit to The screen so the ratio (physical pixel / CSS_px ) must get bigger.
but in window Resizing, screen size reduces as well as pxes. so the ratio will maintain.
zooming: trigger windows.resize event --> and change px_ratio
but
resizing: trigger windows.resize event --> doesn’t change px_ratio
The key point is difference between CSS PX and Physical Pixel.
https://gist.github.com/abilogos/66aba96bb0fb27ab3ed4a13245817d1e
大家一些人的好消息! 当缩放更改时,较新的浏览器将触发窗口调整大小事件。Good news
everyonesome people! Newer browsers will trigger a window resize event when the zoom is changed.我正在使用这段 JavaScript 来对 Zoom“事件”做出反应。
它轮询窗口宽度。
(正如本页(Ian Elliott 链接到的)上的建议:http://novemberborn.net/ javascript/page-zoom-ff3 [存档])
使用 Chrome、Firefox 3.6 和 Opera(而非 IE)进行测试。
问候,马格努斯
I'm using this piece of JavaScript to react to Zoom "events".
It polls the window width.
(As somewhat suggested on this page (which Ian Elliott linked to): http://novemberborn.net/javascript/page-zoom-ff3 [archive])
Tested with Chrome, Firefox 3.6 and Opera, not IE.
Regards, Magnus
这对我有用:
仅在 IE8 上需要它。 所有其他浏览器自然会生成调整大小事件。
This works for me:
It's only needed on IE8. All the other browsers naturally generate a resize event.
有一个由 yonran 构建的漂亮插件可以进行检测。 以下是他之前在 StackOverflow 上回答的问题。 它适用于大多数浏览器。 应用程序就像这样简单:
演示
There is a nifty plugin built from
yonran
that can do the detection. Here is his previously answered question on StackOverflow. It works for most of the browsers. Application is as simple as this:Demo
虽然这是一个 9 年前的问题,但问题仍然存在!
我一直在检测调整大小,同时排除项目中的缩放,因此我编辑了代码,使其能够检测彼此互斥的调整大小和缩放。 它在大多数情况下都有效,所以如果大多数对于您的项目来说足够好,那么这应该会有所帮助! 在我迄今为止的测试中,它能够 100% 检测到缩放。 唯一的问题是,如果用户变得疯狂(即突然调整窗口大小)或窗口滞后,它可能会以缩放而不是窗口调整大小的方式触发。
它的工作原理是在窗口调整大小时检测
window.outerWidth
或window.outerHeight
的变化,同时检测window.innerWidth
或的变化window.innerHeight
与缩放时窗口大小调整无关。注意:我的解决方案类似于 KajMagnus 的解决方案,但这对我来说效果更好。
Although this is a 9 yr old question, the problem persists!
I have been detecting resize while excluding zoom in a project, so I edited my code to make it work to detect both resize and zoom exclusive from one another. It works most of the time, so if most is good enough for your project, then this should be helpful! It detects zooming 100% of the time in what I've tested so far. The only issue is that if the user gets crazy (ie. spastically resizing the window) or the window lags it may fire as a zoom instead of a window resize.
It works by detecting a change in
window.outerWidth
orwindow.outerHeight
as window resizing while detecting a change inwindow.innerWidth
orwindow.innerHeight
independent from window resizing as a zoom.Note: My solution is like KajMagnus's, but this has worked better for me.
根据 MDN,“matchMedia”是执行此操作的正确方法 https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#Monitoring_screen_resolution_or_zoom_level_changes
这有点挑剔,因为每个实例一次只能观看一个 MQ,所以如果您对任何缩放级别更改感兴趣,您需要制作一堆匹配器。但是由于浏览器负责发出事件,因此它可能仍然比轮询更高效,并且您可以限制或反跳回调或将其固定到动画帧或其他东西 - 这是一个看起来非常快速的实现,如果您已经依赖它,请随意交换 _throttle 或其他内容。
运行代码片段并在浏览器中放大和缩小,记下标记中的更新值 - 我只在 Firefox 中测试了这一点! 如果您发现任何问题,请告诉我。
According to MDN, "matchMedia" is the proper way to do this https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio#Monitoring_screen_resolution_or_zoom_level_changes
it's a bit finicky because each instance can only watch one MQ at a time, so if you're interested in any zoom level change you need to make a bunch of matchers.. but since the browser is in charge to emitting the events it's probably still more performant than polling, and you could throttle or debounce the callback or pin it to an animation frame or something - here's an implementation that seems pretty snappy, feel free to swap in _throttle or whatever if you're already depending on that.
Run the code snippet and zoom in and out in your browser, note the updated value in the markup - I only tested this in Firefox! lemme know if you see any issues.
⬤ 调整大小事件通过附加在现代浏览器上工作
window
上的事件,然后读取body
或其他元素的值,例如 (.getBoundingClientRect())。⚠️ 请调整选项卡大小或缩放以触发此片段:
⬤ 另一种现代替代方案:ResizeObserver API
根据您的布局,您可以观察特定元素的大小调整。
这在“响应式”布局上效果很好,因为缩放时容器框的大小会调整。
⬤ The resize event works on modern browsers by attaching the event on
window
, and then reading values of thebody
, or other element with for example (.getBoundingClientRect()).⚠️ Do resize your tab, or zoom, to trigger this snippet:
⬤ An other modern alternative: the ResizeObserver API
Depending your layout, you can watch for resizing on a particular element.
This works well on «responsive» layouts, because the container box get resized when zooming.
???? Be careful to not overload javascript tasks from user gestures events. Use requestAnimationFrame whenever you needs redraws.
这是一个干净的解决方案:
Here is a clean solution:
我想建议对以前的解决方案进行改进,跟踪窗口宽度的变化。 您可以使用现有的 javascript 事件系统并在宽度更改时触发您自己的事件,并将事件处理程序绑定到它,而不是保留自己的事件侦听器数组。
Throttle/debounce 可以帮助降低处理程序的调用率。
I'd like to suggest an improvement to previous solution with tracking changes to window width. Instead of keeping your own array of event listeners you can use existing javascript event system and trigger your own event upon width change, and bind event handlers to it.
Throttle/debounce can help with reducing the rate of calls of your handler.
您还可以通过注入至少包含不可破坏空间(可能是隐藏空间)的 div 并定期检查其高度来获取文本调整大小事件和缩放系数。 如果高度改变,文本大小也改变了,(并且你知道改变了多少 - 顺便说一句,如果窗口在全页模式下缩放,这也会触发,并且你仍然会得到正确的缩放系数,具有相同的高度/高度比)。
You can also get the text resize events, and the zoom factor by injecting a div containing at least a non-breakable space (possibly, hidden), and regularly checking its height. If the height changes, the text size has changed, (and you know how much - this also fires, incidentally, if the window gets zoomed in full-page mode, and you still will get the correct zoom factor, with the same height / height ratio).
在 iOS 10 上,可以向
touchmove
事件添加事件侦听器,并检测页面是否随当前事件缩放。On iOS 10 it is possible to add an event listener to the
touchmove
event and to detect, if the page is zoomed with the current event.这是一种本机方式(主要框架无法在 Chrome 中缩放,因为它们不支持被动事件行为)
Here is a native way (major frameworks cannot zoom in Chrome, because they dont supports passive event behaviour)
我正在回复一个 3 年前的链接,但我想这是一个更容易接受的答案,
创建 .css 文件为,
EG:-
当屏幕缩放到大约 125% 时,上面的代码使字体大小为“10px”。 您可以通过更改“1000px”的值来检查不同的缩放级别。
I'am replying to a 3 year old link but I guess here's a more acceptable answer,
Create .css file as,
EG:-
The above code makes the size of the font '10px' when the screen is zoomed to approximately 125%. You can check for different zoom level by changing the value of '1000px'.