2011-02-10 164 views
0

我有一些mouseover事件的鏈接。 onmouseover將顯示一個圖層。 它工作正常,但它有點煩人,因爲如果鼠標無意間超過鏈接層將顯示。帶超時的鼠標懸停事件

現在我想要的是,如果用戶有鼠標懸停鏈接,並保持200毫秒的鏈接,那麼它應該顯示圖層。

如何以更好的方式做到這一點。因爲我需要註冊並設置超時功能,並且如果鼠標在200 MS之前離開,我必須清除超時。

感謝您的幫助。

+1

你的意思是你想要一個比創建一個計時器更好的方法並在`mouseout`上清除它,或者你不知道該怎麼做? – 2011-02-10 10:26:51

回答

0

enter image description here

<!doctype html> 

<html> 
    <head> 
     <script> 
      window.onload = function() { 
       var interval = null, link = null; 

       // Window = Container 
       window.onmousemove = function (event) { 
        var target = event.target; 

        if (target.nodeName === "A") { 
         link = target; 

         if (interval === null) { 
          interval = setInterval (function() { 
           clearInterval (interval); 

           interval = null; 

           open ("", ""); // Display layer 
          }, 1000); // I think 1 sec is better 
         } 
        } 

        if (interval !== null && link !== target) { 
         clearInterval (interval); 

         interval = null; 
        } 
       } 
      } 
     </script> 

     <title></title> 
    </head> 

    <body> 
     <a href = "#">Click me</a> 
     <a href = "#">Click me</a> 
    </body> 
</html> 
0

我已經寫JS代碼能夠做到這一點,在幾個不同的公司。如果您還想從鼠標上刪除鼠標圖層,那麼情況就會變得複雜,並且也會推遲解僱。

你是對的,你需要定時器的timerID存儲,以便你可以清除它們。我不會爲你寫代碼,但是如果我有一個你可以批評你的實現。