2013-08-27 43 views
0

現在我有這樣的代碼:變化格顏色 - jQuery函數

$('.a').mouseenter(function(){ 
    var $this = $(this); 
    clearTimeout($this.data('timerMouseleave')); 
    $this.css('border', 'solid 1px #444444') 
}).mouseleave(function(){ 
    var $this = $(this); 
    var timer = setTimeout($.proxy(function(){ 
     $this.css('border', 'solid 1px #dddddd') 
    }, this), 1000) 
    $this.data('timerMouseleave', timer) 
}).click(function(){ 
    var $this = $(this); 
    $this.css('border', 'solid 1px black') 
    $this.off('mouseenter mouseleave'); 
}) 

http://jsfiddle.net/7dXAs/6/

我要再次添加紅色邊框僅在進入格的情況下,而超時仍然在。 (如果可能的話,請在這種情況下播放聲音,例如aaa.wav)。

我需要保持這種行爲的其餘部分完全如此,這意味着紅色邊框通常應該在超時後變回默認值。

澄清

超時/延遲被鼠標離開後觸發,並持續1秒鐘。

  • 目前的情況:如果你進入前再次1秒到期的股利,超時被刪除,然後又鼠標離開
  • 想情況後,再次觸發:如果你進入前再次1秒到期的股利,邊界變得紅,超時被刪除,然後又鼠標離開
+0

請詳細說明您的問題..很難理解您真正想要什麼。 – Sid

+0

哪個紅色邊框?我的代碼中沒有看到任何紅色。 – Rob

+0

完成後,從'.data()'中移除'timerMouseleave'。這樣,如果'timerMouseleave'存在於元素的'.data()'中,則意味着超時不是由於它。 (我希望我能正確理解你的問題) – naor

回答

0

後再次觸發嘗試

$('.a').mouseenter(function(){ 
    var $this = $(this); 
    clearTimeout($this.data('timerMouseleave')); 
    if($this.hasClass('hide-timer')){ 
     $this.css('border', 'solid 1px red') 
    } else { 
     $this.css('border', 'solid 1px #444444') 
    } 
}).mouseleave(function(){ 
    var $this = $(this); 
    var timer = setTimeout($.proxy(function(){ 
     $this.css('border', 'solid 1px #dddddd').removeClass('hide-timer') 
    }, this), 1000) 
    $this.data('timerMouseleave', timer).addClass('hide-timer') 
}).click(function(){ 
    var $this = $(this); 
    $this.css('border', 'solid 1px black') 
    $this.off('mouseenter mouseleave'); 
}) 

演示:Fiddle

+0

令人驚歎!非常非常感謝你! – weaponx

+0

嗨阿倫,我有一個關於座標的具體問題:http://stackoverflow.com/questions/18811991/mouse-coordinates-for-one-div-displayed-in-another-div-jquery – weaponx

+0

嘿,阿倫,因爲你的幫助對我來說真的很重要,我想以某種方式來報答你(單方面 - 在我身上,但對我們雙方都有用) - 如果你有一個網站/博客/等,如果你對細節感興趣,我在這裏 - [email protected] – weaponx