Jquery循环数组
这个问题已经被问了很多次,但是作为一个初学者,我很难将其他人的脚本应用到我自己指定的类和变量中 我想知道的是: 我在html中有一个简单的区域,如下所示:Jquery循环数组,jquery,arrays,loops,Jquery,Arrays,Loops,这个问题已经被问了很多次,但是作为一个初学者,我很难将其他人的脚本应用到我自己指定的类和变量中 我想知道的是: 我在html中有一个简单的区域,如下所示: <h1>I like: <span class ="hobbies">array items go here</span></h1> 我希望每个项目都能自己出现,比如说3秒钟左右,然后淡出并被阵列中的下一个项目所取代。我还想让它在完成所有选项后循环。我怎么能做这样的事 我还有: $('.hob
<h1>I like: <span class ="hobbies">array items go here</span></h1>
我希望每个项目都能自己出现,比如说3秒钟左右,然后淡出并被阵列中的下一个项目所取代。我还想让它在完成所有选项后循环。我怎么能做这样的事
我还有:
$('.hobbies').html(hobbies);
提前感谢您通读 基本上,您必须使用
setInterval
在特定的时间延迟重复该功能
在数组中保存当前位置的索引,并在每次迭代时增加索引。确保在索引达到与数组长度相同的值时重置索引
// Always a good idea to cache selectors you're gonna be using alot.
var hobbyContainer = $('.hobby');
hobbyContainer.text(hobbies[0]);
var index = 1;
setInterval(function(){
hobbyContainer.fadeOut('fast',function(){
hobbyContainer.text(hobbies[index]);
index++;
if (index >= hobbies.length){
index = 0;
}
}).fadeIn('fast');
},3000);
您可能必须播放间隔的持续时间,以便为淡入淡出的动画留出时间。或者您可以像我在示例中所做的那样加快淡入淡出动画
基本上,您必须使用
setInterval
在特定的时间延迟重复该功能
在数组中保存当前位置的索引,并在每次迭代时增加索引。确保在索引达到与数组长度相同的值时重置索引
// Always a good idea to cache selectors you're gonna be using alot.
var hobbyContainer = $('.hobby');
hobbyContainer.text(hobbies[0]);
var index = 1;
setInterval(function(){
hobbyContainer.fadeOut('fast',function(){
hobbyContainer.text(hobbies[index]);
index++;
if (index >= hobbies.length){
index = 0;
}
}).fadeIn('fast');
},3000);
您可能必须播放间隔的持续时间,以便为淡入淡出的动画留出时间。或者您可以像我在示例中所做的那样加快淡入淡出动画
试试这样的东西-
var hobbies = //"yourarray";
var count = 0;
setInterval(function() {
scrollText(hobbies[count]);
count++;
if (count == hobbies.length ) {
count = 0;
}
}, 3000);
function scrollText(text) {
$(".hobbies").animate({opacity:0},function(){
$(this).html(text).animate({opacity:1});
});
}
试试这样的方法-
var hobbies = //"yourarray";
var count = 0;
setInterval(function() {
scrollText(hobbies[count]);
count++;
if (count == hobbies.length ) {
count = 0;
}
}, 3000);
function scrollText(text) {
$(".hobbies").animate({opacity:0},function(){
$(this).html(text).animate({opacity:1});
});
}
我想应该是index>=cabiods.length或index==cabiods.length这正是我想要的!非常感谢你!我想应该是index>=cabiods.length或index==cabiods.length这正是我要找的!非常感谢你!