2017-03-31 81 views
0

请参阅下面的代码。此代码工作正常,但我想添加暂停/恢复,前进,后退按钮。所以如果有人点击暂停,它会停止自动旋转。我无法使用Jquery。我研究过这段代码,但我无法将按钮添加到此代码中。Javascript Image Slider - 需要添加按钮

(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: 1024px; 
 
    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 id="image1" src="SupplyImages/WF00/1489007864182_35556.jpg" alt="First" class="slide" /><!-- slides --> 
 
     <img id="image2" src="SupplyImages/WF00/1489008043581_741956.jpg" alt="Second" class="slide" /> 
 
     <img id="image3" src="SupplyImages/WF00/1489008288715_503637.png" alt="Third" class="slide" /> 
 
    </div> 
 
</div>

回答

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.index = 0; 
 
      this.total = this.slides.length; 
 
      this.timer = null; 
 
\t \t \t this.nextButton = document.querySelector('#next'); 
 
\t \t \t this.previousButton = document.querySelector('#previous'); 
 
\t \t \t this.toggleButton = document.querySelector('#toggle'); 
 
      this.action(); 
 
      this.stopStart(); 
 
\t \t \t this.toggleButton.addEventListener('click', function(e){ 
 
\t \t \t \t e.preventDefault(); 
 
\t \t \t \t this.stop(); 
 
\t \t \t }.bind(this)); 
 
\t \t \t this.nextButton.addEventListener('click', function(e){ 
 
\t \t \t \t e.preventDefault(); 
 
\t \t \t \t this.next(); 
 
\t \t \t }.bind(this)); 
 
\t \t \t this.previousButton.addEventListener('click', function(e){ 
 
\t \t \t \t e.preventDefault(); 
 
\t \t \t \t this.previous(); 
 
\t \t \t }.bind(this)) 
 
     }, 
 
     _slideTo: function(slide) { 
 
      var currentSlide = this.slides[slide]; 
 
      currentSlide.style.opacity = 1; 
 

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

 
      }, 5000); 
 
     }, 
 
     stopStart: function() { 
 
      var self = this; 
 
      self.el.addEventListener("mouseover", function(){ 
 
\t \t \t \t \t clearInterval(self.timer); 
 
\t \t \t \t \t self.timer = null; \t 
 
\t \t \t }, false); 
 
      self.el.addEventListener("mouseout", function() { 
 
       self.action(); 
 

 
      }, false); 
 
     }, 
 
\t \t stop: function(){ 
 
\t \t \t if(this.timer){ 
 
\t \t \t \t clearInterval(this.timer); 
 
\t \t \t \t this.timer = null; 
 
\t \t \t \t this.toggleButton.innerText = 'Play'; 
 
\t \t \t }else{ 
 
\t \t \t \t this.action(); 
 
\t \t \t \t this.toggleButton.innerText = 'Pause'; 
 
\t \t \t } 
 
\t 
 
\t \t }, 
 
\t \t next: function(){ 
 
\t \t \t this.index++; 
 
\t \t \t if(this.index === this.slides.length) { 
 
\t \t \t \t this.index = 0; 
 
\t \t \t } 
 
\t \t \t this._slideTo(this.index); 
 

 
\t \t }, 
 
\t \t previous: function(){ 
 
\t \t \t this.index--; 
 
\t \t \t if(this.index <= 0) { 
 
\t \t \t \t this.index = this.slides.length - 1; 
 
\t \t \t } 
 
\t \t \t this._slideTo(this.index); \t \t \t 
 
\t \t } 
 

 

 
    }; \t 
 
    document.addEventListener("DOMContentLoaded", function() { 
 

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

 
    }); 
 

 

 
})();
\t html,body { 
 
\t \t margin: 0; 
 
\t \t padding: 0; 
 
\t } 
 
\t .slider { 
 
\t \t width: 1024px; 
 
\t \t margin: 2em auto; 
 

 
\t } 
 

 
\t .slider-wrapper { 
 
\t \t width: 100%; 
 
\t \t height: 350px; 
 
\t \t position: relative; 
 
\t } 
 

 
\t .slide { 
 
\t \t float: left; 
 
\t \t position: absolute; 
 
\t \t opacity: 0; 
 
\t \t transition: opacity 1s linear; 
 
\t } 
 

 
\t .slider-wrapper > .slide:first-child { 
 
\t \t opacity: 1; 
 
\t }
<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<div class="slider" id="main-slider"><!-- outermost container element --> 
 
    <div class="slider-wrapper"><!-- innermost wrapper element --> 
 
     <img id="image1" src="http://placehold.it/350x250?text=1" alt="First" class="slide" /><!-- slides --> 
 
     <img id="image2" src="http://placehold.it/350x250?text=2" alt="Second" class="slide" /> 
 
     <img id="image3" src="http://placehold.it/350x250?text=3" alt="Third" class="slide" /> 
 
    </div> \t 
 
</div> 
 
<div class="buttonBlock"> 
 
\t <button id="toggle">Pause</button> 
 
\t <button id="next">Next</button> 
 
\t <button id="previous">Previous</button> 
 
</div> \t 
 
</body> 
 
</html>

还有其他的方法来做到这一点,这似乎是根据您当前的代码库的好方法。希望这可以帮助。