幻灯片组件图片无法显示问题求助(附HTML/CSS代码)
解决幻灯片图片不显示的问题
别担心,马上帮你搞定这个作业问题!你的核心问题是缺少控制幻灯片切换的JavaScript代码——W3School的幻灯片示例是需要JS配合的,光靠HTML和CSS没法自动显示第一张图片,也没法切换。
问题原因分析
你把所有.mySlides都设为display: none,但没有JS来控制哪一张该显示,所以所有图片都被隐藏了;删掉display: none后所有图片都出来,自然布局混乱。另外你的.slideshow-container没加position: relative,导致前后切换按钮(.prev/.next)会跑到页面最边缘,这也是布局问题的一个小原因。
完整解决方案
按照下面的步骤修改你的代码:
- 添加JavaScript代码:在HTML的
</body>标签前加入控制幻灯片的JS; - 调整CSS:给
.slideshow-container加上相对定位,让按钮位置正常; - 检查图片路径:确保第三张图片
img3.jpg的路径和前两张一致(比如应该是img/img3.jpg?)
修改后的完整代码:
<!DOCTYPE html> <html> <head> <style> .slide { width: 30%; margin: auto; } .slideshow-container { position: relative; /* 新增:让按钮相对于容器定位 */ max-width: 100%; } .mySlides { display: none; } .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -22px; padding: 16px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; /* 防止选中按钮文字 */ } .next { right: 0; border-radius: 3px 0 0 3px; } .prev:hover, .next:hover { background-color: #ec7979; } .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } .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} } </style> </head> <body> <section id="screens" class="odd"> <h1 id="clear">App Screens</h1> <div class="slide"> <!-- Slideshow container --> <div class="slideshow-container"> <!-- Full-width images with number and caption text --> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="img/store.png" style="width:100%"> <div class="text">store page</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="img/home.png" style="width:100%"> <div class="text">home page</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="img/img3.jpg" style="width:100%"> <!-- 这里建议统一路径 --> <div class="text">Caption Three</div> </div> <!-- Next and previous buttons --> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <br> <!-- The dots/circles --> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div> </div> <p>'My wedding' is an online platform where people share<br> ideas and experiences about their wedding.</p> </section> <script> let slideIndex = 1; showSlides(slideIndex); // Next/previous controls function plusSlides(n) { showSlides(slideIndex += n); } // Thumbnail image controls function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { let i; let slides = document.getElementsByClassName("mySlides"); let 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"; } </script> </body> </html>
关键说明
- JS代码里的
showSlides(slideIndex)会在页面加载时默认显示第一张幻灯片; .slideshow-container加了position: relative后,前后按钮会相对于容器定位,不会跑到页面边缘;- 记得检查第三张图片的路径,确保它和前两张一样放在
img文件夹里,不然也会显示不出来哦!
这样修改后,你的幻灯片应该就能正常显示和切换了,布局也不会乱啦,赶紧试试吧!
内容的提问来源于stack exchange,提问作者Larissa Francis
相关产品推荐
相关产品推荐

