2012-10-31 64 views
0

我有一個鏈接,顯示鼠標懸停div。 在鼠標移出後,我再次隱藏div。 但是,如果用戶從未將鼠標懸停在div上,那麼它將保持打開狀態,因此我需要在一定時間後隱藏div。 由於這些是2個元素(鏈接和div),我不認爲我可以使用.hover。我最好怎麼寫這個來隱藏.tooltip配置文件10秒後沒有鼠標懸停?如果沒有鼠標隱藏

$("#profile").mouseenter(function(){ 
var position = $(".tooltip-profile").offset(); 
$(".tooltip-profile").css({ position: "absolute", left: "720px", top: "-110px" }); 
$(".tooltip-profile").fadeIn(500); 
}); 
$(".tooltip-profile").mouseleave(function(){ 
$(".tooltip-profile").fadeOut(500); 
}); 

回答

0

您需要提供的回調函數,你fadeIn()定義的10秒的超時值。如果鼠標又是鏈接或提示不要隱藏工具提示在此期間

$("#profile").mouseenter(function() { 
    var position = $(".tooltip-profile").offset(); 
    $(".tooltip-profile").css({ 
     position: "absolute", 
     left: "720px", 
     top: "-110px" 
    }); 
    $(".tooltip-profile").fadeIn(500, function() { 
     setTimeOut(function() { 
      $(".tooltip-profile").fadeOut(500); 
     }, 10000); 
    }); 
}); 
$(".tooltip-profile").mouseleave(function() { 
    $(".tooltip-profile").fadeOut(500); 
}); 
+0

好的,但這會殺死我的mouseleave功能? – infatti

0

等待使用的鼠標離開的setTimeout一段時間:當到達,超時將​​的元素。

 var keepOpend ; 
    $("#profile").mouseenter(function(){ keepOpend = true; 
     var position = $(".tooltip-profile").offset(); 
     $(".tooltip-profile").css({ position: "absolute", left: "720px", top: "-110px" }); 
     $(".tooltip-profile").fadeIn(500); 
    }).mouseleave(function(){ 
      keepOpend = false; 
      setTimeout(function(){ 
      !keepOpend && $(".tooltip-profile").fadeOut(500); 
     }, 500); 
    }); 
    $(".tooltip-profile").mouseleave(function(){ 
      keepOpend = false; 
      setTimeout(function(){ 
      !keepOpend && $(".tooltip-profile").fadeOut(500); 
     }, 500); 
    }).mouseenter(function(){ 
     keepOpend = true; 

    }); 
+0

很酷。這樣可行。 – infatti

+0

我正在使用這種方法基本上作爲一個小型的下拉式氣泡爲我的網站周圍的按鈕,隱藏/顯示動態數據的div。任何更好的插件的想法,我可以用這個? – infatti

+0

對不起,我不知道任何特定的工具提示插件,但必須有大量的jQuery插件工具提示。 – Anoop