在jQuery中是否有一种方法可以获取鼠标滚轮事件(不是scroll
事件)?
$(document).ready(function(){
$('#foo').bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta /120 > 0) {
console.log('scrolling up !');
}
else{
console.log('scrolling down !');
}
});
});
DOMMouseScroll
事件在Firefox中使用,因此您需要同时监听 .bind('DOMMouseScroll mousewheel') {
两个事件。 - e382df99a7950919789725ceeec126e.originalEvent.wheelDelta
未定义。 - hofnarwillie同时绑定 mousewheel
和 DOMMouseScroll
对我来说效果非常好:
$(window).bind('mousewheel DOMMouseScroll', function(event){
if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
// scroll up
}
else {
// scroll down
}
});
这种方法在IE9+,Chrome 33和Firefox 27中可用。
编辑 - 2016年3月
我决定重新访问这个问题,因为已经有一段时间了。 MDN关于滚动事件的页面有一个很好的检索滚动位置的方法,它利用了requestAnimationFrame
,这比我的先前的检测方法要好得多。 我修改了他们的代码,以提供更好的兼容性,还包括滚动方向和位置:
(function() {
var supportOffset = window.pageYOffset !== undefined,
lastKnownPos = 0,
ticking = false,
scrollDir,
currYPos;
function doSomething(scrollPos, scrollDir) {
// Your code goes here...
console.log('scroll pos: ' + scrollPos + ' | scroll dir: ' + scrollDir);
}
window.addEventListener('wheel', function(e) {
currYPos = supportOffset ? window.pageYOffset : document.body.scrollTop;
scrollDir = lastKnownPos > currYPos ? 'up' : 'down';
lastKnownPos = currYPos;
if (!ticking) {
window.requestAnimationFrame(function() {
doSomething(lastKnownPos, scrollDir);
ticking = false;
});
}
ticking = true;
});
})();
该代码目前在 Chrome v50、Firefox v44、Safari v9 和 IE9+
中可用
参考资料:
截至2017年,您只需编写以下内容:
$(window).on('wheel', function(event){
// deltaY obviously records vertical scroll, deltaX and deltaZ exist too.
// this condition makes sure it's vertical scrolling that happened
if(event.originalEvent.deltaY !== 0){
if(event.originalEvent.deltaY < 0){
// wheeled up
}
else {
// wheeled down
}
}
});
适用于当前的Firefox 51,Chrome 56和IE9 +
x<0
else ifx>0
。 - ajax333221谈论"mousewheel"事件的答案是指一个已废弃的事件。标准事件名称是 "wheel"。请参阅 https://developer.mozilla.org/en-US/docs/Web/Reference/Events/wheel
这对我起作用了:)
//Firefox
$('#elem').bind('DOMMouseScroll', function(e){
if(e.originalEvent.detail > 0) {
//scroll down
console.log('Down');
}else {
//scroll up
console.log('Up');
}
//prevent page fom scrolling
return false;
});
//IE, Opera, Safari
$('#elem').bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta < 0) {
//scroll down
console.log('Down');
}else {
//scroll up
console.log('Up');
}
//prevent page fom scrolling
return false;
});
这里是一个基本解决方案。如果传递给函数的事件是 event.originalEvent
,它可以在jQuery中使用,因为jQuery将其作为jQuery事件的属性提供。或者如果在callback
函数中,我们在第一行之前添加:event = event.originalEvent;
。
此代码规范化滚轮速度/量,并对典型鼠标中的向前滚动具有正值,并且对于向后鼠标滚轮运动具有负值。
var wheel = document.getElementById('wheel');
function report(ammout) {
wheel.innerHTML = 'wheel ammout: ' + ammout;
}
function callback(event) {
var normalized;
if (event.wheelDelta) {
normalized = (event.wheelDelta % 120 - 0) == -0 ? event.wheelDelta / 120 : event.wheelDelta / 12;
} else {
var rawAmmount = event.deltaY ? event.deltaY : event.detail;
normalized = -(rawAmmount % 3 ? rawAmmount * 10 : rawAmmount / 3);
}
report(normalized);
}
var event = 'onwheel' in document ? 'wheel' : 'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll';
window.addEventListener(event, callback);
还有一个 jQuery 插件,它的代码更冗长并且提供了额外的语法糖:https://github.com/brandonaaron/jquery-mousewheel
$(document).ready(function(){
$('#whole').bind('DOMMouseScroll mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
alert("up");
}
else{
alert("down");
}
});
});
今天我遇到了这个问题,发现以下代码对我非常有效
$('#content').on('mousewheel', function(event) {
//console.log(event.deltaX, event.deltaY, event.deltaFactor);
if(event.deltaY > 0) {
console.log('scroll up');
} else {
console.log('scroll down');
}
});
使用此代码
knob.bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta < 0) {
moveKnob('down');
} else {
moveKnob('up');
}
return false;
});
$(el).on('input',...
。 - rybo111