能否使用 CSS 过渡让文本段落在页面加载时淡入?
我真的很喜欢它在http://dotmailapp.com/ 上的样子并希望使用 CSS 来使用类似的效果。该域名已被购买,不再具有提及的效果。可以查看存档副本 on the Wayback Machine .
插图
有这个标记:
<div id="test">
<p>This is a test</p>
</div>
使用以下 CSS 规则:
#test p {
opacity: 0;
margin-top: 25px;
font-size: 21px;
text-align: center;
-webkit-transition: opacity 2s ease-in;
-moz-transition: opacity 2s ease-in;
-o-transition: opacity 2s ease-in;
-ms-transition: opacity 2s ease-in;
transition: opacity 2s ease-in;
}
如何在加载时触发转换?
最佳答案
方法一:
如果您正在寻找自调用转换,那么您应该使用 CSS 3 Animations .它们也不受支持,但这正是它们的用途。
CSS
#test p {
margin-top: 25px;
font-size: 21px;
text-align: center;
-webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */
-moz-animation: fadein 2s; /* Firefox < 16 */
-ms-animation: fadein 2s; /* Internet Explorer */
-o-animation: fadein 2s; /* Opera < 12.1 */
animation: fadein 2s;
}
@keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
/* Firefox < 16 */
@-moz-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
/* Internet Explorer */
@-ms-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
/* Opera < 12.1 */
@-o-keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
演示
浏览器支持
所有现代浏览器和 Internet Explorer 10(及更高版本):http://caniuse.com/#feat=css-animation
方法二:
或者,您可以使用 jQuery(或纯 JavaScript;请参阅第三个代码块)在加载时更改类:
jQuery
$("#test p").addClass("load");
CSS
#test p {
opacity: 0;
font-size: 21px;
margin-top: 25px;
text-align: center;
-webkit-transition: opacity 2s ease-in;
-moz-transition: opacity 2s ease-in;
-ms-transition: opacity 2s ease-in;
-o-transition: opacity 2s ease-in;
transition: opacity 2s ease-in;
}
#test p.load {
opacity: 1;
}
纯 JavaScript(不在演示中)
document.getElementById("test").children[0].className += " load";
演示
浏览器支持
所有现代浏览器和 Internet Explorer 10(及更高版本):http://caniuse.com/#feat=css-transitions
方法三:
或者,您可以使用.Mail 使用的方法:
jQuery
$("#test p").delay(1000).animate({ opacity: 1 }, 700);
CSS
#test p {
opacity: 0;
font-size: 21px;
margin-top: 25px;
text-align: center;
}
演示
浏览器支持
jQuery 1.x:所有现代浏览器和 Internet Explorer 6(及更高版本):http://jquery.com/browser-support/
jQuery 2.x:所有现代浏览器和 Internet Explorer 9(及更高版本):http://jquery.com/browser-support/
此方法的交叉兼容性最高,因为目标浏览器不需要支持 CSS 3 过渡或动画。
关于css - 使用 CSS 实现页面加载的淡入效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11679567/