為什么要在animate()函數(shù)中加if(offset==0)這個(gè)判斷條件呢?
function animate(offset){
? ? animated = true;
? ? if(offset == 0){
? ? return;
? ? }
? ?
? ? var left = parseInt(list.style.left) + offset;
? ? var time = 300;? //位移總時(shí)間
? ? var interval = 10;? //位移間隔時(shí)間
? ? var speed = offset/(time/interval);? //每次位移量
? ? function go(){
? ? if((speed < 0 && parseInt(list.style.left) > left) || (speed > 0 && parseInt(list.style.left) < left)){
? ? list.style.left = parseInt(list.style.left) + speed + 'px';
? ? setTimeout(go,interval);
? ? }else{
? ? list.style.left = left + 'px';
? ? if(left > -600){
? ? list.style.left = -3000 + 'px';
? ? }
? ? if(left < -3000){
? ? list.style.left = -600 + 'px';
? ? }
? ? animated = false;
? ? }
? ? }
? ? go();
? ? }
2019-11-10
原因在這里
? var myIndex = parseInt(this.getAttribute('index'));
? ? ? ? ? ? ? ? ? ? var offset = -600 * (myIndex - index);
? ? ? ? ? ? ? ? ? ? animate(offset);
假如offset =0,可以推出myIndex =index;
當(dāng)myIndex =index時(shí),其實(shí)就是當(dāng)前顯示高亮的小圓點(diǎn)和你點(diǎn)擊的小圓點(diǎn)是同一個(gè)小圓點(diǎn)。
既然這樣;那么在function animate(offset)函數(shù)中,就可以加個(gè)判斷,當(dāng)offset =0時(shí),什么也不做,
也就是退出該函數(shù)。
2019-11-08
不移動(dòng)后面的代碼就必要執(zhí)行了