Disable scroll down with jQuery

Is there a way to only disable scroll down in jQuery?

Thank you in advance

+3


source to share


2 answers


$('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

+5


source


Following Alex's answer, I managed to do it like this:



$(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;
    }
});

      

+1


source







All Articles