禁用使用jQuery向下滚动

时间:2014-09-03 08:39:39

标签: javascript jquery scroll

有没有办法只在jQuery中禁用向下滚动?

非常感谢您提前

2 个答案:

答案 0 :(得分:5)

$('html, body').bind('DOMMouseScroll mousewheel MozMousePixelScroll', function(e) {
    var scrollTo = 0;

  if (e.type == 'mousewheel') {
      scrollTo = (e.originalEvent.wheelDelta * -1);
  }
  else if (e.type == 'DOMMouseScroll') {
      // scrollTo = 20 * e.originalEvent.detail; // turns out, this sometimes works better as expected...
      scrollTo = e.originalEvent.detail;
  }

  if (scrollTo > 0) {
    e.preventDefault();
    return false;
  }
});

<强> Here you have a working fiddle

答案 1 :(得分:1)

在Alex的回答之后,我设法以下一个方式做到了:

$(window).on('DOMMouseScroll mousewheel MozMousePixelScroll', function(e) {
    var direction = 0;
    if (e.type == 'mousewheel') {
        direction = (e.originalEvent.wheelDelta * -1);
    } else if (e.type == 'DOMMouseScroll') {
        direction = e.originalEvent.detail;
    }
    var divHeight = $("#myDiv").height();
    var scrollTop = $(this).scrollTop();
    if (direction > 0 && scrollTop > divHeight - window.innerHeight) {
        e.preventDefault();
        return false;
    }
});