无限循环一组图像
infinite loop a set of images
当它到达最后一个图像时,效果不太好,我必须单击两次才能返回到第一个图像。
var i = 1;
$('.rightArrow').click(function () {
if(i != 4) {
$('body').css(
'background',
'linear-gradient(rgba(0, 0, 0, 0.30),rgba(0, 0, 0, 0.30)), url("'+
customerBgs[i]+'") no-repeat center center fixed'
);
i++;
}else{
i = 0;
$('body').css(
'background',
'linear-gradient(rgba(0, 0, 0, 0.30),rgba(0, 0, 0, 0.30)), url("'+
customerBgs[i]+'") no-repeat center center fixed'
);
}
});
为什么不做更简单的
var i = 0;
var customerBgs = ['http://placehold.it/350x150', 'http://placehold.it/500x100', 'http://placehold.it/100x510', 'http://placehold.it/300x500'];
$('.rightArrow').click(function() {
$('body').css(
'background',
'linear-gradient(rgba(0, 0, 0, 0.30),rgba(0, 0, 0, 0.30)), url("' + customerBgs[i] + '") no-repeat center center fixed');
i = ++i % customerBgs.length;
});
下方的演示
var i = 0;
var customerBgs = ['http://placehold.it/350x150', 'http://placehold.it/500x100', 'http://placehold.it/100x510', 'http://placehold.it/300x500'];
$('.rightArrow').click(function () {
$('body').css(
'background',
'linear-gradient(rgba(0, 0, 0, 0.30),rgba(0, 0, 0, 0.30)), url("' + customerBgs[i] + '") no-repeat center center fixed');
i = ++i % customerBgs.length;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input class="rightArrow" type="button" value="right" />
我假设customerBgs从0 开始
var i = 0;
$('.rightArrow').click(function(){
i++;
if (i > customerBgs.length - 1) {
i = 0;
}
$('body').css(
'background',
'linear-gradient(rgba(0, 0, 0, 0.30),rgba(0, 0, 0, 0.30)), url("'+
customerBgs[i]+'") no-repeat center center fixed'
);
});
您没有在else块中递增i,因此它重置为图像0,然后在转到图像1之前再次设置背景。然而,您可能会将递增从if情况中提取出来,然后在设置背景之前检查是否需要换行为0。这将使您不必在两个块中重复背景设置。
相关文章:
- fluxxor向一个flux实例添加一组以上的操作
- 无限循环一组图像
- 图像高度只会从一大组图像中检索一次
- 学习JavaScript:有没有办法在不使用个人ID的情况下更改一组图像的图像文件
- 在 JavaScript 中,只有一张图像出现在 3 张图像的数组中
- 在我的网页中显示一组图像
- 使用 jQuery 将文本定位在一组图像上
- CSS:当 image1 动画不合适时,一组图像会移动,而它们应该保留在原处
- 在一组图像中只用 javascript 更改 src
- 使用$(this)添加不同<p>'s在一组图像的顶部
- 每次重新加载我的网站时,从阵列中随机选择一组6个不同的图像
- 检索数组和输出结果以生成一组html图像
- 迭代地显示一组图像,每次只显示一个
- 使用D3js循环遍历一组图像
- 如何在JavaScript中保存一组特定的类图像
- 淡入淡出循环jquery中的一组图像
- 仅当选择了一组特定单选按钮中的至少一个时才显示图像
- 在javascript的提示框中插入一组图像
- 如何根据一组控件和目标坐标将图像转换为不同的大小
- 三个Js如何在一组立方体的立方体的所有侧面映射不同的随机图像