2017-07-04 88 views
0

我遇到了淡入淡出的文字。需要逐漸淡入淡出文字

在開發人員模式下,我可以看到文本從0到1的不透明度隨序列而變化。這是如何實現的?

<div class="text" style="opacity: 0;">The</div> 
<div class="text" style="opacity: 0;">Nomads</div> 
+0

你有你自己嘗試新鮮事物?你應該先嚐試一下,如果你對代碼有任何疑問,那麼你應該問這個問題。 – badiya

回答

0

您可以使用以下方法:

很明顯,我打算使用JQuery

jQuery('<div_name>').css('opacity', '<opacity_value>'); 
2

下面是使用CSS3動畫和keyframes財產一個非常簡單的方法(請注意:我已經編輯了這個答案,包括來自Frits的評論的改進)

Alth ough您可能需要調整它少

其淡入淡出一個文本的其他

後是一個相當失去規範。

/* Define the key frames for animating the fade in/fade out */ 
 

 
@keyframes fade { 
 
    0% { 
 
    opacity: 1; 
 
    } 
 
    50% { 
 
    opacity: 0; 
 
    } 
 
    100% { 
 
    opacity: 1; 
 
    } 
 
} 
 

 

 
/* attach the animations to the elements via their id attribute using a delay of 0s and 50% of the runtime respectively */ 
 

 
#one { 
 
    animation: fade 3s infinite 0s; 
 
} 
 

 
#two { 
 
    animation: fade 3s infinite 1.5s; 
 
}
<p id="one"> 
 
    This line of text will fade out as the next lines fade in 
 
</p> 
 
<p id="two"> 
 
    This line of text will fade in as the previous lines fade out 
 
</p>

+0

堅持CSS。但是,如果我可以提出建議,則可以在關鍵幀上以「25%」和「75%」刪除關鍵幀,因爲在任何情況下,這些值都會在這些時間間隔內被擊中。你也可以在兩個元素上使用相同的關鍵幀,並且在第二個元素的動畫語法上延遲一半的動畫時間(即1.5s) - 這裏是一個例子(https://jsfiddle.net/6qy8ot4h/ )隨時將其納入您的答案。 – Frits

+1

謝謝@Frits - 是很樂意更新答案,以反映這些,這只是我敲了現場,所以沒有把太多的優化到位的方式 – Luke