【发布时间】:2019-06-25 08:36:46
【问题描述】:
//get the object
let slideshowContainer = document.getElementById('slideshow-container');
//get the buttons
let next = document.querySelector('.next');
let prev = document.querySelector('.prev');
//create an index
var slideIndex = 0;
function showSlides(n) {
const slides = document.getElementsByClassName('product');
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = 'none';
}
if (n < 0) {
slideIndex = slides.length
}
if (n > slides.length) {
slideIndex = 0
}
slides[slideIndex].style.display = 'block';
slideIndex = n;
}
function incrementSlides(n) {
showSlides(slideIndex += n)
}
//add event listeners
next.addEventListener('click', function () {
incrementSlides(1);
})
prev.addEventListener('click', function () {
incrementSlides(-1);
})
showSlides(slideIndex);
#section-one .categories {
height: 80px;
background: rgba(0, 0, 0, 0.9);
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
#section-one .categories li {
background: -webkit-gradient(linear, left top, right top, from(#0d0d0d), to(#202020));
background: linear-gradient(to right, #0d0d0d, #202020);
height: inherit;
width: 12.5%;
border-left: 1px solid black;
-webkit-transition: all ease-in 0.3s;
transition: all ease-in 0.3s;
}
#section-one .categories li:hover {
background: green;
}
#section-one .categories li a {
display: inline-block;
font-size: 0.95rem;
height: inherit;
width: 100%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
#section-one .slideshow-container {
height: 1000px;
margin: auto;
position: relative;
background: grey;
}
#section-one .slideshow-container .prev,
#section-one .slideshow-container .next {
top: 50%;
background: blue;
font-size: 18px;
border-radius: 0 3px 3px 0;
width: auto;
position: absolute;
padding: 16px;
}
#section-one .slideshow-container .next {
right: 0;
border-radius: 3px 0 0 3px;
}
#section-one .slideshow-container .prev:hover,
#section-one .slideshow-container .next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
<!-- Section-one -->
<section id="section-one">
<ul class="categories">
<li><a href="#">HEADPHONES</a></li>
<li><a href="#">EARPHONES</a></li>
<li><a href="#">BLUETOOTH</a></li>
<li><a href="#">WATERPROOF</a></li>
<li><a href="#">SPORTS</a></li>
<li><a href="#">METALLIC</a></li>
<li><a href="#">WOODEN/BAMBOO</a></li>
<li><a href="#">EARMUFF</a></li>
</ul>
<div id="slideshow-container">
<div class="product">
<p class="description"></p>
<div class="img">
<img
src="https://i.pinimg.com/originals/5a/e5/8f/5ae58f5036997cfd4636917403c3c951.jpg"
alt="image1"
style="width:100%"
/>
</div>
<a href="#">WIEW MORE</a>
</div>
<div class="product">
<p class="description"></p>
<div class="img">
<img
src="https://images.unsplash.com/photo-1500382017468-9049fed747ef?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80"
alt="image2"
style="width:100%"
/>
</div>
<a href="#">WIEW MORE</a>
</div>
<div class="product">
<p class="description"></p>
<div class="img">
<img
src="https://cdn.pixabay.com/photo/2017/02/22/20/02/landscape-2090495_960_720.jpg"
alt="image3"
style="width:100%"
/>
</div>
<a href="#">WIEW MORE</a>
</div>
<a href="javascript:void(0);" class="prev"></a>
<a href="javascript:void(0);" class="next"></a>
</div>
</section>
我正在尝试构建一个带有图像滑块的页面。 我观看了一些 youtube 视频,我合并了代码并试图让某些东西起作用,但我遇到了一个错误。当我更改下一张或上一张图片时,我得到一个空白页,我不知道为什么。 我将创建一个代码 sn-p 向您展示。我不仅要解决问题,还要解释,拜托! 干杯!!!
【问题讨论】:
-
我已经试用了您的代码,它似乎按预期工作。唯一的区别是我在
anext/prev 添加了文本以便能够点击它们:jsfiddle.net/2L4gbpav。另外,我会隐藏下一个/上一个(基于当前正在查看的图像),这样你就可以避免显示一个空白的白色 div。 -
显示后,第三张图片显示为空白页,然后转到:0 ,1,2,然后又是 0 ,1,2
-
我刚刚编辑了 jsFiddle 来解决这个问题并显示/隐藏 prev/next 按钮。我的错,我没有注意到你想在到达结尾后从幻灯片 0 重新开始的事实。在这种情况下,@ihay 的答案是正确的。
-
好的,现在完成。您可以签出 jsFiddle
-
同样的事情,所有图像显示后显示空白,我想重新启动计数器
标签: javascript function slider carousel