我正在为我的滑块使用一个非常简单的 JS,目前图像水平滑动,向左或向右滑动,具体取决于您按下的箭头。我遇到的问题是动画没有延迟,所以如果你快速单击方向箭头几次,幻灯片需要一段时间才能赶上,并且有很多奇怪的重叠。
我想将动画效果更改为淡入淡出并设置延迟,以便在当前幻灯片完成动画之前您无法前进到下一张或上一张幻灯片。任何帮助将不胜感激!
var slideInProgress = false;
var currentSlideIndex = 0,
slides;
function setTopSlider(){
if (jQuery('#top_slider #container .slide').length != 0) {
slides = jQuery('#top_slider #container > .slide');
for(var i=0; i <= slides.length; i++){
jQuery(slides[i]).css('left','100%');
};
jQuery(slides[currentSlideIndex]).css('left','0%');
var el=jQuery('.dot:eq('+currentSlideIndex+')');
src = el.css("background-image").replace("_off", "_over");
el.css("background-image", src);
el.addClass('active');
};
};
function slide(dir) {
var current, next, nextSlide;
current = jQuery(slides[currentSlideIndex]);
if(!isNaN(dir)){
next = dir;
if(next > currentSlideIndex )dir = 'left';
else dir = 'right';
};
if(dir == 'left'){
if(!next){
next = currentSlideIndex + 1;
if(next >= slides.length){
next = 0;
}
}
if (slideInProgress) {
return;
}
slideInProgress = true;
nextSlide = jQuery(slides[next]);
nextSlide.css('left','100%');
nextSlide.css('z-index',parseInt(current.css('z-index'), 10)+1);
nextSlide.animate({left: '0%'}, 1500);
current.animate({left: '-100%'}, 1500);
}else{
console.log('moving right');
if(!next){
next = currentSlideIndex - 1;
if(next < 0){
next = slides.length-1;
}
}
nextSlide = jQuery(slides[next]);
nextSlide.css('left','-100%');
nextSlide.css('z-index',parseInt(current.css('z-index'), 10)+1);
nextSlide.animate({left: '0%'}, 1500);
current.animate({left: '100%'}, 1500);
omplete: function(){
slideInProgress = false;
});
};
current.addClass('active');
nextSlide.removeClass('active');
var el=jQuery('.dot:eq('+currentSlideIndex+')');
src = el.css("background-image").replace("_over", "_off");
el.css("background-image", src);
el.removeClass('active');
el=jQuery('.dot:eq('+next+')');
src = el.css("background-image").replace("_off", "_over");
el.css("background-image", src);
el.addClass('active');
console.log('currentSlideIndex'+currentSlideIndex);
console.log('next'+next);
console.log('dir'+dir);
currentSlideIndex = next;
};