html:<div id="box">
<ul>
<li class="color1 active"></li>
<li class="color2"></li>
<li class="color3"></li>
<li class="color4"></li>
</ul>
</div>
css:*{
padding:0;
margin:0;
}
#box{
width: 800px;
height: 400px;
margin: 100px auto;
}
ul,li{
width: 100%;
height: 100%;
}
li{
list-style: none;
display: none;
}
.color1{
background: #F00;
}
.color2{
background: #FF0;
}
.color3{
background: #0F0;
}
.color4{
background: #DDD;
}
#box li.active{
display: block;
}
js:<script type="text/javascript">
var $li = $("#box ul li");
var timer = null;
console.log($li);
function autoPlay(){
var index_ = 0;
timer = setInterval(function(){
$li.eq(index_).addClass('active').siblings('li').removeClass('active');
index_ ++;
if (index_ >= $li.length) {
index_ = 0;
}
console.log(index_);
},2000);
}
autoPlay();
</script>
这个只是最简单的一种方法,你可以参考一下,不懂的欢迎提问 |