jquery animate图像停止

时间:2013-10-06 19:23:37

标签: jquery animation

如何让这个动画停在最后一张图像并让它站在最后一张图像上? 也许你可以帮我阻止这个动画。

var myImages = [
    "http://placekitten.com/200/200",
    "http://placekitten.com/150/150",
    "http://placekitten.com/180/180",
    "http://placekitten.com/170/170",
    "http://placekitten.com/140/150",
    "http://placekitten.com/160/160"
];
var counter = 1;  // Start at number 2 since the HTML tag has the first

function switchImage() {
    $('#myImage').attr('src', myImages[counter]);
    counter += 1;

    if (counter == myImages.length) {
        counter = 0;
    }
}

$(document).ready(function() {
    setInterval(switchImage, 5000);
});

1 个答案:

答案 0 :(得分:0)

setInterval()会返回一个区间ID,您可以将其传递给clearInterval()

var refreshIntervalId;
var myImages = [
    "http://placekitten.com/200/200",
    "http://placekitten.com/150/150",
    "http://placekitten.com/180/180",
    "http://placekitten.com/170/170",
    "http://placekitten.com/140/150",
    "http://placekitten.com/160/160"
];
var counter = 1;  // Start at number 2 since the HTML tag has the first

function switchImage() {
    $('#myImage').attr('src', myImages[counter]);
    counter += 1;

    if (counter == myImages.length) {
        counter = 0;
        clearInterval(refreshIntervalId);
    }
}

$(document).ready(function() {
   refreshIntervalId = setInterval(switchImage, 5000);
});

DEMO FIDDLE