捕捉浏览器的“缩放"JavaScript 中的事件 [英] Catch browser's "zoom" event in JavaScript

查看:37
本文介绍了捕捉浏览器的“缩放"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).

谢谢.

推荐答案

没有办法主动检测是否有缩放.我在这里找到了一个很好的条目,介绍了如何尝试实施它.

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.

我找到了两种检测缩放级别.一种检测缩放的方法水平变化依赖于这样一个事实百分比值不会缩放.一个百分比值是相对于视口宽度,因此不受页面缩放.如果插入两个元素,一个以百分比表示的位置,和一个在相同位置的像素,当页面被放大.找出两者之间的比率两个元素的位置和你有缩放级别.看测试案件.http://web.archive.org/web/20080723161031/http://novemberborn.net/javascript/page-zoom-ff3

I’ve found two ways of detecting the zoom level. One way to detect zoom level changes relies on the fact that percentage values are not zoomed. A percentage value is relative to the viewport width, and thus unaffected by page zoom. If you insert two elements, one with a position in percentages, and one with the same position in pixels, they’ll move apart when the page is zoomed. Find the ratio between the positions of both elements and you’ve got the zoom level. See test case. http://web.archive.org/web/20080723161031/http://novemberborn.net/javascript/page-zoom-ff3

您也可以使用上述帖子中的工具来完成.问题是您或多或少地对页面是否缩放进行了有根据的猜测.这在某些浏览器中比其他浏览器效果更好.

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.

这篇关于捕捉浏览器的“缩放"JavaScript 中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆