简体   繁体   English

jQuery,jscrollpane - 防止 window 在到达 div 末尾时滚动

[英]jQuery , jscrollpane - preventing the window from scrolling when reaching end of div

I'm trying to prevent the browser window to scroll if I've reached the top or bottom of my jscrollpane div.如果我到达我的 jscrollpane div 的顶部或底部,我试图阻止浏览器 window 滚动。 Tinyscrollbar does that. Tinyscrollbar 就是这样做的。

with jscrollpane I can detect the div scrolling with something like that:使用jscrollpane我可以检测到 div 滚动,如下所示:

var jsp = $(".scroll-pane").data('jsp'); // getting hold of the api
jsp = $("#" + e.attr('id') + " > .feed-items").bind(
    'jsp-scroll-y', function(e, scrollPositionY, isAtTop, isAtBottom) {
        // reached bottom
        if (jsp.getPercentScrolledY() == 1) {
        } else {
            // ...
        }
    })

I tried using e.preventDefault() and it didn't work.我尝试使用e.preventDefault()并没有用。 I tried everything in my magic box, from returning false to stopping propagation.我尝试了魔法盒中的所有内容,从返回 false 到停止传播。 Nothing works.没有任何效果。 There was this nice hack:有一个不错的 hack:

$("body").css("overflow", "hidden")
         .css('margin-left', "-" + __scrollbarWidth + "px");

But I don't want to use this as it's not perfect.但我不想使用它,因为它并不完美。

Any ideas?有任何想法吗?

  • You must add for example class test to the div例如,您必须将 class测试添加到 div
  • add some javascript:添加一些 javascript:

     $(document).on('mousewheel', '.test', function (e) { var delta = e.originalEvent.wheelDelta; this.scrollTop += (delta < 0? 1: -1) * 30; e.preventDefault(); });

its for vertical scroll.它用于垂直滚动。 if you want to do same for horizontal scroll let me know如果你想对水平滚动做同样的事情,请告诉我

I don't know why its working but when you reach bottom or top on mouse scroll wheel its stop scrolling;)我不知道它为什么起作用,但是当您到达鼠标滚轮的底部或顶部时,它会停止滚动;)

/* Stop scrolling */
$('#cnct_form').bind('jsp-scroll-y',function(event, isScrollable)   {
      console.log('asd');
}).jScrollPane();

$('#cnct_form').mousewheel(function(event) {
    event.preventDefault();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM