最后我setTimeout
按照@BradM 的建议解决了这个问题。
我敢肯定有一个更简单的方法,但我是这样做的。这是代码:
菜单点击事件:
$('.content1').click(function(e){
slide1();
e.preventDefault();
});
$('.content2').click(function(e){
slide2();
e.preventDefault();
});
$('.content3').click(function(e){
slide3();
e.preventDefault();
});
幻灯片效果
function slide1(){
next = 2; //setting which is gonna be the next slide
$('#image1').animate({'margin-left': '0px', 'margin-top': '0px'});
$('#box1').animate({'margin-top': '0px'});
hide2();
hide3();
}
function slide2(){
next = 3; //setting which is gonna be the next slide
hide1();
hide3();
$('#box2').animate({'margin-top': '0px'});
$('#image2').animate({'margin-top': '0px'});
}
function slide3(){
next = 1; //setting which is gonna be the next slide
hide1();
hide2();
$('#box3').animate({'margin-left': '0px'});
$('#image3').stop().delay(1000).show().animate({'opacity': '1'});
}
隐藏功能
function hide1(){
$('#image1').animate({'margin-left': '2000px'});
$('#box1').animate({'margin-top': '-2000px'});
}
function hide2(){
$('#box2').animate({'margin-top': '600px'});
$('#image2').animate({'margin-top': '-2000px'});
}
function hide3(){
$('#box3').animate({'margin-left': '-2000px'});
$('#image3').animate({'opacity' : '0', 'display' : 'none'}, function(){
$(this).hide();
});
}
Onmouse over/out 动作
//when its over, we stop the movement
var delay = 6000; //6 seconds
$('#slider').hover(function(){
clearTimeout(timer);
}, function(){
//starting again the animation (since the last slide)
timer = window.setTimeout(playSlide, delay);
});
主要播放幻灯片功能
var next = 2; //next slide will be the 2nd one at the start
function playSlide(){
var name = "slide" + next;
var method = eval(name);
method();
//calling recursively the function
timer = window.setTimeout(playSlide, delay);
}
第一次通话
//only if there's an slider, we initate it
if($('#slider').length){
//first call the the slider
timer = window.setTimeout(playSlide, delay);
}