0

基本的に、アイテムの数がスクロールサイズで均等に分割されていない場合に、最後のアイテムを超えてスクロールするというScrollableの奇妙な習慣を修正しようとしています。

つまり、11個のアイテムを含むウィジェットがあり、一度に2個表示するように設定されていて、次のボタンをクリックして次の2個のアイテムにスクロールするたびに、11個目のアイテムになるとスクロールして11番目のディスプレイですが、右側に12番目のアイテムがある場所に空きスペースがあります。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; }
  }
});

提案や助けをありがとう。

4

3 に答える 3

3

少し古い質問だと思います。私はこの状況に遭遇し、上記のリンクは私の場合には役に立ちませんでした. 上記のリンクで説明されている解決策は、表示されるアイテムの数が固定されている場合にうまく機能します。

しかし、表示されるアイテムの数が異なる場合はどうなるでしょうか? 私の仕事は、一度に 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>
于 2012-10-05T20:48:39.417 に答える
0

私は最近、これと同じ問題を抱えていました。以下のこのブログ投稿のソリューションは、jQuery Tools 1.2.6 でうまく機能するようです。

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

于 2011-11-10T10:55:18.947 に答える