2017-06-27 1 views
-1

自動スライドショーを作成しました。自動スライドショーと同時にボタンを追加して動作させる方法を知りたいと思います。誰でも助けてくれますか?自動スライドショーにボタンを追加するにはどうしたらいいですか?

(function() { 
 

 
    function Slideshow(element) { 
 
    this.el = document.querySelector(element); 
 
    this.init(); 
 
    } 
 

 
    Slideshow.prototype = { 
 
    init: function() { 
 
     this.wrapper = this.el.querySelector(".slider-wrapper"); 
 
     this.slides = this.el.querySelectorAll(".slide"); 
 
     this.previous = this.el.querySelector(".slider-previous"); 
 
     this.next = this.el.querySelector(".slider-next"); 
 
     this.index = 0; 
 
     this.total = this.slides.length; 
 
     this.timer = null; 
 

 
     this.action(); 
 
     this.stopStart(); 
 
    }, 
 
    _slideTo: function(slide) { 
 
     var currentSlide = this.slides[slide]; 
 
     currentSlide.style.opacity = 1; 
 

 
     for (var i = 0; i < this.slides.length; i++) { 
 
     var slide = this.slides[i]; 
 
     if (slide !== currentSlide) { 
 
      slide.style.opacity = 0; 
 
     } 
 
     } 
 
    }, 
 
    action: function() { 
 
     var self = this; 
 
     self.timer = setInterval(function() { 
 
     self.index++; 
 
     if (self.index == self.slides.length) { 
 
      self.index = 0; 
 
     } 
 
     self._slideTo(self.index); 
 

 
     }, 3000); 
 
    }, 
 
    stopStart: function() { 
 
     var self = this; 
 
     self.el.addEventListener("mouseover", function() { 
 
     clearInterval(self.timer); 
 
     self.timer = null; 
 

 
     }, false); 
 
     self.el.addEventListener("mouseout", function() { 
 
     self.action(); 
 

 
     }, false); 
 
    } 
 

 

 
    }; 
 

 
    document.addEventListener("DOMContentLoaded", function() { 
 

 
    var slider = new Slideshow("#main-slider"); 
 

 
    }); 
 

 

 
})();
html, 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
.slider { 
 
    width: 100%; 
 
    margin: 2em auto; 
 
} 
 

 
.slider-wrapper { 
 
    width: 100%; 
 
    height: 400px; 
 
    position: relative; 
 
} 
 

 
.slide { 
 
    float: left; 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
    opacity: 0; 
 
    transition: opacity 3s linear; 
 
} 
 

 
.slider-wrapper>.slide:first-child { 
 
    opacity: 1; 
 
}
<div class="slider" id="main-slider"> 
 
    <!-- outermost container element --> 
 
    <div class="slider-wrapper"> 
 
    <!-- innermost wrapper element --> 
 
    <img src="http://lorempixel.com/1024/400/animals" alt="First" class="slide" style="width:100%" /> 
 
    <!-- slides --> 
 
    <img src="http://lorempixel.com/1024/400/business" alt="Second" class="slide" style="width:100%" /> 
 
    <img src="http://lorempixel.com/1024/400/city" alt="Third" class="slide" style="width:100%" /> 
 
    </div> 
 
</div>

+0

....例のように、次や前の機能を追加しますか?前の次の機能については?? – Aatman

+0

はい、@ GHOST93 .. –

+0

私が示唆できるひとつのことは、ボタンを追加するJSクラスにプライベートメソッドを持たせることができ、CSSでこれらのスタイルを書くことができるので、レンダリング後にクラスごとにスタイリングされますコード化される。 あなたが必要とするレストコードは、すでにハサン氏の答えです! – Aatman

答えて

0

は、あなたが正確に何のためのボタンを配置する

(function() { 
 

 
    function Slideshow(element) { 
 
    this.el = document.querySelector(element); 
 
    this.init(); 
 
    } 
 

 
    Slideshow.prototype = { 
 
    init: function() { 
 
     this.wrapper = this.el.querySelector(".slider-wrapper"); 
 
     this.slides = this.el.querySelectorAll(".slide"); 
 
     this.previous = this.el.querySelector(".slider-previous"); 
 
     this.next = this.el.querySelector(".slider-next"); 
 
     this.index = 0; 
 
     this.total = this.slides.length; 
 
     this.timer = null; 
 
     this.nextButton= this.el.querySelector(".next"); 
 
     this.prevButton= this.el.querySelector(".prev"); 
 

 
     this.action(); 
 
     this.stopStart(); 
 
     this.nextSlide(); 
 
     this.prevSlide(); 
 
    }, 
 
    _slideTo: function(slide) { 
 
     var currentSlide = this.slides[slide]; 
 
     currentSlide.style.opacity = 1; 
 

 
     for (var i = 0; i < this.slides.length; i++) { 
 
     var slide = this.slides[i]; 
 
     if (slide !== currentSlide) { 
 
      slide.style.opacity = 0; 
 
     } 
 
     } 
 
    }, 
 
    action: function() { 
 
     var self = this; 
 
     self.timer = setInterval(function() { 
 
     self.index++; 
 
     if (self.index == self.slides.length) { 
 
      self.index = 0; 
 
     } 
 
     self._slideTo(self.index); 
 

 
     }, 3000); 
 
    }, 
 
    stopStart: function() { 
 
     var self = this; 
 
     self.el.addEventListener("mouseover", function() { 
 
     clearInterval(self.timer); 
 
     self.timer = null; 
 

 
     }, false); 
 
     self.el.addEventListener("mouseout", function() { 
 
     self.action(); 
 

 
     }, false); 
 
    } 
 
    ,nextSlide:function(){ 
 
     var self=this; 
 
     self.nextButton.addEventListener("click", function() { 
 
     clearInterval(self.timer); 
 
     self.timer = null; 
 
     self.index++; 
 
     if (self.index == self.slides.length) { 
 
      self.index = 0; 
 
     } 
 
     self._slideTo(self.index); 
 

 
     }); 
 
    } 
 
    ,prevSlide:function(){ 
 
     var self=this; 
 
     self.prevButton.addEventListener("click", function() { 
 
     clearInterval(self.timer); 
 
     self.timer = null; 
 
     self.index--; 
 
     if (self.index == -1) { 
 
      self.index = self.slides.length-1; 
 
     } 
 
     
 
     self._slideTo(self.index); 
 

 
     }); 
 
    } 
 

 

 
    }; 
 

 
    document.addEventListener("DOMContentLoaded", function() { 
 

 
    var slider = new Slideshow("#main-slider"); 
 

 
    }); 
 

 

 
})();
html, 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
.slider { 
 
    width: 100%; 
 
    margin: 2em auto; 
 
} 
 

 
.slider-wrapper { 
 
    width: 100%; 
 
    height: 400px; 
 
    position: relative; 
 
} 
 

 
.slide { 
 
    float: left; 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
    opacity: 0; 
 
    transition: opacity 3s linear; 
 
} 
 

 
.slider-wrapper>.slide:first-child { 
 
    opacity: 1; 
 
} 
 
.next{ 
 
position:absolute; 
 
right:0; 
 
} 
 
.prev{ 
 
position:absolute; 
 
left:0; 
 
}
<div class="slider" id="main-slider"> 
 
    <!-- outermost container element --> 
 
    <div class="slider-wrapper"> 
 
    <!-- innermost wrapper element --> 
 
    <img src="http://lorempixel.com/1024/400/animals" alt="First" class="slide" style="width:100%" /> 
 
    <!-- slides --> 
 
    <img src="http://lorempixel.com/1024/400/business" alt="Second" class="slide" style="width:100%" /> 
 
    <img src="http://lorempixel.com/1024/400/city" alt="Third" class="slide" style="width:100%" /> 
 
    <button class='prev'>Prev</button> 
 
    <button class='next'>Next</button> 
 
    </div> 
 
</div>

+0

ありがとう!あなたは本当に私の問題を解決した –

0

あなたの次の& prevuesボタンのクリックイベント関数にし、次の使用のために、次の

self._slideTo(self.index); 
を追加します。ここで私は、自動スライドショーを作成するために書かれたコードですこのプレビュー用に

を使用してください。

if(self.index+1 == this.slides.length) 
self.index = 0 ; 
else 
self._slideTo(self.index -1); 
関連する問題