如何将图片轮播组件放入div容器?HTML新手技术求助
修复你的轮播组件问题
Hey there! Let's fix that slideshow you're trying to get working inside your .rcorners1 container. I noticed a couple of key issues in your code that are keeping it from functioning properly:
- Your HTML code gets truncated at the end (cuts off mid-navbar code), which breaks the page's structure and prevents the browser from parsing everything correctly.
- You're missing the JavaScript code required to make the slideshow switch images—this is the core functionality that makes the carousel work.
- The slideshow wasn't properly placed as the first content inside your
.rcorners1container.
Here's the full, corrected version of your code with all these issues fixed:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style> body { background-color: #252525; margin: 0; font-family: ultra; } .navbar { overflow: hidden; background-color: #333; font-family: ultra; } .navbar a { float: left; font-size: 16px; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } .dropdown { float: left; overflow: hidden; } .dropdown .dropbtn { font-size: 16px; border: none; outline: none; color: white; padding: 14px 16px; background-color: inherit; font-family: inherit; margin: 0; } .navbar a:hover, .dropdown:hover .dropbtn { background-color: #2E86C1; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } .dropdown-content a { float: none; color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #2E86C1; } .dropdown:hover .dropdown-content { display: block; } .navbar .search-container { float: right; } .navbar input[type=text] { padding: 6px; margin-top: 8px; font-size: 17px; border: none; } .navbar .search-container button { float: right; padding: 6px 10px; margin-top: 8px; margin-right: 16px; background: #ddd; font-size: 17px; border: none; cursor: pointer; } .navbar .search-container button:hover { background: #ccc; } @media screen and (max-width: 600px) { .navbar .search-container { float: none; } .navbar a, .navbar input[type=text], .navbar .search-container button { float: none; display: block; text-align: left; width: 100%; margin: 0; padding: 14px; } .navbar input[type=text] { border: 1px solid #ccc; } } .header { background-color: #252525; padding: 5px; text-align: center; color: blue; margin: 0; } h1 { color: #2E86C1; font-size: 60px; font-family: ultra; } .header img{ float:left; } .rcorners1 { margin: 50px auto; padding-top: 100px; border-radius: 25px; background: #fffafa; padding: 20px; width: 90%; } .text2{ font-size:25px } /* Slideshow styles */ * {box-sizing: border-box} .mySlides {display: none} img {vertical-align: middle;} /* Slideshow container */ .slideshow-container { max-width: 1000px; position: relative; margin: 0 auto 30px; /* Added bottom margin to space from other content */ } /* 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; user-select: none; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add a black background color with a little bit see-through */ .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; } /* The dots/bullets/indicators */ .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; } /* Fading animation */ .fade { animation-name: fade; animation-duration: 1.5s; } @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} } </style> </head> <body> <div class="header"> <img src="Untitled-2.png" alt="M" width="80" height="80" /> <h1>World Vector Control</h1> </div> <div class="navbar"> <a href="#home">Home</a> <a href="#news">About</a> <div class="dropdown"> <button class="dropbtn"> Introduction <i class="fa fa-caret-down"></i> </button> <div class="dropdown-content"> <a href="#">The Problem</a> <a href="#">The Solution</a> </div> </div> <a href="#FAQ">FAQ</a> <div class="search-container"> <form action="/action_page.php"> <input type="text" placeholder="Search.." name="search"> <button type="submit"><i class="fa fa-search"></i></button> </form> </div> </div> <div class="rcorners1"> <!-- Slideshow placed as first content in the container --> <div class="slideshow-container"> <div class="mySlides fade"> <div class="numbertext">1 / 3</div> <img src="img1.jpg" style="width:100%"> <div class="text">Caption Text</div> </div> <div class="mySlides fade"> <div class="numbertext">2 / 3</div> <img src="img2.jpg" style="width:100%"> <div class="text">Caption Two</div> </div> <div class="mySlides fade"> <div class="numbertext">3 / 3</div> <img src="img3.jpg" style="width:100%"> <div class="text">Caption Three</div> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <br> <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> <!-- Add your other content for .rcorners1 here --> <p class="text2">Your other content goes here!</p> </div> <script> let slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } 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"; // Auto-advance slides every 4 seconds (adjust value as needed) setTimeout(showSlides, 4000); } </script> </body> </html>
关键修复说明:
- 轮播位置调整:我把整个轮播组件(容器+导航点)移到了
.rcorners1容器的最顶部,确保它是该容器内的首个内容。 - 修复截断的HTML:补全了你原代码中被截断的导航栏搜索容器部分,保证页面结构完整。
- 添加轮播JavaScript:包含了处理前后按钮、导航点切换和自动轮播的所有必要JS函数(你可以修改
setTimeout里的4000值来调整轮播速度)。 - 微调CSS样式:给轮播容器添加了底部外边距,避免和下方内容挤在一起,同时修复了搜索按钮缺失的背景色问题。
只需要把img1.jpg、img2.jpg、img3.jpg替换成你自己的图片路径,轮播就能在容器里正常工作了!
内容的提问来源于stack exchange,提问作者Drone Flying Toronto
相关产品推荐
相关产品推荐

