Ahhhhhhhhhhhhhdfgbv Ahhhhhhhhhhhhhdfgbv - 1 month ago 14
jQuery Question

Stop .animate() when it reaches last div

So I have a web app where each panel is 480x300 (-20px for status bar) and two navigation buttons to scroll left or right.

Now everything works great, except when you continue scrolling past the max panels displayed, it keeps going.

I was wondering if it is possible to stop the Jquery .animate() after it reaches the last panel.

http://jsfiddle.net/gS33Y/

Answer Source

Hiya another demo http://jsfiddle.net/qpHSw/ or http://jsfiddle.net/yEsDQ/ or http://jsfiddle.net/yEsDQ/show

this sample dynamically counts your li in ul and adjust the animate according.

code

   var cur = 1;
var max = $(".scroll-content ul").children("li").length;

$("nav.back").click(function(){

    if (cur+1 > max) return;
    cur++;

    $("#panel").animate({
        marginLeft: "-=500px",
    }, 1000);

});

$("nav.forward").click(function(){
    if (cur-1 < 1) return;
    cur--; 
    $("#panel").animate({
        marginLeft: "+=500px",
    }, 1000);

});​