jQuery でスライドショーを実行し、「次のスライド」ボタンと「前のスライド」ボタンのコードを見て、いくつかの機能が変わることを除けば、ほとんど同じであることに気付きました。
これをリファクタリングしてより効率的にできると思いますが、どうすればよいかわかりません。
誰かが私に方法を教えてもらえますか?
//UI Binding
$nextbutton.on("click", function(){
//Blocking control
if (slideshowBlocked) return;
slideshowBlocked = true;
//Get active slide
var $active = $slides.filter(".active");
//Get new Slied
var $newSlide = $active.next(".slide");
if (!$newSlide.length){
$newSlide = $slides.first();
}
//Prepare new slide beneath the active
$newSlide.css({
"z-index": 5,
"display": "block"
});
//Fade out the active
$active.fadeOut(function(){
//Update states and CSS properties
$(this).removeClass("active");
$newSlide.addClass("active").css( "z-index", 10);;
//Unblock slideshow
slideshowBlocked = false;
});
});
$prevbutton.on("click", function(){
//Blocking control
if (slideshowBlocked) return;
slideshowBlocked = true;
//Get active slide
var $active = $slides.filter(".active");
//Get new Slied
var $newSlide = $active.prev(".slide");
if (!$newSlide.length){
$newSlide = $slides.last();
}
//Prepare new slide beneath the active
$newSlide.css({
"z-index": 5,
"display": "block"
});
//Fade out the active
$active.fadeOut(function(){
//Update states and CSS properties
$(this).removeClass("active");
$newSlide.addClass("active").css( "z-index", 10);;
//Unblock slideshow
slideshowBlocked = false;
});
});