求解,有bug
window.onload?=?function(){ var?container?=?document.getElementById("container"), list?=?document.getElementById("list"), btn?=document.getElementById("btn").getElementsByTagName("span"), prev?=?document.getElementById("prev"), next?=?document.getElementById("next"), index?=?1, animated?=?false, timer; //圓點的函數(shù) function?showButton(){ //清除圓點屬性 for(var?i?=?0;?i?<?btn.length;?i++){ if(btn[i].className?==?'on'){ btn[i].className?=?''; break; } } btn[index?-?1].className?=?'on'; } //點擊圓點切換圖片 for(var?i?=?0;?i?<?btn.length;?i++){ btn[i].onclick?=?function(){ //條件成立,退出函數(shù),后面的將不執(zhí)行 if(this.className?==?'on'){ return; } //獲取當前點擊的index值 var?myIndex?=?parseInt(this.getAttribute('index')); var?offset?=?-960?*?(myIndex?-?index); animate(offset); index?=?myIndex; if(!animated){ showButton(); } } } //封裝點擊箭頭切換圖片的函數(shù) function?animate(offset){ animated?=?true; var?newLeft?=?parseInt(list.style.left)?+?offset; var?time?=?300; //位移總時間 var?interval?=?5; //位移間隔時間 var?speed?=?offset/(time/interval);??//每次位移量 function?go(){ if(speed?<?0?&&?parseInt(list.style.left)?>?newLeft?||?speed?>?0?&&?parseInt(list.style.left)?<?newLeft){ list.style.left?=?parseInt(list.style.left)?+?speed?+?"px"; setTimeout(go,interval); }else{ animated?=?false; list.style.left?=?newLeft?+?"px"; //無限滾動 if(newLeft?>?-960){ list.style.left?=?-4800?+?"px"; } if(newLeft?<?-4800){ list.style.left?=?-960?+?"px"; } } } go(); } //自動切換函數(shù) function?play(){ timer?=?setInterval(function(){ next.onclick(); },1500); } //清除定時器 function?stop(){ clearInterval(timer); } //點擊右箭頭 next.onclick?=?function(){ if(!animated){ if(index?==?5){ index?=?1; }else{ index?+=?1; } showButton(); animate(-960); } } //點擊左箭頭 prev.onclick?=?function(){ if(!animated){ if(index?==?1){ index?=?5; }else{ index?-=?1; } showButton(); animate(960); } } container.onmouseover?=?stop; container.onmouseout?=?play; play(); }
這里有個bug求解,點擊圓點圖片跳轉沒問題,但是不會高亮,要點兩次才會亮起來,,求解
2017-07-11
32-36行應該是:
?? ? ? ? ? ?index?=?myIndex;
?????????????showButton();?
????????????if(!animated){
????????????????animate(offset);
????????????}
上一個動畫執(zhí)行完了才可以執(zhí)行? animate(offset);
而你上面的是 動畫不在執(zhí)行的時候才能執(zhí)行showButton(); 點第一次時在執(zhí)行動畫,這個時候執(zhí)行不了?showButton(); 點第二次時沒在執(zhí)行動畫然后執(zhí)行了?showButton(); 所以出現(xiàn)你那種情況。