【问题标题】:Jquery Tools Scrollable: Make it stop at last itemJquery Tools Scrollable:让它停在最后一项
【发布时间】:2011-11-02 21:37:42
【问题描述】:

基本上,我正在尝试修复 Jquery Tools 1.2.6 Scrollable 的奇怪习惯,即如果项目数没有除以滚动大小,则滚动过去最后一个项目。

换句话说,如果我有一个包含 11 个项目的小部件,并且设置为一次显示 2 个,并且每次单击下一个按钮以滚动到下一个 2 个项目时,当涉及到第十一个项目时,它将滚动以显示第十一个,但在第十二个项目所在的右侧有一个空白区域。尽管没有第二项,但基本上将下一个 2 滚动到视图中

这里我有一个 jsFiddle 来演示我的修复:http://jsfiddle.net/natepers/6kmuE/21/

通过将scroll-size 重置为小于scroll-size 的剩余项目数,我设法让它停在最后一个项目上;

问题是我无法恢复到第一个项目。

这里是javascript:

var scrollable = $(".scrollable").data("scrollable");
var size = scrollable.getConf().size;

// Catch any requests for items at the end of the list
scrollable.onSeek(function(event, index) {

    var self_size = this.getSize();

    // Last vsisible item
    var last = index + size;

    // How many to the last item
    var difference = self_size - last;

     // How many more than the scroll size
    var remainder = index%size;

    //reset scroll size for each request
    scrollable.getConf().size = size;


    // If next request has items but less than the scroll size
    if ( remainder == 0 && difference < size && difference > 0 ) {

            // Set the scroll size to th enumber of items left
           scrollable.getConf().size = difference;
    }
    //If we're at the end
    if (index++ === self_size - size) {

            // Set disabled style on next button
             $("a.next").addClass("disabled");   
    }
    //If the items are not evenly divided by the scroll size we know we're at the end
    if (remainder != 0) {

            // Set scroll size to the what's left 
            scrollable.getConf().size = remainder;        
    }
});

// Stop scrolling at last item
scrollable.onBeforeSeek(function(event, index) {
  var self_index = this.getIndex();
  var self_size = this.getSize();
  var last = self_index + size;
    //If the last visible item is the last item
  if (last == self_size) {
      // If the next requested item is >= the last item do nothing
      if (index > self_index) { return false; }
  }
});

感谢您的任何建议或帮助。

【问题讨论】:

    标签: jquery jquery-tools scrollable


    【解决方案1】:

    我知道这是一个有点老的问题。我遇到了这种情况,上面建议的链接对我没有帮助。上面链接中描述的解决方案在显示固定数量的项目的情况下效果很好。

    但是如果显示的项目数量可以变化呢?我的任务涉及显示一堆可滚动的链接,这些链接一次滚动一个链接。链接文本的长度总是不同的。我无法知道一次会显示多少项。

    这是我想出的插件解决方案:

        $.fn.restrictScroll = function () {
    
        var api = $(this).data("scrollable");
        var container = this;
    
        Init();
    
        // Initialization method.
        function Init() {
            // Subscribe to events we are interested in.
            api.onBeforeSeek(function (event, index) {
                return isScrollable(index);
            });
            api.onSeek(function (event, index) {
                changeNavButtonState(index);
            });
            $(window).resize(function () {
                var index = api.getIndex();
                changeNavButtonState(index);
            });
    
            // set up initial state of the nav button
            var index = api.getIndex();
            changeNavButtonState(index);
        }
    
        function changeNavButtonState(index) {
            var conf = api.getConf();
            $(conf.next).toggleClass(conf.disabledClass, !isScrollable(index));
        }
    
        function isScrollable(index) {
    
            var answer = false;
            var currentIndex = api.getIndex();
    
            if (index < currentIndex) { // if navigating back, always allow scroll.
                answer = true;
            }
            else {
                var items = api.getItems();
                var itemsWidth = 0;
    
                for (var i = currentIndex; i < items.length; i++) {
                    itemsWidth += $(items[i]).width();
                }
    
                answer = itemsWidth > $(container).width();
            }
    
            return answer;
        }
    }
    

    下面是如何使用这个插件:

    $("#scrollable").scrollable().restrictScroll();
    

    示例 HTML:

        <div class="slidingTabs">
        <!-- "previous page" action -->
        <a class="prev browse left"></a>
    
        <div class="scrollable" id="scrollable">
    
            <!-- root element for the items -->
            <div class="items"">
                <div><a href="#">1 | Some small text</a></div>
                <div><a href="#">2 | Some long tab name</a></div>
                <div><a href="#">3 | Three is a crowd</a></div>
                <div><a href="#">4 | quick brown fox jumps</a></div>
                <div><a href="#">5 | Bollywood music is awesome</a></div>
                <div><a href="#">6 | Nicky Minaz is weird looking</a></div>
                <div><a href="#">7 | Tab number 7</a></div>
                <div><a href="#">8 | Tab number 8</a></div>
                <div><a href="#">9 | This one has real long name</a></div>
            </div>
        </div>
    
        <!-- "next page" action -->
        <a class="next browse right"></a>
    </div>
    

    【讨论】:

    • 像个老板!谢谢你。我必须在垂直可滚动上使用它,我所要做的就是将 width() 调用更改为 height();
    【解决方案2】:

    我最近遇到了同样的问题。下面这篇博文中的解决方案在 jQuery Tools 1.2.6 中似乎对我很有效

    http://www.jasoncarr.com/technology/jquery-tools-scrollable-stop-scrolling-past-the-end

    【讨论】:

      【解决方案3】:

      当我遇到这个问题时,我正在寻找其他东西,我意识到这有点晚了,但几个月前我遇到了类似的问题,并通过René Schubert 找到了这个答案。建议的解决方案对我来说效果很好:

      jQuery Tools Scrollable: 3 Items on screen but scroll one at a time

      干杯

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        相关资源
        最近更新 更多