使用CSS对页面加载产生淡入效果

CSS

乐米亚

2020-03-19

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

我真的很喜欢它在http://dotmailapp.com/上的外观,并希望通过CSS使用类似的效果。此域已购买,不再具有上述作用。可以在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;
}​

如何在负载上触发转换?

第2263篇《使用CSS对页面加载产生淡入效果》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
阿飞神无 2020.03.19

方法1:

如果您正在寻找一个自我调用的过渡,那么您应该使用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


方法2:

另外,您可以使用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


方法3:

或者,您可以使用.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: 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/

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android