【发布时间】:2014-12-05 22:11:53
【问题描述】:
我尝试了几种方法来准确检测mousewheel / DOMMouseScroll 事件,但似乎结果会因浏览器而异,尤其是硬件不同。 (例如:MacBook Magic Trackpad 触发许多鼠标滚轮事件等)
JS 库已经多次尝试“规范化”mousewheel 事件的wheelDelta。但他们中的许多人都失败了(我不再找到相关的 SO 问题,但有些人指出了这个失败)。
这就是为什么我现在尝试没有mousewheel 事件,而是onscroll 事件的解决方案。 Here is an example 使用滚动的隐藏容器(#scroller)和带有正常内容的普通容器(#fixed_container)进行滚动/鼠标滚轮检测。
由于#scroller 的高度有限(此处为 4000 像素),我无法检测到滚动/鼠标滚轮
无限……
如何允许无限滚动事件(通过为#scroller设置无限高度?如何?)?
代码/Live demo:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body { overflow:hidden; }
#scroller { height: 4000px; }
#fixed_container { position:fixed; top:0px; left:0px; }
#text { position:absolute; top:100px; left:100px; }
</style>
<script type="text/javascript">
window.onscroll = function(e) {
console.log("scroll event detected! " + window.pageXOffset + " " + window.pageYOffset);
e.preventDefault();
return false;
}
</script>
</head>
<body>
<div id="scroller"></div>
<div id="fixed_container">
<div id="text">
Bonjour
</div>
</div>
</body>
</html>
【问题讨论】:
-
@Bhojendra-C-LinkNepal 当然,我已经尝试过
DOMMouseScroll,但正如我的问题中提到的,wheelDelta的结果因浏览器而异很多另一个浏览器,从硬件到另一个硬件......这就是为什么我想尝试别的东西
标签: javascript mouseevent mousewheel