我正在尝试为项目设置滑块。我有一个对象数组,每个对象都包含一个图像和一个图例。每次用户单击下一个或上一个按钮时,它应显示数组的下一个或上一个对象。问题在于它显示的是同一张图片,而不是下一张,第三张,第五张等。
var index = 0;
var images = [
{image: 'images/1.jpg', legend: 'Street Art'},
{image: 'images/2.jpg', legend: 'Fast Lane'},
{image: 'images/3.jpg', legend: 'Colorful Building'},
{image: 'images/4.jpg', legend: 'Skyscrapers'},
{image: 'images/5.jpg', legend: 'City by night'},
{image: 'images/6.jpg', legend: 'Tour Eiffel la nuit'}
];
var previous = document.getElementById('slider-previous');
var next = document.getElementById('slider-next');
next.addEventListener('click', function() {
img.src= images[index++].image;
figcaption.textContent = images[index++].legend;
});
previous.addEventListener('click', function() {
img.src= images[index--].image;
figcaption.textContent = images[index--].legend;
});
答案 0 :(得分:1)
效果更好,谢谢菲尔
next.addEventListener('click', function() {
++index;
img.src= images[index].image;
figcaption.textContent = images[index].legend;
});
previous.addEventListener('click', function() {
--index;
img.src= images[index].image;
figcaption.textContent = images[index].legend;
});