Slider只循环一次
jquery, Slider only goes round once
我有这个,这是一个图像滑动条,它意味着在它到达结束和返回时继续旋转:
$(function () {
var sliderUL = $('div.headslide').children('ul'),
imgs = sliderUL.find('img'),
imgWidth = imgs[0].width,
imgsLenth = imgs.length,
current = 1,
totalImgsWidth = imgsLenth * imgWidth,
direction = 'next',
loc = imgWidth,
interval = setInterval(swapBKgnd, 9000);
function swapBKgnd() {
(direction === 'next') ? ++current : --current;
if (current === 0) {
current = imgsLenth;
loc = totalImgsWidth - imgWidth;
direction = 'next';
} else if (current - 1 === imgsLenth) {
current = 1;
loc = 0;
}
transition(sliderUL, loc, direction);
};
function transition(container, loc, direction) {
var unit;
if (direction && loc !== 0 ) {
unit = (direction === 'next') ? '-=' : '+=';
}
container.animate({
'margin-left': unit ? (unit + loc) : loc,
});
}
});
它意味着继续,但一旦它到达最后,回到第一个…它停止。我该如何解决这个问题?
当您到达图像的末尾时,您设置loc = 0
,然后设置current = 1
。然而,在下一次迭代中,loc
仍然是0
,所以它不移动。您需要添加一个else
:
if (current === 0) {
current = imgsLenth;
loc = totalImgsWidth - imgWidth;
direction = 'next';
} else if (current - 1 === imgsLenth) {
current = 1;
loc = 0;
} else {
loc = imgWidth;
}
演示:http://jsfiddle.net/jtbowden/5W8Av/
相关文章:
- Javascript返回值只在循环中返回一次
- For循环在Jquery中只运行一次
- 可能只在for循环内部运行一次代码块
- mongoose.js Model.remove在循环中只能工作一次
- 循环遍历数组并仅获取一次相同的值
- Jquery 循环一次或在数据数组中显示一次数据
- 一次循环遍历一组
- 如何在if循环中只执行一次
- AJAX在数组循环中调用,只在上一次完成后调用next
- 正在加载“"循环,每3秒更改一次文本
- 我怎么能有一个实时计时器,在3秒的循环内每秒钟更新一次
- 在Javascript中每五秒钟循环一次
- 如何一次运行一次循环javascript
- 使用胡须.js一次循环访问一个测验项目
- 递归 JS 函数每秒错过一次循环
- 使用 javascript 一次循环播放 4 个以上的声音
- 为什么我的循环只有一次循环,即使有3个项目
- 在下一次循环迭代之前等待promise结果
- Jquery 一次循环和绑定 10 条记录
- JavaScript在同一循环中每x次迭代一次循环