Tab選項卡切換我遇到了三個問題,求大神解答,謝謝
function $(id){
? ?return typeof id === 'string'?document.getElementById(id):id;
}
window.onload = function(){
? ?var lis = $('notice-tit').getElementsByTagName('li');
? ?var divs = $('notice-con').getElementsByTagName('div');
? ?var index = 0;
? ?var timer = null;
? ?for(var i=0;i<lis.length;i++){
? ? ? ?lis[i].id = i;
? ? ? ?lis[i].onmouseenter = function(){
? ? ? ? ? ?clearInterval(timer);
? ? ? ? ? ?changeOption(this.id);
? ? ? ?};
? ? ? ?lis[i].onmouseleave = function(){
? ? ? ? ? ?timer = setInterval(autoPlay,2000);
? ? ? ?}
? ?}
? ?if(timer){
? ? ? ?clearInterval(timer);
? ? ? ?timer=null;
? ?} //clearInterval(timer); ???有啥區別?為啥刪了if(timer){}也沒事??
? ?timer = setInterval(autoPlay,2000);
? ?function autoPlay() {
? ? ? ?index++;
? ? ? ?if (index >= lis.length) {
? ? ? ? ? ?index = 0;
? ? ? ?}
? ? ? ?changeOption(index);
? ?}
? ?function changeOption(currentIndex){
? ? ? ?for(var j=0;j<lis.length;j++){
? ? ? ? ? ?lis[j].className = '';
? ? ? ? ? ?divs[j].style.display = 'none';
? ? ? ?}
? ? ? ?lis[currentIndex].className = 'select';
? ? ? ?divs[currentIndex].style.display = 'block';
? ? ? ?index = currentIndex;
? ?}
};
一: ?onmouseleave事件中,如果啟動定時器不寫timer= ?的時候,鼠標離開它會自動亂切換,亂跳;具體代碼:lis[i].onmouseleave = function(){
? ? ? ? ? ?setInterval(autoPlay,2000);
? ? ? ?}?
????????為什么一定要寫成 timer = setInterval(autoPlay,2000);?
二、鼠標放在內容標簽里,標題標簽還是自動切換,對用戶體驗不好,用戶肯定想把鼠標放在內容上瀏覽內容,標題標簽不切換,怎么解決?
第三點 ?if(timer){
? ? ? ?clearInterval(timer);
? ? ? ?timer=null;
? ?} ?為什么不換成 clearInterval(timer); ??
? ?我試了下為什么timer=null;不寫也沒影響?
? ?甚至這兩種方法都不寫也沒影響啊,我試了下,并沒有出現老師說的快速切換多個動畫BUG,很正常的切換啊,怎么回事?
2017-08-15
1;因為你沒有指定timer變量去承載這個方法,方法就找不到執行的規律
2:可以直接獲取內容,并遍歷執行切換函數
3:給clear=null;是為了讓程序有更好的可讀性,并從新賦值給clear,避免不必要的bug