jquery鼠标滚轮

use*_*300 10 jquery mousewheel

我想用mousewheel jquery插件滚动div的内容.我有这个,但它不起作用.有什么想法吗?

$(function() {
$('#contentBox').bind('mousewheel', function(event, delta) {
   if (delta > 0) {
        $('#contentBox').css('top', parseInt($('#contentBox').css('top'))+40);
    } else {
        $('#contentBox').css('top', parseInt($('#contentBox').css('top'))-40);
    }
  return false;
}); 
}); 
Run Code Online (Sandbox Code Playgroud)

ruv*_*van 15

$('#contentBox').bind('mousewheel DOMMouseScroll', function(event) {
  event.preventDefault();
  var delta = event.wheelDelta || -event.detail;
  $(this).css({'top': $(this).position().top + (delta > 0 ? '+=40' : '-=40')});
}); 
Run Code Online (Sandbox Code Playgroud)

http://www.adomas.org/javascript-mouse-wheel/

  • 为什么在我的浏览器中,Ubuntu上的Firefox和Chrome都需要使用它来使其工作:(`var delta = e.wheelDelta || -e.detail || e.originalEvent.wheelDelta || -e.originalEvent.detail` (3认同)

Bob*_*ack 7

只是一个猜测:确实在CSS值中添加+'px'修复了什么?实际上,"媒体"指的是什么?

UPDATE

好的,我有机会测试你的代码并且它看起来都很好,假设你已经正确设置了CSS.你有没有为top#contentBox 分配一个值?没有现有价值,parseInt($('#contentBox').css('top'))将返回NaN.这是我使用的代码:

$(function() {
    $('#contentBox').bind('mousewheel', function(event, delta) {

        $('#contentBox').css('top', parseInt($('#contentBox').css('top')) + (delta > 0 ? 40 : -40));

        return false;
    });
});

#contentBox { height: 4em; background-color: #eee; border: 1px solid #ccc; position: absolute; top: 100px; width: 200px; }

<div id="contentBox"></div>
Run Code Online (Sandbox Code Playgroud)

请注意,我已经使用三元运算符来简化/减少代码,但这只是为了将此答案的大小保持一点,并且完全是可选的.我还在那里使用了一些测试CSS来看看我在做什么; 我相信你的不一样!