jQuery hover延時觸發

$('.grow').hover(function(){
    var _this = $(this);                    // 把$(this)對象賦給一個變量
    trigger = setTimeout(function(){
        _this.animate({height:'150px'},300);// (非常重要)jQuery的setTimeout中不能直接使用$(this)
    },200);                                 // 延遲時間0.2秒
}, function(){
    clearTimeout(trigger);                  // 清除上面的延遲觸發的事件
    $(this).animate({height:'70px'},300);
});

setTimeout方法使用時需注意:

//以下兩種方式都行:
setTimeout(function () { test(); }, 2000);
//或者
setTimeout('test()',2000);
function test(){
 alert("aaaa");
}
//以下是錯誤示例
setTimeout(test(),2000);
//會馬上執行,沒有延遲效果

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章