【发布时间】:2015-08-11 11:24:42
【问题描述】:
我正在制作带有两个左右按钮的幻灯片,当单击按钮时,图像src 会发生变化并出现新图像。图像的位置存储在一个数组中,++ 运算符用于移动下一张照片,-- 用于上一张有时它不起作用我必须双击按钮才能工作。为什么会这样?
HTML
<div class="row">
<div class="col-xs-1">
<a href="" id="left"><img src="images/left.png" width="80px" height="80px"></a>
</div>
<div class="col-xs-6">
<img src="images/1.jpg" id="image">
</div>
<div class="col-xs-1">
<a href="" id="right"><img src="images/right.png" width="80px" height="80px"></a>
</div>
</div>
jQuery
var count = 1;
var imagesLocation = ['images/1.jpg',
'images/2.jpg',
'images/3.jpg',
'images/4.jpg',
'images/5.jpg'];
$('#right').on('click',function(e){
e.preventDefault();
if(count < 6){
$('#image').attr('src',imagesLocation[count]);
count++;
}
});
$('#left').on('click',function(e){
e.preventDefault();
count--;
$('#image').attr('src',imagesLocation[count]);
});
【问题讨论】:
-
javascript 数组索引从 0 开始
标签: javascript jquery increment