私は多かれ少なかれ Jquery の初心者であり、まずこれに基づいて、クリック可能な循環スライドショーを作成しようとしました。ここまでは順調ですね。コードは機能します (以下を参照)。私の質問:
コードを書く簡単な方法はありませんか。前と次で同じコードを繰り返しています。
次/前のリンクをクリックするとアニメーションが開始され、アニメーション中にもう一度クリックすると、画像がちらついたり、エラーが発生したりすることがあります。クリックに反応する前に、アニメーションを強制的に終了させる方法はありますか?
Javascript:
$(function() {
$('#prev').click(function() {
var $active = $('#slideshow IMG.active');
var $prev = $active.prev();
$active.addClass('last-active');
if($active.is(":first-child")){$prev = $("IMG:last-child");}
$prev.css({opacity: 0.0})
.addClass('active')
.animate({opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
});
$('#next').click(function() {
var $active = $('#slideshow IMG.active');
var $next = $active.next();
$active.addClass('last-active');
if($active.is(":last-child")){$next = $("IMG:first-child");}
$next.css({opacity: 0.0})
.addClass('active')
.animate({opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
});
});
CSS:
#slideshow {position:relative; height:350px; opacity:1.0;}
#slideshow IMG {position:absolute; top:0; left:0; z-index:8; opacity:0.0;}
#slideshow IMG.active {z-index:10; opacity:1.0;}
#slideshow IMG.last-active {z-index:9;}
#next {color:#0000ff; cursor:pointer;}
#prev {color:#0000ff; cursor:pointer;}
HTML:
<div id="slideshow">
<img src="image1.jpg" class="active" />
<img src="image2.jpg" />
<img src="image3.jpg" />
<img src="image4.jpg" />
</div>
<div id="next">NEXT</div>
<div id="prev">PREV</div>