zoukankan      html  css  js  c++  java
  • jQuer配合CSS3实现背景图片动画

    HTML部分:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>displayOrder</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    </head>
    <body>
    <div id="topDiv">
        <!-- 顶部图片 -->
        <div class="_img" >
            <img src="./images/img_title.png" alt="图片">
        </div>
    </div>
    </body>
    </html>

    css部分:

    /*--begin--*/
    ._topDiv0{
        width: 100%;
        background: url("../images/bg_img.png") no-repeat 0 0;
        background-size: 100%;
        padding-top: 30px;
        position: relative;
        animation: mymove 8s infinite;
        -webkit-animation: mymove 8s infinite;
        /*Safari and Chrome*/
    }
    ._topDiv1{
        width: 100%;
        background: url("../images/bg_img.png") no-repeat 0 0;
        background-size: 100%;
        padding-top: 30px;
        position: relative;
        animation: mymove1 8s infinite;
        -webkit-animation: mymove1 8s infinite;
        /*Safari and Chrome*/
    }
    
    
    /*CSS3 定义动画 Internet Explorer 9 以及更早的版本不支持 animation 属性*/
    @keyframes mymove {
        from {background: url("../images/bg_img.png") no-repeat right top;}
        to {background: url("../images/bg_img.png") no-repeat left top;}
    }
    /*Safari and Chrome*/
    @-webkit-keyframes mymove {
        from {background: url("../images/bg_img.png") no-repeat right top;}
        to {background: url("../images/bg_img.png") no-repeat left top;}
    }
    
    @keyframes mymove1 {
        from {background: url("../images/bg_img.png") no-repeat left top;}
        to {background: url("../images/bg_img.png") no-repeat right top;}
    }
    /*Safari and Chrome*/
    @-webkit-keyframes mymove1 {
        from {background: url("../images/bg_img.png") no-repeat left top;}
        to {background: url("../images/bg_img.png") no-repeat right top;}
    }
    
    
    ._img{
        width: 100%;
        height: auto;
    }
    ._img>img{
        width: 100%;
        height: auto;
    }

    jQuery部分:

    var direction = 0;// 方向标识,0代表向左,1代表向右。
        //动画执行
        function animationChange() {
            $("#topDiv").removeClass("_topDiv"+direction);
            switch (direction){
                case 0:direction=1;
                break;
                default:direction=0;
            }
            $("#topDiv").addClass("_topDiv"+direction);
            window.setTimeout(animationChange,7500);
        }
        animationChange();

    注:本例子没有提供jQuery文件,需要另外引入jQuery文件才能看效果。

    另:CSS3 定义动画 Internet Explorer 9 以及更早的版本不支持 animation 属性

    如有什么问题或者更好的建议,诚挚欢迎提出来,本人将虚心接受。

    谢谢阅读!

  • 相关阅读:
    Will Go eventually replace C++ as Google hoped when Go came out?
    5G到底什么时候来,它究竟能给我们带来什么?
    eog——Eye of GNOME Image Viewer
    Appweb——Embedded Web Server
    【2017】数字重排
    【9203】众数
    【2034】四人投票
    【9204】第k小整数
    【2031】求一元三次方程的解
    iOS 7: 如何为iPhone 5s编译64位应用
  • 原文地址:https://www.cnblogs.com/kingchan/p/7170788.html
Copyright © 2011-2022 走看看