使用 CSS 实现页面加载的淡入效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11679567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 04:36:27  来源:igfitidea点击:

Using CSS for a fade-in effect on page load

csscss-transitionsopacity

提问by user1556266

Can CSS transitions be used to allow a text paragraph to fade-in on page load?

可以使用 CSS 过渡来允许文本段落在页面加载时淡入吗?

I really like how it looked on http://dotmailapp.com/and would love to use a similar effect using CSS. The domain has since been purchased and no longer has the effect mentioned. An archived copy can be viewed on the Wayback Machine.

我真的很喜欢它在http://dotmailapp.com/ 上的样子,并且很想使用 CSS 来使用类似的效果。该域名已被购买,不再具有上述效果。可以在 Wayback Machine 上查看存档副本。

Illustration

插图

Having this markup:

有这个标记:

<div id="test">
    <p>?This is a test</p>
</div>??????????????????????????????????????????????????????

With the following CSS rule:

使用以下 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?

如何在加载时触发转换?

回答by A.M.K

Method 1:

方法一:

If you are looking for a self-invoking transition then you should use CSS 3 Animations. They aren't supported either, but this is exactly the kind of thing they were made for.

如果您正在寻找自调用过渡,那么您应该使用CSS 3 Animations。它们也不受支持,但这正是它们的用途。

CSS

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; }
}

Demo

演示

Browser Support

浏览器支持

All modern browsers and Internet Explorer 10 (and later): http://caniuse.com/#feat=css-animation

所有现代浏览器和 Internet Explorer 10(及更高版本):http: //caniuse.com/#feat=css-animation



Method 2:

方法二:

Alternatively, you can use jQuery (or plain JavaScript; see the third code block) to change the class on load:

或者,您可以使用 jQuery(或纯 JavaScript;请参阅第三个代码块)在加载时更改类:

jQuery

jQuery

$("#test p").addClass("load");?

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 JavaScript (not in the demo)

纯 JavaScript(不在演示中)

document.getElementById("test").children[0].className += " load";

Demo

演示

Browser Support

浏览器支持

All modern browsers and Internet Explorer 10 (and later): http://caniuse.com/#feat=css-transitions

所有现代浏览器和 Internet Explorer 10(及更高版本):http: //caniuse.com/#feat=css-transitions



Method 3:

方法三:

Or, you can use the method that .Mailuses:

或者,您可以使用.Mail使用的方法:

jQuery

jQuery

$("#test p").delay(1000).animate({ opacity: 1 }, 700);?

CSS

CSS

#test p {
    opacity: 0;
    font-size: 21px;
    margin-top: 25px;
    text-align: center;
}

Demo

演示

Browser Support

浏览器支持

jQuery 1.x: All modern browsers and Internet Explorer 6 (and later): http://jquery.com/browser-support/
jQuery 2.x: All modern browsers and Internet Explorer 9 (and later): http://jquery.com/browser-support/

jQuery 1.x:所有现代浏览器和 Internet Explorer 6(及更高版本):http: //jquery.com/browser-support/
jQuery 2.x:所有现代浏览器和 Internet Explorer 9(及更高版本):http:// jquery.com/browser-support/

This method is the most cross-compatible as the target browser does not need to support CSS 3 transitions oranimations.

这种方法是最具交叉兼容性的,因为目标浏览器不需要支持 CSS 3 过渡动画。

回答by Ned

You can use the onload=""HTML attribute and use JavaScript to adjust the opacity style of your element.

您可以使用onload=""HTML 属性并使用 JavaScript 来调整元素的不透明度样式。

Leave your CSS as you proposed. Edit your HTML code to:

按照您的建议保留您的 CSS。将您的 HTML 代码编辑为:

<body onload="document.getElementById(test).style.opacity='1'">
    <div id="test">
        <p>?This is a test</p>
    </div>
</body>

This also works to fade-in the complete page when finished loading:

这也适用于在完成加载后淡入整个页面:

HTML:

HTML:

<body onload="document.body.style.opacity='1'">
</body>

CSS:

CSS:

body{ 
    opacity: 0;
    transition: opacity 2s;
    -webkit-transition: opacity 2s; /* Safari */
}

Check the W3Schoolswebsite: transitionsand an article for changing styles with JavaScript.

查看W3Schools网站:transitions和一篇使用 JavaScript 更改样式的文章。

回答by Rob

In response to @A.M.K's question about how to do transitions without jQuery. A very simple example I threw together. If I had time to think this through some more, I might be able to eliminate the JavaScript code altogether:

回应@AMK 关于如何在没有 jQuery 的情况下进行转换的问题。我拼凑了一个非常简单的例子。如果我有时间再考虑一下,我也许可以完全消除 JavaScript 代码:

<style>
    body {
        background-color: red;
        transition: background-color 2s ease-in;
    }
</style>

<script>
    window.onload = function() {
        document.body.style.backgroundColor = '#00f';
    }
</script>

<body>
    <p>test</p>
</body>