我用cloneNode克隆元素,也加上了事件,但是每个元素都是第一个有事件,
然后第二个就没有了,好纠结。大神帮看看。
http://jsbin.com/ipirut/1/edit
<script type="text/javascript">
window.onload=function(){
var moveTo=document.getElementById('moveTo');
var aDrogBox=document.getElementById('myDrog');
var oul=moveTo.getElementsByTagName('ul')[0];
var aPre=document.getElementById('pre');
var aNext=document.getElementById('next');
var aDrog=aDrogBox.getElementsByTagName('li');
var aLi=moveTo.getElementsByTagName('li');
var itemMove=document.getElementById('item-move');
var aSpan=getClass(moveTo,'jSer');
var oClos=getClass(aDrogBox,'clos');
var oneSize=aLi[0].offsetWidth+10;
var iNum=1;
var i=0;
var bShop=true;
var timer=null;
var tis=null;
var aMark=document.createElement('div');
var timer=null;
document.body.appendChild(aMark);
aMark.id='allMark';
//计算ul的宽度
function ulWidth(){
oul.style.width=oneSize*aLi.length+'px';
}
//页面加载完执行ul的宽度
ulWidth();
//封装运动内部循环
function moves(iLiTarget,reiTarget){
ulWidth();
for(i=0;i<iNum;i++){
startMove(oul,{left:iLiTarget},function(){
for(i=0;i<iNum;i++){
oul.removeChild(reiTarget);
oul.style.left=0;
}
bShop=true;
})
}
}
//向左
function pre(){
if(bShop){
bShop=false;
for(i=0;i<iNum;i++){
var oLi=aLi[i].cloneNode(true);
oul.appendChild(oLi);
moves(-iNum*oneSize,aLi[i]);
}
}
}
//向右
function next(){
if(bShop){
bShop=false;
for(i=0;i<iNum;i++){
var oLi=aLi[aLi.length-1].cloneNode(true);
oul.insertBefore(oLi,aLi[i]);
oul.style.left=-iNum*oneSize+'px';
moves(0,aLi[aLi.length-1]);
}
}
}
timer=setInterval(pre,2000);
//点击鼠标向左移动
aPre.onclick=function(){
pre();
}
//点击鼠标向右移动
aNext.onclick=function(){
clearInterval(timer);
next();
}
//鼠标离开再次执行定时器
itemMove.onmouseout=function(ev){
var oEvent=ev||event;
clearInterval(timer);
timer=setInterval(pre,2000);
oEvent.cancelBubble=true;
}
//1s自动轮播
for(i=0;i<aSpan.length;i++){
aSpan[i].style.bottom='-20px';
}
for(i=0;i<aLi.length;i++){
aLi[i].index=i;
aLi[i].tis=null;
aLi[i].onmouseover=function(){
aSpan[this.index].timer=null;
startMove(aSpan[this.index],{bottom:0})
clearInterval(tis);
}
aLi[i].onmouseout=function(){
startMove(aSpan[this.index],{bottom:-20})
}
aLi[i].onclick=function(){
for(i=0;i<aDrog.length;i++){
aDrog[i].style.display='none';