一、關(guān)于鼠標(biāo)hover事件及延時(shí)
鼠標(biāo)經(jīng)過事件為web頁面上非常常見的事件之一。簡單的hover可以用CSS :hover偽類實(shí)現(xiàn),復(fù)雜點(diǎn)的用js。
一般情況下,我們是不對(duì)鼠標(biāo)hover事件進(jìn)行延時(shí)處理。但是,有時(shí)候,為了避免不必要的干擾,常會(huì)對(duì)鼠標(biāo)hover事件進(jìn)行延時(shí)處理。所謂干擾,就是當(dāng)用戶鼠標(biāo)不經(jīng)意劃過摸個(gè)鏈接,選項(xiàng)卡,或是其他區(qū)域時(shí),本沒有顯示隱藏層,或是選項(xiàng)卡切換,但是由于這些元素上綁定了hover事件(或是mouseover事件),且無延時(shí),這些時(shí)間就會(huì)立即觸發(fā),反而會(huì)對(duì)用戶進(jìn)行干擾。
例如,在騰訊網(wǎng)首頁,幾乎對(duì)所有的鼠標(biāo)經(jīng)過事件進(jìn)行了延時(shí)處理,例如其選項(xiàng)卡:
復(fù)制代碼 代碼如下:
(function($){
$.fn.hoverDelay = function(options){
var defaults = {
hoverDuring: 200,
outDuring: 200,
hoverEvent: function(){
$.noop();
},
outEvent: function(){
$.noop();
}
};
var sets = $.extend(defaults,options || {});
var hoverTimer, outTimer;
return $(this).each(function(){
$(this).hover(function(){
clearTimeout(outTimer);
hoverTimer = setTimeout(sets.hoverEvent, sets.hoverDuring);
},function(){
clearTimeout(hoverTimer);
outTimer = setTimeout(sets.outEvent, sets.outDuring);
});
});
}
})(jQuery);
復(fù)制代碼 代碼如下:
$("#test").hoverDelay({
hoverEvent: function(){
alert("經(jīng)過我!");
}
});
復(fù)制代碼 代碼如下:
<div>
<h3>網(wǎng)頁</h3>
<h3>圖片</h3>
<h3>視頻</h3>
<h3>音樂</h3>
<h3>搜吧</h3>
<h3>問問</h3>
<h3>博客</h3>
<h3>更多▼
<div>
<ul>
<li><a href="#">綜合</a></li>
<li><a href="#">新聞</a></li>
<li><a href="#">詞典</a></li>
<li><a href="#">生活</a></li>
<li><a href="#">百科</a></li>
<li><a href="#">所有產(chǎn)品</a></li>
</ul>
</div>
</h3>
</div>
復(fù)制代碼 代碼如下:
$(".s2").each(function(){
$("#sosoFod h3").each(function(){
var that = $(this);
var id = that.attr("id");
if(id){
that.hoverDelay({
hoverEvent: function(){
$(".s1").attr("class","s2");
that.attr("class","s1"); //感謝“type23”提供了綁定對(duì)象方法
$(this).attr("class","s1");
}
});
}else{
that.hoverDelay({
outDuring: 1000,
hoverEvent: function(){
$("#tm").show();
},
outEvent: function(){
$("#tm").hide();
}
});
}
});
hoverDuring 鼠標(biāo)經(jīng)過的延時(shí)時(shí)間
outDuring 鼠標(biāo)移出的延時(shí)時(shí)間
hoverEvent 鼠標(biāo)經(jīng)過執(zhí)行的方法
outEvent 鼠標(biāo)移出執(zhí)行的方法
新聞熱點(diǎn)
疑難解答
圖片精選