function $(id){?? ?return typeof id==='string'?document.getElementById(id):id;}window.onload=function(){? // 標(biāo)簽的索引? var index1=0;? var timer1=null;? var lis1=$('notice-tit').getElementsByTagName('li'),????? divs1=$('notice-con').getElementsByTagName('div');? if(lis1.length!=divs1.length) return;? // 遍歷所有的頁簽? for(var i=0;i<lis1.length;i++){??? lis1[i].id=i;??? lis1[i].onmouseover=function(){????? // 用that這個變量來引用當(dāng)前滑過的li????? var that=this;????? // 如果存在準(zhǔn)備執(zhí)行的定時器,立刻清除,只有當(dāng)前停留時間大于500ms時才開始執(zhí)行????? if(timer1){??????? clearTimeout(timer1);??????? timer1=null;????? }????? // 延遲半秒執(zhí)行????? timer1=window.setTimeout(function(){??????? for(var j=0;j<lis1.length;j++){????????? lis1[j].className='';????????? divs1[j].style.display='none';??????? }??????? lis1[that.id].className='select';??????? divs1[that.id].style.display='block';????? },500);??? }? }}function $(id){?? ?return typeof id==='string'?document.getElementById(id):id;}window.onload=tab;function tab(){? // 當(dāng)前高亮顯示的頁簽的索引? var index=0;? var timer=null;? // 獲取所有的頁簽和要切換的內(nèi)容? var lis=$('new_tit').getElementsByTagName('li');? var divs=$('new-con').getElementsByTagName('div');? // 遍歷每一個頁簽且給他們綁定事件? for(var e=0;e<lis.length;e++){??? lis[e].id=e;??? lis[e].onmouseover=function(){????? clearInterval(timer);????? changeOption(this.id);??? }??? lis[e].onmouseout=function(){ ?????? timer=setInterval(autoPlay,2000);?? ???? }? }? ? if(timer){??? clearInterval(timer);??? timer=null;? } ? // 添加定時器,改變當(dāng)前高亮的索引? timer=setInterval(autoPlay,2000);? function autoPlay(){????? index++;????? if(index>=lis.length){???????? index=0;????? }????? changeOption(index);? }? function changeOption(curIndex){??? for(var f=0;f<lis.length;f++){?????? lis[f].className='';?????? divs[f].style.display='none';??? }??? // 高亮顯示當(dāng)前頁簽??? lis[curIndex].className='select';??? divs[curIndex].style.display='block';??? index=curIndex;? }}看如上代碼,為什么兩條代碼不能同時執(zhí)行呢?我是小白,問題過于簡單,請大神多多原諒!
選項卡的自動切換和手動延遲切換可以同時使用嗎?
zigengziyun
2014-12-30 15:52:23