2017-06-16 138 views
0

我试图创建一个幻灯片,其中每张幻灯片显示2000毫秒的时间间隔,但是,我无法获得第二张幻灯片显示...我已经包括我的Javascript和CSS下面任何帮助或简单的修改,你认为会有所帮助,将不胜感激。我相信这是一个简单的修补程序,但我只需要另一双眼睛,以帮助这个问题...Javascript幻灯片不工作

body{ 
 
    background: white; 
 
    font-family: 'Raleway', sans-serif; 
 
    margin: 0 auto; 
 
} 
 
/* FOR SLIDES */ 
 

 
/* 
 
essential styles: 
 
these make the slideshow work 
 
*/ 
 
#slides{ 
 
\t position: relative; 
 
\t height: 100%; 
 
\t padding: 0px; 
 
\t list-style-type: none; 
 
} 
 

 
.slide{ 
 
\t position: absolute; 
 
\t left: 0px; 
 
\t top: 0px; 
 
\t width: 100%; 
 
\t height: 100%; 
 
\t opacity: 0; 
 
\t z-index: 1; 
 

 
\t -webkit-transition: opacity 1s; 
 
\t -moz-transition: opacity 1s; 
 
\t -o-transition: opacity 1s; 
 
\t transition: opacity 1s; 
 
} 
 

 
.showing{ 
 
\t opacity: 1; 
 
\t z-index: 2; 
 
} 
 

 

 

 
/* 
 
non-essential styles: 
 
just for appearance; change whatever you want 
 
*/ 
 

 
.slide{ 
 
\t font-size: 40px; 
 
\t padding: 40px; 
 
\t box-sizing: border-box; 
 
\t background: #333; 
 
\t color: #fff; 
 
} 
 

 
.slide:nth-of-type(1){ 
 
\t background: url(Slide0.png) no-repeat; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(2){ 
 
\t background: url(Slide1.png) no-repeat; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(3){ 
 
\t background: url(Slide2.png) no-repeat; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(4){ 
 
\t background: url(Slide3.png) no-repeat; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(5){ 
 
\t background: url(Slide4.png) no-repeat; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
}
<DOCTYPE! html> 
 
    <head> 
 
     <link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet"> 
 
     <!-- CSS --> 
 

 
     <link rel="stylesheet" type="text/css" href="style.css"/> 
 
     <title>Widget1.0</title> 
 
     <!-- JS!!! --> 
 
     <script> 
 
     var slides = document.querySelectorAll('#slides .slide'); 
 
     var currentSlide = 0; 
 
     var slideInterval = setInterval(nextSlide,2000); 
 

 
     function nextSlide(){ 
 
     slides[currentSlide].className = 'slide'; 
 
     currentSlide = (currentSlide+1)%slides.length; 
 
     slides[currentSlide].className = 'slide showing'; 
 
     } 
 

 
     var playing = true; 
 
     var pauseButton = document.getElementById('pause'); 
 

 
     function pauseSlideshow(){ 
 
     pauseButton.innerHTML = 'Play'; 
 
     playing = false; 
 
     clearInterval(slideInterval); 
 
     } 
 

 
     function playSlideshow(){ 
 
     pauseButton.innerHTML = 'Pause'; 
 
     playing = true; 
 
     slideInterval = setInterval(nextSlide,2000); 
 
     } 
 

 
     pauseButton.onclick = function(){ 
 
     if(playing){ pauseSlideshow(); } 
 
     else{ playSlideshow(); } 
 
     }; 
 
    </script> 
 
    </head> 
 
    <body> 
 
     <ul id="slides"> 
 
     <li class="slide showing" id="slide1">Slide 1</li> 
 
     <li class="slide" id="slide2">Slide 2</li> 
 
     <li class="slide" id="slide3">Slide 3</li> 
 
     <li class="slide" id="slide4">Slide 4</li> 
 
     <li class="slide" id="slide5">Slide 5</li> 
 
     </ul> 
 
     <button class="controls" id="pause">Pause</button> 
 
    </body> 
 
</html>

回答

0

您需要将您的JavaScript上DOM准备的事件或元素被渲染后的移动功能,即:刚刚</body>标签

body{ 
 
    background: white; 
 
    font-family: 'Raleway', sans-serif; 
 
    margin: 0 auto; 
 
} 
 
/* FOR SLIDES */ 
 

 
/* 
 
essential styles: 
 
these make the slideshow work 
 
*/ 
 
#slides{ 
 
\t position: relative; 
 
\t height: 200px; 
 
\t padding: 0px; 
 
\t list-style-type: none; 
 
} 
 

 
.slide{ 
 
\t position: absolute; 
 
\t left: 0px; 
 
\t top: 0px; 
 
\t width: 100%; 
 
\t height: 100%; 
 
\t opacity: 0; 
 
\t z-index: 1; 
 

 
\t -webkit-transition: opacity 1s; 
 
\t -moz-transition: opacity 1s; 
 
\t -o-transition: opacity 1s; 
 
\t transition: opacity 1s; 
 
} 
 

 
.showing{ 
 
\t opacity: 1; 
 
\t z-index: 2; 
 
} 
 

 

 

 
/* 
 
non-essential styles: 
 
just for appearance; change whatever you want 
 
*/ 
 

 
.slide{ 
 
\t font-size: 40px; 
 
\t padding: 40px; 
 
\t box-sizing: border-box; 
 
\t background: #333; 
 
\t color: #fff; 
 
} 
 

 
.slide:nth-of-type(1){ 
 
\t background-color: red; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(2){ 
 
\t background-color: blue; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(3){ 
 
\t background-color: green; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(4){ 
 
\t background-color: magenta; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
.slide:nth-of-type(5){ 
 
\t background-color: orange; 
 
    background-size: cover; 
 
    background-position: 50% 50%; 
 
} 
 
#pause
<DOCTYPE! html> 
 
    <head> 
 
     <link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet"> 
 
     <!-- CSS --> 
 

 
     <link rel="stylesheet" type="text/css" href="style.css"/> 
 
     <title>Widget1.0</title> 
 
    </head> 
 
    <body> 
 
     <ul id="slides"> 
 
     <li class="slide showing" id="slide1">Slide 1</li> 
 
     <li class="slide" id="slide2">Slide 2</li> 
 
     <li class="slide" id="slide3">Slide 3</li> 
 
     <li class="slide" id="slide4">Slide 4</li> 
 
     <li class="slide" id="slide5">Slide 5</li> 
 
     </ul> 
 
     <button class="controls" id="pause">Pause</button> 
 
     <!-- JS!!! --> 
 
     <script> 
 
     var slides = document.querySelectorAll('#slides .slide'); 
 
     var currentSlide = 0; 
 
     var slideInterval = setInterval(nextSlide,2000); 
 

 
     function nextSlide(){ 
 
     slides[currentSlide].className = 'slide'; 
 
     currentSlide = (currentSlide+1)%slides.length; 
 
     slides[currentSlide].className = 'slide showing'; 
 
     } 
 

 
     var playing = true; 
 
     var pauseButton = document.getElementById('pause'); 
 

 
     function pauseSlideshow(){ 
 
     pauseButton.innerHTML = 'Play'; 
 
     playing = false; 
 
     clearInterval(slideInterval); 
 
     } 
 

 
     function playSlideshow(){ 
 
     pauseButton.innerHTML = 'Pause'; 
 
     playing = true; 
 
     slideInterval = setInterval(nextSlide,2000); 
 
     } 
 

 
     pauseButton.onclick = function(){ 
 
     if(playing){ pauseSlideshow(); } 
 
     else{ playSlideshow(); } 
 
     }; 
 
    </script> 
 

 
</body> 
 
</html>

以上
0

这只是执行代码。你应该设置你的增值经销商,注册点击监听,并开始在页面负载的setInterval(甚至更好的使用jQuery和使用

  1. 我补充说,body.onload运行的初始化函数,其设置ready事件您 全局变量,点击监听,并启动setInterval的
  2. 我评论了,你在上面设置你的全局变量,只是宣布他们,因为他们现在到了init()设置

<DOCTYPE! html> 
 
    <head> 
 
     <link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet"> 
 
     <!-- CSS --> 
 

 
     <!-- <link rel="stylesheet" type="text/css" href="style.css"/> --> 
 
     <title>Widget1.0</title> 
 
     <style type='text/css'> 
 
\t \t \t body{ 
 
\t \t \t background: white; 
 
\t \t \t font-family: 'Raleway', sans-serif; 
 
\t \t \t margin: 0 auto; 
 
\t \t \t } 
 
\t \t \t /* FOR SLIDES */ 
 

 
\t \t \t /* 
 
\t \t \t essential styles: 
 
\t \t \t these make the slideshow work 
 
\t \t \t */ 
 
\t \t \t #slides{ 
 
\t \t \t \t position: relative; 
 
\t \t \t \t height: 100%; 
 
\t \t \t \t padding: 0px; 
 
\t \t \t \t list-style-type: none; 
 
\t \t \t } 
 

 
\t \t \t .slide{ 
 
\t \t \t \t position: absolute; 
 
\t \t \t \t left: 0px; 
 
\t \t \t \t top: 0px; 
 
\t \t \t \t width: 100%; 
 
\t \t \t \t height: 100%; 
 
\t \t \t \t opacity: 0; 
 
\t \t \t \t z-index: 1; 
 

 
\t \t \t \t -webkit-transition: opacity 1s; 
 
\t \t \t \t -moz-transition: opacity 1s; 
 
\t \t \t \t -o-transition: opacity 1s; 
 
\t \t \t \t transition: opacity 1s; 
 
\t \t \t } 
 

 
\t \t \t .showing{ 
 
\t \t \t \t opacity: 1; 
 
\t \t \t \t z-index: 2; 
 
\t \t \t } 
 

 

 

 
\t \t \t /* 
 
\t \t \t non-essential styles: 
 
\t \t \t just for appearance; change whatever you want 
 
\t \t \t */ 
 

 
\t \t \t .slide{ 
 
\t \t \t \t font-size: 40px; 
 
\t \t \t \t padding: 40px; 
 
\t \t \t \t box-sizing: border-box; 
 
\t \t \t \t background: #333; 
 
\t \t \t \t color: #fff; 
 
\t \t \t } 
 

 
\t \t \t .slide:nth-of-type(1){ 
 
\t \t \t \t background: url(http://placehold.it/720x246?text=Slide0.png) no-repeat; 
 
\t \t \t background-size: cover; 
 
\t \t \t background-position: 50% 50%; 
 
\t \t \t } 
 
\t \t \t .slide:nth-of-type(2){ 
 
\t \t \t \t background: url(http://placehold.it/720x246?text=Slide1.png) no-repeat; 
 
\t \t \t background-size: cover; 
 
\t \t \t background-position: 50% 50%; 
 
\t \t \t } 
 
\t \t \t .slide:nth-of-type(3){ 
 
\t \t \t \t background: url(http://placehold.it/720x246?text=Slide2.png) no-repeat; 
 
\t \t \t background-size: cover; 
 
\t \t \t background-position: 50% 50%; 
 
\t \t \t } 
 
\t \t \t .slide:nth-of-type(4){ 
 
\t \t \t \t background: url(http://placehold.it/720x246?text=Slide3.png) no-repeat; 
 
\t \t \t background-size: cover; 
 
\t \t \t background-position: 50% 50%; 
 
\t \t \t } 
 
\t \t \t .slide:nth-of-type(5){ 
 
\t \t \t \t background: url(http://placehold.it/720x246?text=Slide4.png) no-repeat; 
 
\t \t \t background-size: cover; 
 
\t \t \t background-position: 50% 50%; 
 
\t \t \t } 
 
     </style> 
 
     <!-- JS!!! --> 
 
     <script> 
 
     var slides;// = document.querySelectorAll('#slides .slide'); 
 
     var currentSlide;// = 0; 
 
     var slideInterval;// = setInterval(nextSlide,2000); 
 

 
     function nextSlide(){ 
 
     slides[currentSlide].className = 'slide'; 
 
     currentSlide = (currentSlide+1)%slides.length; 
 
     slides[currentSlide].className = 'slide showing'; 
 
     } 
 

 
     var playing;// = true; 
 
     var pauseButton;// = document.getElementById('pause'); 
 

 
     function pauseSlideshow(){ 
 
     pauseButton.innerHTML = 'Play'; 
 
     playing = false; 
 
     clearInterval(slideInterval); 
 
     } 
 

 
     function playSlideshow(){ 
 
     pauseButton.innerHTML = 'Pause'; 
 
     playing = true; 
 
     slideInterval = setInterval(nextSlide,2000); 
 
     } 
 

 
     function init() 
 
     { 
 
     \t playing = true; 
 
     \t pauseButton = document.getElementById('pause'); 
 
     \t slides = document.querySelectorAll('#slides .slide'); 
 
     \t currentSlide = 0; 
 
     \t slideInterval = setInterval(nextSlide,2000); 
 
     \t pauseButton.onclick = function(){ 
 
     if(playing){ pauseSlideshow(); } 
 
\t   else{ playSlideshow(); } 
 
\t  }; 
 
     } 
 
    </script> 
 
    </head> 
 
    <body onload="init()"> 
 
     <ul id="slides"> 
 
     <li class="slide showing" id="slide1">Slide 1</li> 
 
     <li class="slide" id="slide2">Slide 2</li> 
 
     <li class="slide" id="slide3">Slide 3</li> 
 
     <li class="slide" id="slide4">Slide 4</li> 
 
     <li class="slide" id="slide5">Slide 5</li> 
 
     </ul> 
 
     <button class="controls" id="pause">Pause</button> 
 
    </body> 
 
</html>