【问题标题】:Changing css animation in javascript在javascript中更改css动画
【发布时间】:2015-03-31 18:40:51
【问题描述】:

我的 html 中有一个闪烁的红色框,它使用 css 动画。我希望能够将它从闪烁的红色和白色变为绿色和白色。我知道这可以通过使用 getElementbyId 在 id 元素上完成,但是如何访问 css 中的绿色氨基化框。 红框是这样的:

@-webkit-keyframes 'noConnection' 
{
    1% { background-color: red; }
    33% { background: white; }
    66% { background: red; }
    100% { background: white; }
}

绿色是这样的:

@-webkit-keyframes 'Connection' 
{
    1% { background-color: green; }
    33% { background: white; }
    66% { background: green; }
    100% { background: white; }
}

动画如下所示:

#animate { 
    height: 15px; 
    width: 15px;
}
.cssanimations #animate {
    -webkit-animation-direction: normal;
    -webkit-animation-duration: 5s;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-name: Connection;
    -webkit-animation-timing-function: ease;   

我认为我必须从 javascript 更改属性 -webkit-animation-name: 才能执行此操作,但我不知道如何处理它来更改它。 还是创建一个重复的 #animate 并使用 getElementById 重命名它会更好?

【问题讨论】:

  • 也许$('#animate').css('-webkit-animation-name','noConnection');(因为您使用的是jQuery)有效?不过不确定。
  • 效果很好

标签: javascript jquery html css animation


【解决方案1】:

最简单的方法是在 CSS 中创建两个类,然后在两者之间切换。

比如:

.connection {
  animation: 'Connection' 5s ease infinite
}


.no-connection {
  animation 'noConnection' 5s ease infinite
}

然后使用Javascript在两者之间切换

function toggleConnectionStatus(){
 var element = document.getElementById('animate');
 if(/* connection is active */) element.className = 'connection';
 else element.className = 'no-connection'

}

【讨论】:

    【解决方案2】:

    这是一个简单的网页,演示了如何使用 Javascript 来修改 CSS 动画。它包含一个简单的 div,一点点 Javascript 使 div 在页面中随机移动。

    在您的具体情况下,您只需修改调用“insertRule”的行以插入新的闪烁规则。

    <html><head><style></style></head>
    <body>
        <div id="mymovingdiv">
            <h1>Moving Div</h1>
            <p>And some little text too</p>
        </div>
    
    <script>
    function animatedMove(id, xStart, yStart, xEnd, yEnd, secs)
    {
        // Remove any CSS rules inserted by a previous call to this method
        let rulename = `AnimMove${id}`;
        let ss = document.styleSheets; // all stylesheets
        for (let i = 0; i < ss.length; ++i) { // for each stylesheet...
            for (let j = ss[i].cssRules.length - 1; j > 0; j--) { // for each rule...
                if (ss[i].cssRules[j].name === rulename) { // does the name match?
                    ss[i].deleteRule(j);
                }
            }
        }
    
        // Insert a CSS rule for this animation
        document.styleSheets[0].insertRule(`@keyframes ${rulename} { 0% { left: ${xStart}px; top: ${yStart}px; } 100% { left: ${xEnd}px; top: ${yEnd}px } }`);
    
        // Remove any CSS rules inserted by a previous call to this method
        for (let i = 0; i < ss.length; ++i) { // for each stylesheet...
            for (let j = ss[i].cssRules.length - 1; j > 0; j--) { // for each rule...
                if (ss[i].cssRules[j].name === rulename) { // does the name match?
                    ss[i].deleteRule(j);
                }
            }
        }
    
        // Insert a CSS rule for this animation
        document.styleSheets[0].insertRule(`@keyframes ${rulename} { 0% { left: ${xStart}px; top: ${yStart}px; } 100% { left: ${xEnd}px; top: ${yEnd}px } }`);
    
        // assign the animation to our element
        let el = document.getElementById(id);
        el.style.position = 'absolute';
        el.style.animation = `${rulename} ${secs}s`;
    
        // Make the element stay where the animation ends
        el.style.left = `${xEnd}px`;
        el.style.top = `${yEnd}px`;
    
        // Re-clone the element, to reset the animation
        el.parentNode.replaceChild(el.cloneNode(true), el);
    }
    
    let x = 0;
    let y = 0;
    
    function randomMove()
    {
        let newX = Math.floor(Math.random() * 800);
        let newY = Math.floor(Math.random() * 600);
        animatedMove('mymovingdiv', x, y, newX, newY, 1);
        x = newX;
        y = newY;
    }
    
    setInterval(randomMove, 1000);
    </script>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2013-11-28
      • 2021-11-10
      • 2014-05-30
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多