简体   繁体   English

香草Javascript滑块不起作用

[英]Vanilla Javascript slider not working

There are 3 css classes/phases. 有3个CSS类/阶段。

The active class is the current background that is show. 活动类是显示的当前背景。

The unactive class is given to the previous background. 非活动类被赋予先前的背景。 And it makes the background slide out to the left of the screen. 并使背景滑到屏幕左侧。

The transport class is given to the background after it has received the unactive class. 接收到非活动类后,将传输类分配给后台。 The transport class moves the background back to the right of the screen. 传输类将背景移回屏幕右侧。

The slider totally ignores the unactive class for some reason. 出于某种原因,滑块完全忽略了非活动类。 The background never slides to the left of the screen. 背景永远不会滑动到屏幕的左侧。

 var slides = document.getElementsByClassName('bg'); var i = 0; // When page loads show first background (function() { slides[i].className += ' active'; i++; })(); function changeSlide() { // Slide previous slide to the left of the screen if(i === 0) { slides[2].className = 'bg unactive'; } else { slides[i - 1].className = 'bg unactive'; } // Slide the current slide in from the right of the screen slides[i].className += ' active'; // Make the slide go back to the right of the screen if(i === 0) { slides[2].className = 'bg transport'; slides[2].className = 'bg'; } else { slides[i - 1].className = 'bg transport'; slides[i - 1].className = 'bg'; } i++ if(i === slides.length) { i = 0; } } setInterval(changeSlide, 2000); 
 * { margin: 0; padding: 0; } html, body, .bg { width: 100%; height: 100%; } .bg { position: absolute; left: 100%; background-color: tan; -webkit-transition: all 0.5s linear; -o-transition: all 0.5s linear; -moz-transition: all 0.5s linear; transition: all 0.5s linear; } /* The background that is shown */ .active { position: absolute; left: 0; } /* Make the background go to the left of the screen */ .unactive { position: absolute; left: -100%; } /* Hide the background and make it go back to the right of the screen */ .transport { display: none; position: absolute; left: 100%; z-index: -1; } 
 <!-- background 1 --> <div class="bg" style="background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Birnbaum_am_Lerchenberg_retouched.jpg/310px-Birnbaum_am_Lerchenberg_retouched.jpg)"> </div> <!-- background 2 --> <div class="bg" style="background-image: url(https://cdn.pixabay.com/photo/2015/03/29/01/54/tree-696839_960_720.jpg)"> </div> <!-- background 3 --> <div class="bg" style="background-image: url(http://www.slate.com/content/dam/slate/articles/health_and_science/science/2017/06/170621_SCI_TreePlantingHoax.jpg.CROP.promo-xlarge2.jpg)"></div> 

Check out this codepen. 签出此codepen。 I have the same code above except I commented out a block of javascript code. 除了注释掉一段JavaScript代码外,我上面的代码相同。 Watch the slides go in and out. 观看幻灯片进出幻灯片。 That is how I want it. 那就是我想要的。 But I want the slider to infinitely repeat and never stop. 但是我希望滑块无限重复,并且永不停止。

https://codepen.io/anon/pen/aVoNNd https://codepen.io/anon/pen/aVoNNd

You are overwriting unactive with slides[ ].className = 'bg'; 您正在使用slides[ ].className = 'bg';覆盖unactive slides[ ].className = 'bg'; a few lines below (same with transport ), therefore its never applied. 下面几行(与transport相同),因此从未应用。

I also had to change some z-index values and remove a few things to make it work. 我还必须更改一些z-index值并删除一些内容以使其工作。 (See the comments in the code) (请参阅代码中的注释)

 var slides = document.getElementsByClassName('bg'); var i = 0; // When page loads show first background (function() { slides[i].className += ' active'; i++; })(); function changeSlide() { // Slide previous slide to the left of the screen if(i === 0) { slides[slides.length-1].className = 'bg unactive';//Changed 2 to slides.length-1 to avoid hardcoding values } else { slides[i - 1].className = 'bg unactive'; } // Slide the current slide in from the right of the screen slides[i].className = 'bg active';// removed += to override transport // Make the slide go back to the right of the screen // prepare NEXT slide if(i === slides.length-1) { slides[0].className = 'bg transport'; //slides[2].className = 'bg'; // dont override transport } else { slides[i + 1].className = 'bg transport'; //slides[i - 1].className = 'bg'; // dont override transport } i++ if(i === slides.length) { i = 0; } } setInterval(changeSlide, 2000); 
 * { margin: 0; padding: 0; } html, body, .bg { width: 100%; height: 100%; } .bg { position: absolute; left: 100%; background-color: tan; -webkit-transition: all 0.5s linear; -o-transition: all 0.5s linear; -moz-transition: all 0.5s linear; transition: all 0.5s linear; } /* The background that is shown */ .active { position: absolute; left: 0; } /* Make the background go to the left of the screen */ .unactive { position: absolute; left: -100%; z-index: -1; /*added*/ } /* Hide the background and make it go back to the right of the screen */ .transport { /*display: none;*/ position: absolute; left: 100%; z-index: -2; /*changed to -2*/ } 
 <!-- background 1 --> <div class="bg" style="background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Birnbaum_am_Lerchenberg_retouched.jpg/310px-Birnbaum_am_Lerchenberg_retouched.jpg)"> </div> <!-- background 2 --> <div class="bg" style="background-image: url(https://cdn.pixabay.com/photo/2015/03/29/01/54/tree-696839_960_720.jpg)"> </div> <!-- background 3 --> <div class="bg" style="background-image: url(http://www.slate.com/content/dam/slate/articles/health_and_science/science/2017/06/170621_SCI_TreePlantingHoax.jpg.CROP.promo-xlarge2.jpg)"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM