是否有一个函数可以在默认事件发生后执行一些代码?例如,我想在默认鼠标滚轮发生后获取 scrollTop 值。jquery 或 javascript 可以为我做这个吗?
4 回答
There is a broad range of event handlers in jQuery. You can use .scroll()
to respond to scroll event. Full list of events [handlers].
$(window).scroll(function() {
alert('scrolled!');
});
A scroll event is sent whenever the element's scroll position changes, regardless of the cause. A mouse click or drag on the scroll bar, dragging inside the element, pressing the arrow keys, or using the mouse's scroll wheel could cause this event.
$(document).on('mousewheel DOMMouseScroll', function() {
var top = $(this).scrollTop();
console.log(top);
});
鼠标滚轮处理可能有点痛苦。您可能应该尝试使用 jQuery Mousewheel 插件。
Brandon Aaron 的这个在过去对我很有效。
我正在使用 Brandon Aaron 的 jquery.mousewheel.js 来附加这样的事件:
$('#mydiv').mousewheel(function(event, delta, deltaX, deltaY) { alert(deltaY); });
你可以从https://github.com/brandonaaron/jquery-mousewheel/downloads下载它