观察带有溢出集的元素上滚动的最佳方法是什么? [英] What is the best way to observe the scroll over an element with overflow set?
问题描述
观察文档滚动事件很简单,但我似乎找不到与设置了css属性溢出的任何其他元素的滚动相关的东西?
It's simple to observe the documents scroll event but I can't seem to find anything relating to the scroll of any other element with css property overflow being set?
为了自己解决这个问题,我一直在观察mousemove,它只有在您按住滚动条时才会触发,但这需要与鼠标滚轮配合使用才能观察到效果。
To get around this myself i've been observing mousemove which only fires while you hold down on the scrollbar but this would need to be teamed with a mouse wheel observe also to get the full effect.
有人可以指出正确的方向,以实现更好的实现吗?
Could anyone point me in the right direction for a better way of implementing this?
推荐答案
您可以使用 onscroll
事件,该事件将在每次滚动元素,使用鼠标滚轮,上/下键或拖动滚动条时触发:
You can use the onscroll
event, which will fire whenever the element is scrolled, with the mouse wheel, page up/down keys or by dragging the scrollbar:
element.onscroll = function ()
{
alert(this.scrollTop);
}
AFAIK,它受IE5.5 +,Firefox,Chrome,Safari的支持和Opera,但您可以在 quirksmode.org的测试页上正确测试其中的任何一个。
AFAIK, it is supported by IE5.5+, Firefox, Chrome, Safari and Opera but you can test any of them properly at quirksmode.org's test page.
这篇关于观察带有溢出集的元素上滚动的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!