javascript - 如何更改 JavaScript 中的动画计时功能?

标签 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";

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

最佳答案

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

关于javascript - 如何更改 JavaScript 中的动画计时功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17296583/

相关文章:

javascript - 如何让 Discord 机器人检查它是否正在被私信,然后使用 Discord.js 回复所述私信?

javascript - 点击另一个div时如何更改div样式

html - 两个内联 div 中的内容对齐问题?

CSS - 表格单元格布局在 Firefox/IE 中超出高度

html - CSS 复选框样式

javascript - Google map 信息窗口左箭头、右箭头传递 TypeError : Cannot read property '__e3_' of undefined?

javascript - 使用 React 和 Node js 的电子邮件发送应用程序

html - Mac 上浏览器之间的字体大小不一致

javascript - 如何将事件与选择框的选项 ID 相关联

JavaScript 无法验证 HTML 中的表单