jQuery因mouseover,mouseout冒泡產生的閃爍問題

來源:互聯網
上載者:User

標籤:lib   targe   ext   tar   time   blog   min   lan   art   

由於瀏覽器的冒泡行為。造成如果在一個DIV元素上同時定義了mouseover,mouseout的時候,當滑鼠移動到DIV中的child子項目的時候,就會同時執行了兩個操作mouseover和mouseout。

解決方案:阻止冒泡行為,當執行mouseover的時候不觸發mouseout的操作。

方法1:
順延強制(setTimeout)、取消延遲(clearTimeout),就是當mouseout的時候順延強制,而在mouseover的時候取消順延強制。當滑鼠在DIV上邊移動的時候因為延遲的執行所以mouseout一直都不會被觸發。

$(‘div‘).mouseout(function(){     clearTimeout(t);      t=setTimeout(zoomIn,400);    }).mouseover(function(){   if(t!=null)clearTimeout(t);   t=null;  }); 

  

方法2:jQuery(需要版本號碼大於1.2.2)
mouseenter和mouseleave事件IE特有的函數,使用jquery就很好的解決了相容問題。函數的原理當第一次滑鼠進入節點地區時觸發,以後在節點地區內(子節點間)移動時不觸發。

$(‘div‘).mouseout(function(){     clearTimeout(t);      t=setTimeout(zoomIn,400);    }).mouseover(function(){   if(t!=null)clearTimeout(t);   t=null;  }); 

  

jquery版本 >1.4簡化寫法:

$(‘div‘).mouseout(function(){     clearTimeout(t);      t=setTimeout(zoomIn,400);    }).mouseover(function(){   if(t!=null)clearTimeout(t);   t=null;  }); 

  

jQuery因mouseover,mouseout冒泡產生的閃爍問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.