使用CSS对页面加载的淡入效果 [英] Using CSS for fade-in effect on page load
问题描述
可以使用CSS Transitions来允许文本段落在页面加载时淡入?
Can CSS Transitions be used to allow a text paragraph to fade-in on page load?
我真的很喜欢它在 http://dotmailapp.com/ ,并且希望使用类似的效果CSS
I really like how it looks on http://dotmailapp.com/ and would love to use a similar effect using CSS.
注意:此域名已被购买,不再具有上述效果。您可以在查看存档的副本 。
Note: The domain has since been purchased and no longer has the effect mentioned. An archived copy can be viewed 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;
}
如何在加载时触发转换?
How can the transition be triggered on load?
推荐答案
方法1:
如果您正在寻找一个自我调用的转换,使用 CSS3动画,但它们不受支持,但此
Method 1:
If you are looking for a self-invoking transition then you should use CSS3 Animations, they aren't supported as well but this is exactly the kind of thing they were made for.
#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; }
}
演示
- http://jsfiddle.net/SO_AMK/VV2ek/ li>
Demo
- http://jsfiddle.net/SO_AMK/VV2ek/
所有现代浏览器,IE 10+: http://caniuse.com/#feat=css-animation
All modern browsers, IE 10+: http://caniuse.com/#feat=css-animation
或者,您可以使用jQuery plain JS,见第三个代码块)在负载上更改类:
Alternatively, you can use jQuery (or plain JS, see third code block) to change the class on load:
$("#test p").addClass("load");
$ b b
CSS
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; }
Plain JS(不在演示中)
Plain JS (not in demo)
document.getElementById("test").children[0].className += " load";
演示
- http://jsfiddle.net/SO_AMK/a9dnW/
- http://jsfiddle.net/SO_AMK/a9dnW/
Demo
所有现代浏览器,IE 10+: http://caniuse.com/#feat=css-transitions
All modern browsers, IE 10+: http://caniuse.com/#feat=css-transitions
或者,您可以使用 .Mail 使用:
Or, you can use the method that .Mail uses:
$("#test p").delay(1000).animate({ opacity: 1 }, 700);
CSS
CSS
#test p { opacity: 0; font-size: 21px; margin-top: 25px; text-align: center; }
演示
- http://jsfiddle.net/SO_AMK/a9dnW/3/
- http://jsfiddle.net/SO_AMK/a9dnW/3/
Demo
jQuery 1.x :所有现代浏览器,IE 6+: http://jquery.com/browser-support/
jQuery 2.x :所有现代浏览器,IE 9+: http://jquery.com/browser-support/jQuery 1.x: All modern browsers, IE 6+: http://jquery.com/browser-support/
jQuery 2.x: All modern browsers, IE 9+: http://jquery.com/browser-support/此方法与目标浏览器最兼容不需要支持CSS3转换或动画。
This method is the most cross-compatible as the target browser does not need to support CSS3 transitions or animations.
这篇关于使用CSS对页面加载的淡入效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!