我有一个jquery图像滑块,设置为将图像向左移动-100% 当图像离开时:-100%;有没有办法让它回到周期的开始?
<div id="homeslider" class="firefly_slider_wrapper">
<div class="firefly_slider">
<ul id="ffslider">
<li class="slide">
<div class="slideplacment">
<img src="" name="0" />
</div>
</li>
<li class="slide">
<div class="slideplacment">
<img src="" name="1" />
</div>
</li>
</ul>
</div>
</div>
脚本如下所示:
jQuery(window).load(function(){
var tl2 = new TimelineMax({onComplete: upDatePosition});
var imgArray = [];
var imgLength = 0;
var photoContWidth = 0;
var imgWidth = 0;
n = jQuery("#ffslider li").length;
function setDefaults(){
imgLength = jQuery('#ffslider li').length;
photoContWidth = (imgLength * 100) + '%';
for(var i=0; i<imgLength; i++){
jQuery('#ffslider li').eq(i).attr('name',i);
jQuery('#ffslider li').eq(i).css('left', (i * 100) + "%");
imgArray.push(jQuery('#ffslider li').eq(i));
}
startAnimation();
};
function startAnimation(){
tl2.to(imgArray, 1, {left:'-=100' + '%', delay:3});
}
function upDatePosition(){
for(var i=0; i<imgLength; i++){
if((imgArray[i].css('left') <= -100 + '%')){
imgArray[i].css("left", (n - 1) * '100' + '%');
}
}
startAnimation();
}
setDefaults();
});
它的动态并用于WordPress主题。
答案 0 :(得分:0)
.animate()
函数有一个可以使用的回调方法。
这是一个简单的例子
<div></div>
div
{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 0;
}
$("div").click(function() {
$(this).animate({left: '-100%'}, 1000, function() {
alert('Move left animation finished...');
$(this).css({left: 0});
});
});
答案 1 :(得分:0)
延长我之前的答案;这是一个实际的例子:
<div class="rotator-container">
<div class="rotator">
<span id="s1"></span>
<span id="s2"></span>
<span id="s3"></span>
</div>
</div>
<a href="#">Show/hide</a>
.rotator-container
{
border: 1px solid black;
height: 100px;
width: 100px;
overflow: hidden;
}
.rotator
{
width: 3000px;
}
span
{
width: 100px;
height: 100px;
display: block;
float: left;
}
#s1 { background-color: red; }
#s2 { background-color: green; }
#s3 { background-color: blue; }
.sized
{
width: 250px;
height: 110px;
}
// Initialise...
d();
function d() {
// Remember our parent and first child elements
parent = $('.rotator');
firstChild = parent.find('span:first');
// Slide left animation
firstChild.animate({'margin-left': -100}, 1000, function() {
// Remove the current element and reset the margin
firstChild.remove().css({'margin-left': 0});
// Add the element back to the end of the parent
parent.append(firstChild);
// Callback to self (repeat the animation!)
d();
});
};
$('a').click(function(e) {
e.preventDefault();
$('.rotator-container').toggleClass('sized');
});