javascript - 在 JavaScript 中捕获浏览器的 "zoom"事件

标签 javascript events zooming

是否可以使用 JavaScript 检测用户何时更改页面的缩放? 我只是想捕捉一个“缩放”事件并响应它(类似于 window.onresize 事件)。

谢谢。

最佳答案

没有办法主动检测是否有缩放。我在这里找到了一个关于如何尝试实现它的好条目。

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

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

如果他们在缩放时加载您的页面,则无法判断页面是否已缩放。

关于javascript - 在 JavaScript 中捕获浏览器的 "zoom"事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/995914/

相关文章:

java - OAuth2 Spring boot 缺少必需的“用户名

ffmpeg - 放大ffmpeg时如何选择位置

javascript - 在对象数组中查找具有更大键的元素

javascript - 如何使用 ES6 类运行 NodeJS?

xslt - 在 xslt concat 函数中转义单引号

javascript - 动态创建的元素上的事件绑定(bind)?

javascript - Cordova 应用程序中的触发事件

azure - 用于管理数百万订阅者(即客户)的 Webhook 架构

javascript - php session 数组不使用ajax添加新元素

d3.js - D3 中的图表放大