可以隐藏所有与CSS的图像:
img {
width: 100px;
height: 100px;
display: none;
}
获取处理的图像:
var slider = document.querySelector('#img-slider');
var images = slider.getElementsByTagName('img');
设置一个指数为0:
var index = 0;
再展第一张图片:
images[index].style.display = 'block';
将按钮添加到您的HTML:
<div>
<button id="next">Next</button>
</div>
与脚本得到它,并添加一个单击处理程序:
var button = document.querySelector('#next');
button.addEventListener('click', function() {
for (var ix = 0; ix < images.length; ix++) {
images[ix].style.display = 'none';
}
index++;
if (index >= images.length) index = 0;
images[index].style.display = 'block';
});
的点击处理程序遍历图像和隐藏它们,增加了指数,检查以确保索引没有超出图像的数量(如果是,则将其重置为0),然后显示下一张图像。
全部放在一起:
https://jsfiddle.net/subterrane/osszqoLe/
_ “定期JS和jQuery插件不工作” _?为什么? – Rayon