Jquery Tools Scrollable:使其停止在最后一项

发布于 2024-12-13 06:51:35 字数 2128 浏览 0 评论 0原文

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

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

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

我设法通过将 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; }
  }
});

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

Basically I'm trying to fix the Jquery Tools 1.2.6 Scrollable's strange habit of scrolling past the last item if the number of items is not evenly divided by the scroll size.

In other words if I have a widget that contains 11 items and it is set to display 2 at a time and each time the next button is clicked to scroll to the next 2 items, when it comes to the eleventh item it will scroll so the eleventh displays but with an empty space to the right where the twelfth item would be were there one. Essentially scrolling the next 2 into the view despite the absence of the second item

Here I have a jsFiddle that demonstrates my fix: http://jsfiddle.net/natepers/6kmuE/21/

I managed to get it to stop at the last item by resetting the scroll-size to the remaining number of items less than the scroll-size;

Problem is I can't get it back to the first item.

Here's the 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; }
  }
});

Thanks for any suggestions or help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆追雨的时光 2024-12-20 06:51:35

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

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

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

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

I know it's a bit old question. I came across this situation and the link suggested above did not help in my case. The solution described in the link above works well in case of fixed number of items displayed.

But what if the number of items displayed can vary? My task involved showing bunch of scrollable links that would scroll one link at a time. Invariably, the text of links would vary in length. I had no ways of knowing how many items will be displayed at one point.

Here is the plug-in solution I came up with:

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

And here is how to use this plug-in:

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

Sample 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>
花开半夏魅人心 2024-12-20 06:51:35

我最近也遇到了同样的问题。下面这篇博客文章中的解决方案似乎对我来说在 jQuery Tools 1.2.6

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

I recently had this same problem. The solution in this blog post below seems to work well for me in jQuery Tools 1.2.6

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

半世蒼涼 2024-12-20 06:51:35

当我遇到这个问题时,我正在寻找其他东西,我意识到这有点晚了,但几个月前我遇到了类似的问题,并通过 雷内·舒伯特。建议的解决方案对我来说非常有效:

jQuery 工具可滚动:屏幕上有 3 个项目,但一次滚动一个

干杯

I was looking for something else when I came upon this question, and I realize this is a bit late, but I had a similar problem a few months ago, and found this answer by René Schubert. The proposed solution worked very well for me:

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

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文