我有 2 个测试页面http://www.emoceanstudios.com.au/test.php和http://www.emoceanstudios.com.au/test2.php,这是第一页的相关脚本:
<script type="text/javascript">
function slide_out(){
$('#red-block').animate({
marginLeft: -278
}, 500);
$('#yellow-block').animate({
marginTop: -316
}, 500);
$('#gray-block').animate({
marginLeft: 278
}, 500, function(){
$('#three-color-container').fadeOut(500, function() {
$('#three-color-container-new').fadeIn(500, function() {
window.setTimeout(function(){slide_in()}, 4000);
});
});
});
}
function slide_in(){
$('#three-color-container-new').fadeOut(500, function() {
$('#three-color-container').fadeIn(500, function(){
$('#red-block, #yellow-block, #gray-block').animate({
marginLeft: 0,
marginTop: 0
}, 500, function() {
window.setTimeout(function(){slide_out()}, 4000);
});
});
});
}
window.setTimeout(function(){slide_out()}, 4000);
</script>
现在红色黄色灰色块像这样滑动:出,入,出,入,出然后闪烁(这不是我想要的),仅适用于两个半循环。如果我将计时器从 4000 设置为 6000,它最多可以工作 3 个循环,然后也会中断。
我希望它永远出入入出入出入出入出入出入出入出setTimeout 函数。但是有人怀疑这是因为三个animate调用了next函数3次,所以我修改并创建了第二个页面,将三个animation放在一个函数中然后回调next函数,代码如下:
<script type="text/javascript">
function slide_out(){
block_out(callback);
}
function slide_in(){
$('#three-color-container-new').fadeOut(500, function() {
$('#three-color-container').fadeIn(500, function(){
$('#red-block, #yellow-block, #gray-block').animate({
marginLeft: 0,
marginTop: 0
}, 500, function() {
window.setTimeout(function(){slide_out()}, 4000);
});
});
});
}
function block_out(callback){
$('#red-block').animate({
marginLeft: -278
}, 500);
$('#yellow-block').animate({
marginTop: -316
}, 500);
$('#gray-block').animate({
marginLeft: 278
}, 500);
callback();
}
function callback(){
$('#three-color-container').fadeOut(500, function() {
$('#three-color-container-new').fadeIn(500, function() {
window.setTimeout(function(){slide_in()}, 4000);
});
});
}
window.setTimeout(function(){slide_out()}, 4000);
</script>
现在它变成了 4-5 循环的工作,然后再次中断。