如何在JavaScript中更改动画计时功能?

时间:2013-06-25 11:52:26

标签: javascript html css animation

我在CSS中有一个动画,它围绕着它的中心旋转图像。我希望能够在JavaScript中以编程方式更改animation-timing-function和-webkit-animation-timing-function等。这是代码和我尝试过的内容:

  <div id="content"> <div id="container"> 
<div id="outerQ"></div> <div id="innerQ">
</div>
 </div>
 <div id="logo"></div> 
<span id="sp"></span> 
<input type="button" id="linear" value="Linear"/>
 <input type="button" id="ease" value="Ease in and out"/>
 </div>

脚本:

<head>
    <script src="http://code.jquery.com/jquery-2.0.1.min.js"></script>
<script>
        $(document).ready(function ()
        {
            $("#linear").click(function ()
            {
                $("#innerQ").css("-webkit-animation-timing-function", "linear");
            });

            $("#ease").click(function ()
            {
                $("#innerQ").css("-webkit-animation-timing-function", "ease-in-out");
            });
        });
    </script>

CSS:

 <style>
            body
            {
                background: #018a9a;
                margin: 20px;
            }

            #content
            {
                width: 566px;
                height: 120px;
                position: fixed;
                top: 50%;
                left: 50%;
                margin-top: -80px;
                margin-left: -278px;
            }

            #outerQ
            {
                width: 96px;
                height: 120px;
                background-image: url('outerQ.png');
            }

            #innerQ
            {
                width: 96px;
                height: 120px;
                background-image: url('innerQ.png');
                position: absolute;
                left: 0;
                top: 0;
            }

            #container
            {
                width: 96px;
                height: 120px;
                float: left;
            }

            #logo
            {
                width: 470px;
                height: 120px;
                background-image: url('LogoLarge.png');
                float: left;
            }

            #sp
            {
                clear: both;
            }

            @keyframes spin
            {
                from {
                    transform: rotate(0deg);
                }
                to {
                    transform: rotate(360deg);
                }
            }

            @-webkit-keyframes spin
            {
                from {
                    -webkit-transform: rotate(0deg);
                }
                to {
                    -webkit-transform: rotate(360deg);
                }
            }

            @-moz-keyframes spin
            {
                from {
                    -moz-transform: rotate(0deg);
                }
                to {
                    -moz-transform: rotate(360deg);
                }
            }

            #innerQ
            {
                /*IE 10*/
                animation-name: spin;
                animation-iteration-count: infinite;
                animation-timing-function: linear;
                animation-duration: 1.3s;
                animation-play-state: running;
                transform-origin: 48px 50.5px;

                /*Chrome*/
                -webkit-animation-name: spin;
                -webkit-animation-iteration-count: 1;
                -webkit-animation-timing-function: linear;
                -webkit-animation-duration: 1.3s;
                -webkit-animation-play-state: running;
                -webkit-transform-origin: 48px 50px;

                /*Firefox*/
                -moz-animation-name: spin;
                -moz-animation-iteration-count: infinite;
                -moz-animation-timing-function: linear;
                -moz-animation-duration: 1.3s;
                -moz-animation-play-state: running;
                -moz-transform-origin: 48px 50px;
            }
        </style>
</head>
<body>

HTML:                                                                                               

        <input type="button" id="linear" value="Linear"/>
        <input type="button" id="ease" value="Ease in and out"/>
    </div>


</body>

我在这里使用webkit因为我使用Chrome,点击按钮什么都不做。我也想改变'animation-iteration-count'和'animation-duration',但这些都不起作用。唯一有效的是'-webkit-animation-play-state'。我也尝试过:

$("#innerQ").style.WebkitAnimationTimingFunction = "ease-in-out";

这也行不通。有没有办法在页面加载后更改这些属性?感谢

1 个答案:

答案 0 :(得分:1)

function function_name() {
    document.getElementById('innerQ"').className = "innerQ";
}

CSS

.innerQ
        {
           //you css code(put all timing stuff here!!!)
        }

//Step 1--->Put you desired css animation in a css class
//Step 2--->add that css class with javascript(className)
//Step 3---->Call function animation on click of the button