javascript - 使用 jQuery 获取调整大小时的尺寸变化
问题描述
对于我的网站,我使用以下代码:
$(window).resize(function (event) {
window.location.reload();
});
不幸的是,在移动设备上使用该网站时,会发生微小的调整大小事件。我想设置一个容差,以便这些微小的更改不会触发此重新加载。为此,我需要知道在调整大小事件中发生的尺寸变化。我一直在看这个event
物体,但它又大又丑。
提前致谢 :)
解决方案
您可以通过跟踪原始窗口尺寸,然后将这些尺寸与当前尺寸(在每次调整大小事件期间获取)进行比较以确定变化量来实现这一点。
下面显示了如果宽度改变了一定的总量,你如何触发重新加载THRESHOLD
:
var THRESHOLD = 50; // the threshold that must be exceeded to trigger reload
$(window).resize(function(e) {
var width = $(window).width();
// Record the starting window width that the comparison will
// be relative to
if(window.startWidth == undefined) {
window.startWidth = width;
}
// Calculate the total change since first resize event
var widthChange = Math.abs(width - window.startWidth);
// If change exceeds THRESHOLD, trigger reload
if(widthChange > THRESHOLD) {
window.location.reload();
}
})
推荐阅读
- ios - 相等的矩形动画不同?
- liferay - 哪些表有Liferay中所有页面标题和liferay导航菜单标题的信息
- c# - 如何让 FxCopAnalyzers 跳过 Specflow AssemblyHooks 警告
- karate - 有没有一种好方法可以将空手道用于依赖于数据库状态的测试?
- jwt - JWT 的所有权证明
- reactjs - 将状态从主页传递到导航栏和条件渲染
- python - 如何使用 pytest xfail 断言异常消息(异常类型的断言有效)
- angular - 如何将动态点击事件绑定到 *ngFor 中的动态模板 ref/var?
- reactjs - 未处理的拒绝(TypeError):无法读取未定义的属性“项目”
- c++ - C ++如何在同一行中进行cin和cout?