简体   繁体   English

简单的幻灯片放映第一张幻灯片完全淡出

[英]Simple Slideshow First Slide Entirely Fades Out

I'm creating a simple slideshow and for some reason the first slide will always fade entirely out before fading in the next list item on the first loop through. 我正在创建一个简单的幻灯片,由于某种原因,第一张幻灯片将始终完全淡出,然后淡入到第一个循环中的下一个列表项。 Everything after the first transition is perfectly fine it's just the first loaded index. 第一次转换后的一切都很好,它只是第一个加载的索引。

I've tried playing with the opacity, how it's being displayed ( block, list-item, etc. ), tried fading in the next slide first before attempting to fade out the current list item, different jquery loads ( document ready, window load ). 我尝试过不透明度,显示方式(块,列表项等),先尝试在下一张幻灯片中淡入淡出,然后尝试淡出当前列表项,不同的jquery加载(文档就绪,窗口加载) )。

Why is it only happening on the first iteration and how can I fix it? 为什么只在第一次迭代中发生,如何解决?

 function fadeBanners() { var totalPanels = $('#bgrs li').length; var currIndex = $('#bgrs li.selected').index(); var nextPanel = ( currIndex + 1 ) % totalPanels; setPanel( nextPanel ); } function setPanel(referencePanel) { var currIndex = referencePanel; var currPanel = $('#bgrs li.selected').index(); if (currPanel != referencePanel) { /* Start Backgrounds - Fade Out and In */ $('#bgrs li').eq(referencePanel).fadeIn(1000).addClass('selected'); $('#bgrs li').eq(currPanel).fadeOut(1000).removeClass('selected'); } } function startShow() { var timerId = setInterval(function () { fadeBanners() }, 2 * 1000); } startShow(); 
 #outerWrapper { position: relative; width: 100%; height: 1000px; } #bgrs { position: absolute; top: 0; left: 0; width: 100%; height: 100%; list-style: none; margin: 0; padding: 0; } #bgrs li { position: absolute; top: 0; left: 0; background-size: cover!important; width: 100%; height: 100%; background-repeat: no-repeat; background-position: center; display: none; } #bgrs li.selected { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="outerWrapper"> <ul id="bgrs"> <li class="selected" style="background: url( 'http://i.imgur.com/EA7pkYA.jpg' );"></li> <li style="background: url( 'http://i.imgur.com/6U8rQOn.jpg' );"></li> <li style="background: url( 'http://i.imgur.com/MyXAIiX.jpg' );"></li> </ul> </div> 

For those who like JSFiddle better. 对于那些喜欢JSFiddle的来说更好。 The expect output is that the images should fade in and out together so the transition is smooth. 预期的输出是图像应一起淡入和淡出,以使过渡平滑。

The first slide gets its display: block from having the class selected so once it loses that it jumps to the display: none given to #bgrs li , as the fade effect occurs. 第一张幻灯片得到display: block selected该类,因此一旦失去该类,它会跳转到display: none随着淡入淡出效果, display: none#bgrs li给出任何#bgrs li This quick fix just adds an inline display property to the initial selected slide, so that the first slide remains displayed while it fades. 此快速修复方法只是向初始selected幻灯片添加了嵌入式display属性,以便第一张幻灯片在淡出时保持显示状态。 The rest were working properly because fadeIn gives them an inline display property which remains until they are faded out. 其余的都正常工作,因为fadeIn为它们提供了一个内联display属性,该属性一直保留到它们淡出为止。

 function fadeBanners() { var totalPanels = $('#bgrs li').length; var currIndex = $('#bgrs li.selected').index(); $('#bgrs li.selected').css("display", "block"); var nextPanel = currIndex + 1; if (nextPanel > totalPanels - 1) { nextPanel = 0; } setPanel(nextPanel); } function setPanel(referencePanel) { var currIndex = referencePanel; var currPanel = $('#bgrs li.selected').index(); if (currPanel != referencePanel) { /* Start Backgrounds - Fade Out and In */ $('#bgrs li').eq(referencePanel).fadeIn(1000).addClass('selected'); $('#bgrs li').eq(currPanel).fadeOut(1000).removeClass('selected'); } } function startShow() { var timerId = setInterval(function () { fadeBanners() }, 2 * 1000); } startShow(); 
 #outerWrapper { position: relative; width: 100%; height: 1000px; } #bgrs { position: absolute; top: 0; left: 0; width: 100%; height: 100%; list-style: none; margin: 0; padding: 0; } #bgrs li { position: absolute; top: 0; left: 0; background-size: cover!important; width: 100%; height: 100%; background-repeat: no-repeat; background-position: center; display: none; } #bgrs li.selected { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="outerWrapper"> <ul id="bgrs"> <li class="selected" style="background: url( 'http://i.imgur.com/EA7pkYA.jpg' );"></li> <li style="background: url( 'http://i.imgur.com/6U8rQOn.jpg' );"></li> <li style="background: url( 'http://i.imgur.com/MyXAIiX.jpg' );"></li> </ul> </div> 

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

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