Jquery工具可滚动:让它停在最后一项

时间:2011-11-02 21:37:42

标签: jquery jquery-tools scrollable

基本上我正在尝试修复Jquery工具1.2.6如果项目数量不均匀地除以滚动大小,Scrollable的滚动过去最后一项的奇怪习惯。

换句话说,如果我有一个包含11个项目的小部件,并且它被设置为一次显示2个,并且每次单击下一个按钮以滚动到接下来的2个项目,当涉及到第11个项目时,它将滚动,所以第十一显示,但在右边有一个空的空间,第十二项将在那里一个。尽管没有第二项

,基本上将下一个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; }
  }
});

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

3 个答案:

答案 0 :(得分:3)

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

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

以下是我提出的插件解决方案:

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

答案 1 :(得分:0)

我最近遇到了同样的问题。以下博客文章中的解决方案在jQuery Tools 1.2.6

中似乎对我有用

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

答案 2 :(得分:0)

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

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

干杯