2011-08-19 56 views
1

我想爲所有用戶提醒,我們有新的東西,所以當用戶訪問網站時,我想要一個小小的語音氣泡出現幾秒鐘,這會說「不要忘記檢查我們的新內容」並消失。任何幫助如何開始在此工作?div出現幾秒鐘,並在jquery/javascript中消失?

Regards,

+1

你到目前爲止嘗試過什麼?從你的嘗試中發佈一些代碼,人們將更願意幫助你。 –

回答

3
$('#mydiv').fadeIn(3000).delay(4000).fadeOut(3000); 

這應該爲你做。

+0

只是將其包裝在腳本標籤中,那就是它? –

+0

包含jquery腳本標記,然後將該行代碼包裝在$(document).ready(function(){ - HERE-})中;然後把它放在腳本標籤中。完成,它會起作用。 – frosty

+0

簡單而酷!成功了! –

0

Checkout JQuery動畫,它們也處理事件鏈。

0

您只需製作您想要顯示的內容並將其放置在<div>中即可。

然後(如果你使用jQuery),你可以調用它的fadeIn,並setTimeoutfadeOut ...

編輯

如果你想要的東西,jQuery的自由,你可以一展身手與this fader object我改編自WWW的某個地方(我忘了在哪裏,否則我會相信他們)。請注意,這不像jQuery那樣通用,可能也不是交叉兼容的,但是我在帶寬很重要的情況下使用它的結果很好,我不希望每次都下載整個jQuery庫。

如何使用它的一個例子:

var objToFade = document.getElementById('object_to_fade'); 
var theFader = new FadeHandlers(objToFade); 

// Fade the object out 
theFader.disappear(); 

// Fade the object in 
theFader.appear(); 

// Hide the object instantly 
theFader.hide(); 

// Show the object instantly 
theFader.show(); 

// The appear() and disappear() methods will do a callback on completion, which can be defined like this... 
theFader.fadeCallBack = function (el) { 
    // The @el parameter is a reference to objToFade 
    alert(el.id+' has finished fading'); 
} 

// There is an boolean isVisible property... guess what it means... 
if (theFader.isVisible) { 
    theFader.disappear(); 
} 

// If you have many objects to fade, I like to do this... 
var objToFade = document.getElementById('object_to_fade'); 
objToFade.fader = new FadeHandlers(objToFade); 
// ...then you can do things like this later on... 
document.getElementById('object_to_fade').fader.appear(); 

如果你希望對象開始隱藏,只是這樣做在你的風格:

#object_to_fade { 
    filter:alpha(opacity=0); 
    opacity: 0; 
} 

有更多的選擇等等,看評論在文件中...

看看this JS fiddle如果你想玩它。

+0

setTimeout有它的位置。這不是其中之一。 – frosty

+0

爲什麼不呢?我的意思是我認爲setTimeout已被過度使用,但我不認爲這是錯的嗎? – DaveRandom

+0

@aaronfrost - 爲什麼不是'setTimeout'的地方?考慮到在問題中沒有提及jQuery的任何地方,我不認爲這是jQuery的地方... –

0

也許pulsate從jQuery的效果可以幫助你。

0

大家似乎都很高興給jQuery的答案,當沒有提到問題中的任何地方的jQuery,我會添加一個非jQuery的答案。

假設你的消息包含在一個元素ID爲「新」,你可以簡單地在網頁加載時運行此:

setTimeout(function() { 
    document.getElementById("new").style.display = "none"; 
}, 2000); 

下面是上述的example在行動。

但是,請注意,它可能會更容易使用jQuery,如果您想要淡入淡出效果,使用jQuery肯定會更容易。

+0

它看起來我需要什麼。任何想法如何包括淡入/淡出效果? –