繁体   English   中英

Javascript-CSS幻灯片不显示默认图像

[英]Javascript - CSS Slideshow not displaying default image

我在当前的热情项目中实施的CSS幻灯片出现问题,在该幻灯片中,加载网页时没有默认图像显示。 导航/按点后,图像已成功显示,但是我似乎无法弄清楚会在页面加载时显示第一幅图像的小代码段(可能是Javascript函数所缺少的)。 我试图将“活动”类简单地添加到所需的图像中,但这只会使“点”按钮显示为活动状态,而没有显示相应的图像。 感谢任何协助

这是代码:

 var slideIndex = 1; function plusSlides(n) { 'use strict'; showSlides(slideIndex += n); } function currentSlide(n) { 'use strict'; showSlides(slideIndex = n); } function showSlides(n) { 'use strict'; var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1; } if (n < 1) { slideIndex = slides.length; } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; } 
 * { box-sizing: border-box } body { font-family: Verdana, sans-serif; margin: 0 } .mySlides { display: none } /* Slideshow container */ .slideshow-container { max-width: 400px; position: relative; margin: auto; } /* Next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add see-through background */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* Caption text */ .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } /* Number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* */ .transition { cursor: pointer; height: 13px; width: 13px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } /* */ .active, .transition:hover { background-color: #222; } /* Fading animation */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } /* */ @-webkit-keyframes fade { from { opacity: .4 } to { opacity: 1 } } /* */ @keyframes fade { from { opacity: .4 } to { opacity: 1 } } /* On smaller screens, decrease text size */ @media only screen and (max-width: 300px) { .prev, .next, .text { font-size: 11px } } 
 <div class="homePage"> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 2</div> <img src="graphics/logo.jpg" style="width:100%"> <div class="text"></div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 2</div> <img src="graphics/1.jpg" style="width:100%"> <div class="text"></div> </div> <a class="prev" onclick="plusSlides(-1)">&#10094;</a> <a class="next" onclick="plusSlides(1)">&#10095;</a> </div> <br> <div style="text-align:center"> <span class="transition" onclick="currentSlide(1)"></span> <span class="transition" onclick="currentSlide(2)"></span> <br> </div> 

您可以为滑块中的第一张图像添加另一个类

<div class="mySlides fade block ">
 <div class="numbertext">1 / 2</div>
  <img src="#" style="width:100%">
  <div class="text"></div>
</div>

然后给它一个display:block

.block {
 display:block;
}

范例: https : //jsfiddle.net/1ueun1L1/2/

暂无
暂无

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

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