Javascript J-query循环问题
我有一个简单的jquery延迟映像循环 我已经检查了我的语法,我不知道为什么它拒绝工作 所有的css和代码似乎都是正确的 下面是函数Javascript J-query循环问题,javascript,jquery,html,css,loops,Javascript,Jquery,Html,Css,Loops,我有一个简单的jquery延迟映像循环 我已经检查了我的语法,我不知道为什么它拒绝工作 所有的css和代码似乎都是正确的 下面是函数 $(document).ready(function () { $.doTimeout('loop', 500, function () { //changes the background of img2 to 'i' in 'sequence' $(".img2").css('background', 'url(' + s
$(document).ready(function () {
$.doTimeout('loop', 500, function () {
//changes the background of img2 to 'i' in 'sequence'
$(".img2").css('background', 'url(' + sequence[i] + ')');
//increments 'i' for the next image
i++;
//toggles the class
$("img1").toggleClass("img2");
//changes the the background of img1 to 'i' in 'sequence' ready for when class is toggled again
$(".img1").css('background', 'url(' + sequence[i] + ')');
//toggles the class
$("img2").toggleClass("img1");
//increments 'i; for the next change
i++;
//restarts the loop if i is equal to the sequence length
if (i === sequence.length) {
i = 0;
}
});
toggleclass必须存在,因为我打算稍后添加css3转换,这将使每个图像淡入淡出
有人能帮忙吗 在回调中返回
True
,以便循环再次发生,文档中提到了它
$(document).ready(function(){
// Start a polling loop with an id of 'loop' and a counter.
$.doTimeout('loop', 500, function(){
$('.img1').css('background', 'url(' + sequence[i] + ')');
i++;
return true;
});
});
另一个问题是,循环将中断,因为数组将越界,而不是使用array.shift
从数组中取出第一个元素,并将其推回数组末尾。有了这些元素,数组中的元素将在一个周期内运行,您不必维护计数器并重置它等等
正确的做法是:-
抱歉,链接已断开,我现在已修复。请解释脚本中的.shift和.push在做什么?更新了我的答案。。这有意义吗?@CaedanLavender文档对每个人都是免费的:@bažmegakapa谢谢@@CaedanLavender Hope,你现在得到了……@凯丹兰德请考虑标记为答案,如果这解决了你的问题,并得到你的问题回答…
var sequence = ["http://goo.gl/u4QHd",
"http://goo.gl/s5tmq",
"http://goo.gl/MMsQ6",
"http://goo.gl/dtEYw"];
$(document).ready(function(){
// Start a polling loop with an id of 'loop' and a counter.
$.doTimeout('loop', 500, function(){
var url = sequence.shift(); //Remove the first element out of the array
sequence.push(url); //push it to the end of the array. probably you can put it after the next statement.
$('#img1').css('background', 'url(' + sequence[0] + ')');//get the first item from the array. this will always be the next one to previous cycle.
return true;
});
});