我正在写一个轮播,但我无法理解为什么变量会不断拉动 DOM 的原始值,尽管 DOM 发生了变化:
// Slide container selector
var carouselSlides = $('#carousel_1 #carousel_slides');
// Selects last slide in the DOM
var cloneSlide = $('#carousel_slides .carousel_slide').last().clone();
// Prepend last slide to slide container
cloneSlide.prependTo(carouselSlides);
function changeSlideNext() {
carouselSlides.animate({
left: '-=980'
}, 1000, 'easeOutExpo', function() {
// Remove prepended slide
cloneSlide.remove();
// Move first slide to the last
$('#carousel_slides .carousel_slide').first().appendTo(carouselSlides);
carouselSlides.css('left','-980px');
// Add new prepended slide when animation is complete (and DOM is updated)
cloneSlide.add().prependTo(carouselSlides);
});
}
上面的代码总是在原始 DOM 中添加最后一张幻灯片。DOM 更新后如何选择 .last() 幻灯片?