我正在测试以下代码:
$(window).bind('mousewheel', function(e) {
alert(e.wheelDelta);
});
每次移动鼠标时,我都会“未定义”。任何人都可以帮忙解决这个问题,因为我试图捕捉鼠标滚轮并根据它移动一个div。
答案 0 :(得分:2)
我认为您需要做更多工作才能让它在所有浏览器中运行 - 或者尝试jquery-mousewheel extension
如果扩展程序仍无法正常运行,您可以尝试我之前创建的此修改版本:
/* jquery.mousewheel.min.js
* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
*
* Version: 3.0.2
*
* Requires: 1.2.2+
*/
(function(c) {
var a = [ "DOMMouseScroll", "mousewheel" ];
c.event.special.mousewheel = {
setup : function() {
if (this.addEventListener) {
for ( var d = a.length; d;) {
this.addEventListener(a[--d], b, false)
}
} else {
this.onmousewheel = b
}
},
teardown : function() {
if (this.removeEventListener) {
for ( var d = a.length; d;) {
this.removeEventListener(a[--d], b, false)
}
} else {
this.onmousewheel = null
}
}
};
c.fn.extend({
mousewheel : function(d) {
return d ? this.bind("mousewheel", d) : this.trigger("mousewheel")
},
unmousewheel : function(d) {
return this.unbind("mousewheel", d)
}
});
function b(f) {
var d = [].slice.call(arguments, 1), g = 0, e = true;
if (f.wheelDelta) {
g = f.wheelDelta / 120
}
if (f.detail) {
g = -f.detail / 3
}
// I had to move the following two lines down, probably because of jQuery update
f = c.event.fix(f || window.event);
f.type = "mousewheel";
f.pageX = f.originalEvent.pageX;
f.pageY = f.originalEvent.pageY;
d.unshift(f, g);
return c.event.handle.apply(this, d)
}
})(jQuery);
我刚刚在Firefox和Chrome中测试过它,但我不知道它是否适用于其他浏览器。用法:
$('element').mousewheel(function(e, delta) { alert(delta); });
答案 1 :(得分:2)
我最终使用了以下代码段:
jQuery(function($) {
$(document)
.bind('mousewheel', function(event, delta) {
if(delta > 0) {
// Scrolling down
} else {
// Scrolling up
}
});
});
这需要jquery鼠标滚轮扩展并适用于IE,Firefox和Chrome。
答案 2 :(得分:0)
我遇到了同样的问题,解决方案是将jquery版本从1.7.x更改为1.9.x