【问题标题】:Detect end of horizontal scrolling div with jQuery使用 jQuery 检测水平滚动 div 的结尾
【发布时间】:2011-05-11 20:45:27
【问题描述】:

所以,我有一些数据被扔进了一个 div。它按日期分成几块。它使用 jQuery 和鼠标滚轮插件水平滚动。

当 div 到达它的终点(最左边,最右边)时,我需要触发一个事件。我认为通过检测鼠标滚轮插件中获取的数据,可以通过当前实现的方式来计算您何时无法进一步滚动。我只需要朝着正确的方向轻推。这是为我执行水平滚动的代码:

$(document).ready(function () {        
    $('#timeline').mousedown(function (event) {
        $(this)
            .data('down', true)
            .data('x', event.clientX)
            .data('scrollLeft', this.scrollLeft);
        return false;
    }).mouseup(function (event) {
        $(this).data('down', false);
    }).mousemove(function (event) {
        if ($(this).data('down') == true) {
            this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
        }
    }).mousewheel(function (event, delta) {
        this.scrollLeft -= (delta * 30);
    }).css({
        'overflow' : 'hidden',
        'cursor' : '-moz-grab'
    });
});

谁能给我一些指导?谢谢!

【问题讨论】:

  • 为什么要花哨的滚动?如果您只是删除垂直滚动并设置overflow-x:scroll,它将水平滚动而不会被破解。我猜有什么东西阻碍了你这样做?
  • 我正在使用一个插件,它可以在没有滚动条的情况下提供水平滚动效果。这就像看着一扇窗户,只看到它前面经过的东西。

标签: jquery scroll mousewheel


【解决方案1】:

您好,我已经为您准备了一个关于实现的页面。您可以了解如何使用 jQuery 检测滚动区域的结束。

对于整个文档,您必须在 javascript 中检测.scrollTop 是否等于.scrollHeight。使用 jQuery 可以检测到:

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

宽度也是如此。看看div here的例子。

【讨论】:

  • 感谢您的快速回复。与此同时,我实现了一个似乎可以满足我需要的插件。但是,通过这个解决方案,我能够消除对插件的需求。
  • 有没有办法限制事件触发?我希望它在您第一次滚动到它时触发,并且再也不会触发。想法?
  • 没关系 - 我只是在触发事件时向对象添加一个类。然后我检查它是否有那个类来决定事件是否应该再次触发。这听起来像是一个好的解决方案吗?
  • 另外,这对 div 的最左端很有效,但右边呢?
  • 右侧部分使用相同的变量。这些是您可以使用的唯一与滚动相关的变量。类的解决方案是可以的,虽然你可以使用.data 来存储变量并检查它,这种方式将对消费者隐藏:)
【解决方案2】:

这是您想要的代码。 已证明可在 IE、Safari、Chrome、Firefox 等上运行。

这是 HTML 部分。

    <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>

这是 JavaScript 函数中的 jQuery 部分。

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

【讨论】:

    【解决方案3】:
    $('.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')
      }
    });
    

    【讨论】:

    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2014-02-08
    • 1970-01-01
    相关资源
    最近更新 更多