console
window.onload=function(){
var div2=document.querySelectorAll("#div2");
var ul1=document.getElementById("ul1");
ul1.style.width=220*div2.length+"px";
var Alla=document.getElementsByTagName("a");
var div3=document.getElementById("div3");
var div1=document.getElementById("div1");
div3.style.left=(div1.offsetWidth-div3.offsetWidth)/2+"px";
Alla[0].style.backgroundColor="yellowgreen"
for(var i=0;i<Alla.length;i++)
{
Alla[i].sy=i;
Alla[i].onclick=function(){
for(var i=0;i<Alla.length;i++)
Alla[i].style.backgroundColor="";
move(ul1,"left",10,-220*this.sy);
};
}
};
function move(obj,attr,speed,weizhi){
clearInterval(obj.timer);
var oldvalue=parseInt(getComputedStyle(obj,null)[attr]);
if(oldvalue>weizhi)
speed=-speed;
obj.timer=setInterval(function(){
var oldvalue=parseInt(getComputedStyle(obj,null)[attr]);
var newvalue=oldvalue+speed;
if(newvalue>weizhi&&speed>0||newvalue<weizhi&&speed<0)
newvalue=weizhi;
obj.style[attr]=newvalue+"px";
if(newvalue==weizhi)
clearInterval(obj.timer);
},30)
}
<div id="div1">
<ul id="ul1">
<li><div id="div2" style="background-color:red"></div></li>
<li><div id="div2" style="background-color:green"></div></li>
<li><div id="div2" style="background-color:yellow"></div></li>
<li><div id="div2" style="background-color:blue"></div></li>
</ul>
<div id="div3">
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
</div>
</div>
*{
margin:0;
padding:0;
}
#div1{
width:220px;
height:150px;
padding:10px 0 ;
background-color:greenyellow;
margin: 50px auto;
position:relative;
overflow: hidden;
}
#div2{
width:200px;
height:150px;
}
#ul1{
position:absolute;
width:2000px;
left:0px;
}
#ul1 li{
margin:0 10px;
list-style:none;
float:left;
}
#div3{
position:absolute;
bottom:15px;
}
#div3 a{
float:left;
width:15px;
height:15px;
background-color:black;
margin:0 2px;
opacity:0.5;
}
#div3 a:hover{
background-color:yellowgreen;
opacity:0.5;
}