3 回答
TA贡献1853条经验 获得超6个赞
幻灯片放映有效,您只是看不到图像,因为它们是背景图像,并且没有样式或内容,divs 具有0px宽度和高度。正确显示图像的设置:
.banner__item {
width:50px;
height:50px;
}
附带说明一下,如果您将这些图像作为实际img标签放在 HTML 中,则无需指定宽度和高度。这具有允许您使用该alt属性的额外好处,从而提高了可访问性。
此外,内联样式可能会让人头疼。通过类应用样式可以清理标记,从长远来看更易于维护。
与内联样式一起,内联 JS 也会引起头痛。您可以将内联onClick处理程序移动到事件侦听器中,以使标记更清晰。在代码片段中,我添加了监听器:
window.addEventListener("click", currentSlide);
为了解释点击侦听器,而不是传递我查找的索引:
function currentSlide(event) {
const dots = document.getElementsByClassName("dot");
const clickIndex = Array.from(dots).indexOf(event.target);
showSlides(clickIndex);
}
我还修改了 showSlides 逻辑以仅使用基于 0 的数组,而不是从传入的整数中减去 1。希望它仍然对您有用!:)
如果你想居中,这些天我通常会使用flexbox!
您可以在这个 codepen中看到它与您的使用方式一起工作,或者在下面的代码片段中进行建议的更改:
showSlides(0);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(event) {
const dots = document.getElementsByClassName("dot");
const clickIndex = Array.from(dots).indexOf(event.target);
showSlides(clickIndex);
}
function showSlides(slideIndex) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
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].style.display = "block";
dots[slideIndex].className += " active";
}
window.addEventListener("click", currentSlide);
.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;
float: left;
text-align: center;
}
.banner__list {
width: 100px;
}
.banner__item {
width: 100%;
margin: 0 auto;
text-align: center;
}
.banner__list {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
<div role="list" class="banner__list w-dyn-items">
<div role="listitem" class="banner__item w-dyn-item mySlides">
<img src="http://placehold.it/50/bada55" alt="green square" />
</div>
<div role="listitem" class="banner__item w-dyn-item mySlides">
<img src="http://placehold.it/50/990000" alt="red square" />
</div>
<div role="listitem" class="banner__item w-dyn-item mySlides">
<img src="http://placehold.it/50/000099" alt="blue square" />
</div>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
TA贡献1806条经验 获得超8个赞
也许您提到的那些其他类正在覆盖您的一些代码。
我刚刚向您的 div 添加了属性width并且效果很好。我还添加了一些边框颜色以显示图像确实在变化。heightbanner__list
看:
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
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";
}
<div role="list" class="banner__list w-dyn-items" style="width: 300px; height: 300px;">
<div style="border: 3px solid red; background-image:url(https://via.placeholder.com/300); width: 100%; height: 100%;" role="listitem" class="banner__item w-dyn-item mySlides">
</div>
<div style="border: 3px solid green; background-image:url(https://via.placeholder.com/300); width: 100%; height: 100%;" role="listitem" class="banner__item w-dyn-item mySlides">
</div>
<div style="border: 3px solid blue; background-image:url(https://via.placeholder.com/300); width: 100%; height: 100%;" role="listitem" class="banner__item w-dyn-item mySlides">
</div>
<span class="dot" style="cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
float:left;
text-align:center;" onclick="currentSlide(1)"></span>
<span class="dot" style="cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
float:left;
text-align:center;" onclick="currentSlide(2)"></span>
<span class="dot" style="cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
float:left;
text-align:center;" onclick="currentSlide(3)"></span>
</div>
TA贡献1833条经验 获得超4个赞
你可以试试 Twitter 的 Bootstrap。它有一个幻灯片组件。
将此添加到您的样式中:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
这是示例模板:
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="[IMAGE-1-SOURCE]" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="[IMAGE-2-SOURCE]" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="[IMAGE-3-SOURCE]" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
添加回答
举报