为 div 滚动 [英] onscroll for div
问题描述
div 元素是否没有 onscroll
事件处理程序?我页面上的行为似乎并未表明 div onscroll
事件处理程序被识别.
Does a div element not have an onscroll
event handler?
The behaviour on my page doesn't seem to indicate the div onscroll
event handler is recognized.
<div id='bd' onscroll='alert("Scroll Called");'></div>
还有,
根据 DOM 事件冒泡,div 滚动事件是否会汇总到窗口滚动事件?
Also,
Do div scroll events roll up to window scroll events, as per DOM event bubbling ?
推荐答案
根据您使用的 HTML 版本,您可以改用 onwheel
事件.
Depending on which version of HTML you're using, you could use the onwheel
event, instead.
onscroll
事件仅在以下所有条件都为真时起作用:
The onscroll
event works only if all the following are true:
- div有
overflow:auto
、overflow:scroll
、overflow-y:scroll
等 - 该 div 当前有一个可以滚动的可见滚动条.
- 鼠标移动实际上会导致滚动条滚动.
所以 onscroll
事件并不真正适合检测一般的鼠标滚轮移动.
So the onscroll
event is not really suited for detecting general mouse wheel movement.
请注意 onwheel
事件是 HTML 5 中的新事件.根据对 w3schools 来说,它得到了相当广泛的支持.
Please note that the onwheel
event is new in HTML 5. According to w3schools, it is pretty widely supported, though.
这篇关于为 div 滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!