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

如何在加载时触发转换?

推荐答案

方法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

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

演示

浏览器支持

All modern browsers and Internet Explorer 10 (and later): 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";

演示

浏览器支持

All modern browsers and Internet Explorer 10 (and later): 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.

Css相关问答推荐

在Angular mat-drawer组件中防止在mat-drawer-innercontainer中添加滚动条

日期面板未与Angular中的日期控件对齐

不了解重复-线性-渐变 colored颜色 停止

如何为多个背景和棋盘设置不同的大小

设置ScaleImageButton的样式

输入中的框始终显示在MUI文本字段中

臭虫?嵌套的css供应商前缀中断Chrome css解析

Prettier/VS Code 在 CSS 中的 !important 之前添加一个空格,导致它在 WordPress 中崩溃

如何向 Highcharts 迷你图添加具有淡入淡出效果的线性渐变?

有没有办法将px单位添加到 Sass mixin 的参数数组中?

如何更改导航栏标题中链接的文本 colored颜色 和导航丸中的链接(在shiny 的应用程序中)?

如何在相对位置和绝对位置之间转换元素?

我怎样才能将我的按钮向上移动,因为文本是

如何定位 CSS 网格布局中的特定列或行?

纯 CSS 复选框图像替换

水平列表项

使用 CSS 强制侧边栏高度 100%(带有粘性底部图像)?

使用自定义 CSS 在 WebView 中呈现 HTML

使用没有设置宽度或高度的边界半径的胶囊形状?

如何实现最大字体大小?