私はスライダーに非常に単純な 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;
};