2013-08-19 35 views
-1

下面是一個jQuery語句,它在其單擊事件上隱藏了一個div元素。無論5秒後沒有點擊,我都希望元素淡出。有沒有簡單的方法,我可以用相同的表達式調用fadeOut函數,或者沒有click事件干擾動畫?在jQuery中的事件處理程序上組合動作

$(".fadeOutbox").click(function(){ 
    $(this).fadeOut('slow'); 
}); 
+0

你想在點擊時隱藏它,但如果他不點擊卻仍然隱藏?什麼時候知道? – Sergio

+0

您需要安排超時後,我假設這是出現的任何事件,您沒有指定任何。 –

回答

3

我想這是另一個功能是啓動與箱內。此解決方案會在5秒後隱藏框,或者在點擊後立即隱藏。

var $box = $('.fadeOutbox'); 
var fadeOut = function() { 
    clearTimeout(timeout); 
    $box.fadeOut('slow'); 
}; 
var timeout = setTimeout(fadeOut, 5000); 
$box.click(fadeOut); 
+0

感謝這項工作。 –

1

嘗試

$('#div').delay(5000).fadeOut(400) 

Demo

+0

@Mikel Max要求隱藏,如果不單擊 – sdespont

2

編輯澄清:

var clear = setTimeout(function(){ $(".fadeOutbox").fadeOut('slow'); }, 5000); 

$(".fadeOutbox").on('click', function(){ 
    clearTimeout(clear); 
}); 

演示:http://jsfiddle.net/mGbHq/

+0

澄清。要麼是在我看到它後編輯的,要麼是我錯過了它。上面的正確答案。 –

2

使用超時內單擊處理的:

setTimeout(function() { 
    $(".fadeOutbox").fadeOut('slow'); 
}, 5000); 

jQuery代碼就變成了:

// set a timeout for 5 seconds 
setTimeout(function() { 
    $(".fadeOutbox").fadeOut('slow'); 
}, 5000); 

// attach click handler 
$(".fadeOutbox").on("click", function() { 
    $(this).fadeOut('slow'); 
}); 

JSFIDDLE

+0

這不起作用 –

+0

@MikelMax使用超時解決方案,'delay()'解決方案阻止你的div點擊動畫。 –

2

保存的事實,即用戶點擊與否和定時器測試

var isClicked = false; 

setTimeout(function() { 
    if(!isClicked) 
     $(".fadeOutbox").fadeOut('slow'); 
}, 5000); 

$(".fadeOutbox").click(function() { 
isClicked = true; 
}); 
2

試試這個:

var wasClicked = false; 
$(".fadeOutbox").click(function() { wasClicked = true; }); 

setTimeout(function() { 
    if(wasClicked = false) 
     $(".fadeOutbox").fadeOut('slow'); 
}, 5000); 
4

大多數的jQuery組件鏈可以,你的函數按照原樣返回對初始對象的引用。

你可以達到你想要什麼簡單地使用:

$(".fadeOutbox").click(function() { 
    $(this).stop().fadeOut('slow'); 
}).delay(5000).fadeOut('slow'); 

基本上行文的onclick,淡出否則5秒後淡出。

+1

不要使用延遲功能,因爲點擊處理程序的動畫將不起作用:http://jsfiddle.net/KSEXY/1/ –

+0

@Johnツ斑點 - 更新和工作正常:) – Emissary

+0

好!投票! –

2

嘗試爲超時值保存一個變量,並在用戶每次單擊時清除該變量。

Working example

// Timeout variable 
var t; 

$('.fadeOutBox').click(function() 
{ 
    $box = $(this); 
    $box.fadeIn("fast"); 

    // Reset the timeout 
    clearTimeout(t); 
    t = setTimeout(function() 
    { 
     $box.fadeOut("slow"); 
    }, 5000); 
}); 

希望這有助於你。

2

哇,沒有一個答案給出了簡單的解決方案:使用setTimeout和取消點擊超時:

$(".fadeOutbox").click(function() { 
    // Cache the jQuery object 
    var $this = $(this); 

    // Do we already have a timer running? 
    var timer = $this.data("timer"); 
    if (timer) { 
     // Yes, cancel it 
     clearTimeout(timer); 
     $this.removeData("timer"); 
    } 

    // (You may want an `else` here, it's not clear) 

    // In five seconds, fade out 
    $this.data("timer", setTimeout(function() { 
     $this.removeData("timer"); 
     $this.fadeOut('slow'); 
    }, 5000)); 
}); 

我不是100%肯定的是,以上是你想要的事件觸發,但相關兩段代碼是這樣,它的時間表定時動作:

// In five seconds, fade out 
$this.data("timer", setTimeout(function() { 
    $this.removeData("timer"); 
    $this.fadeOut('slow'); 
}, 5000)); 

與此,它取消它(例如,在點擊):

var timer = $this.data("timer"); 
if (timer) { 
    // Yes, cancel it 
    clearTimeout(timer); 
    $this.removeData("timer"); 
}