CSS3:前端三种方法实现页面加载时动画效果

方法 一:

如果你想寻找一种自己调用的方法,可以选用CSS animation。

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案例

浏览器支持情况

所有的现代浏览器,包括IE10以上: 参考网址

 

方法二:

或者,你可以使用jQuery(或者普通JS,参见第三个代码块)在加载时更改class:

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

原生JS:

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

Demo案例

浏览器支持情况

所有的现代浏览器,包括IE10以上: 参考网址

 

Method 3:

或者可以使用jQuery的animate方法

jQuery

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

CSS

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

Demo

浏览器支持情况

jQuery 1.x: 所有的现代浏览器,IE6以上: 参考网址

jQuery 2.x: 所有的现在浏览器,IE 9以上: 参考网址

由于目标浏览器不需要支持CSS3 transitionanimation,因此该方法是最具交叉兼容性的。

(0)
上一篇 2019年3月1日 下午11:48
下一篇 2019年3月4日 下午10:52

相关推荐

发表评论

登录后才能评论