在最后<李>

Stop scroll on the end of the last <li>

本文关键字:gt lt 最后      更新时间:2023-09-26

我有这把小提琴http://jsfiddle.net/kureiii/45Exw/

var cur = 1;
var max = $(".collectionsCnt2").children("li").length;
$(".button-right").click(function () {
if (cur + 1 > max) return;
cur++;
$(".collectionsCnt2").animate({
    marginLeft: "-=500px",
}, 1000);
});
(".button-left").click(function () {
if (cur - 1 < 1) return;
cur--;
$(".collectionsCnt2").animate({
    marginLeft: "+=500px",
}, 1000);
});

滚动正在工作,但我希望滚动到最后一个li或最后一个框时停止?。。

试试这个:

var cur = 1;
var max = $(".collectionsCnt2").children("li").length;
$(".button-right").click(function () {
    if (cur >= max) return;
        cur =  cur + (500/100 < max ? 500/100 : max);
        $(".collectionsCnt2").animate({
        marginLeft: "-=500px",
    }, 1000);
});
$(".button-left").click(function () {
    if (cur <= 1) return;
        cur =  cur - (500/100 < max ? 500/100 : max);
        $(".collectionsCnt2").animate({
            marginLeft: "+=500px",
        }, 1000);
    });
});

Fiddle

你可以这样做,而不是对值进行硬编码,

marginLeft: -$(".collectionsCnt2 li:last-child").offset().left + $(".collectionsCnt2")[0].scrollLeft -51 + 'px'