Detect end of horizontal scrolling div with jQuery

前端 未结 3 1146
终归单人心
终归单人心 2020-12-16 05:15

So, I\'ve got some data tossed in a div. It\'s split up into chunks by date. It scrolls horizontally with the use of jQuery and the mousewheel plugin.

I need to fire

相关标签:
3条回答
  • 2020-12-16 05:58

    Hey, I've prepared a page for you with the implementation. You can see how to detect the end of scrolling area with jQuery.

    For the document as a whole you must detect in javascript whether .scrollTop has become equal to .scrollHeight. With jQuery it would be to detect:

    if ( $(document).scrollTop() == ( $(document).height() - $(window).height() ) {
      // Do something here ...
    }
    

    The same is done for width. Have a look on example with div here.

    0 讨论(0)
  • 2020-12-16 06:02
    $('.div-with-scrollbar').scroll(function () {
      var $elem = $('.div-with-scrollbar');
      var newScrollLeft = $elem.scrollLeft(),
          width = $elem.width(),
          scrollWidth = $elem.get(0).scrollWidth;
      var offset = 0;
      if (scrollWidth - newScrollLeft - width === offset) {
        alert('right end')
      }
      if (newScrollLeft === 0) {
        alert('left')
      }
    });
    
    0 讨论(0)
  • 2020-12-16 06:12

    Here is the code that you want. It's proved that works on IE, Safari, Chrome, Firefox, etc.

    Here is the HTML part.

        <div id="slide-wrap" style="width:1000px; height:200px; overflow:hidden; padding:0 auto;">
            <div id="inner-wrap" style="float:left;">
                <!-- 'Put Inline contains here like below.' -->
                <div class='containBox' style="width:250px; height:100%; float:left; display:inline-block;"></div>
                <!--  ...  -->
                <div class='containBox' style="width:250px; height:100%; float:left; display:inline-block;"></div>
                <!-- 'Put Inline contains here like above.' -->
            </div>
            <div style="display: block; width:40px; height:60px; position: absolute; margin-left:0px;   margin-top:40px">
                <img id='scroll_L_Arrow' src='../assets/img/l-arrow.png' onclick="scrollThumb('Go_L')"/>
            </div>
            <div style="display: block; width:40px; height:60px; position: absolute; margin-left:960px; margin-top:40px">
                <img id='scroll_R_Arrow' src='../assets/img/r-arrow.png' onclick="scrollThumb('Go_R')"/>
            </div>
        </div>
    

    Here is jQuery part in JavaScript functions.

        function scrollArrowShow() {
            var maxScroll = ($('#inner-wrap').width() - $('#slide-wrap').scrollLeft()) - $('#slide-wrap').width();
            if ( 0 == $('#slide-wrap').scrollLeft()) {
                $('#scroll_L_Arrow').css({visibility: 'hidden'});
            }else{
                $('#scroll_L_Arrow').css({visibility: 'visible'});
            }
            if ( 0 == maxScroll) {
                $('#scroll_R_Arrow').css({visibility: 'hidden'});
            }else{
                $('#scroll_R_Arrow').css({visibility: 'visible'});
            }
        }
    
           function scrollThumb(direction) {
            if (direction=='Go_L') {
                $('#slide-wrap').animate({
                    scrollLeft: "-=" + 250 + "px"
                }, function(){
                    // createCookie('scrollPos', $('#slide-wrap').scrollLeft());
                    scrollArrowShow();
                });
            }else
            if (direction=='Go_R') {
                $('#slide-wrap').animate({
                    scrollLeft: "+=" + 250 + "px"
                }, function(){
                    // createCookie('scrollPos', $('#slide-wrap').scrollLeft());
                    scrollArrowShow();
                });
            }
           }
    
    0 讨论(0)
提交回复
热议问题