简体   繁体   English

通过JavaScript重新启动CSS动画

[英]Restarting CSS animation via JavaScript

I went through a couple of articles and stack questions for a solution, but I haven't been able to get this working. 我浏览了几篇文章,并提出了解决方案的问题,但我一直无法解决这个问题。 I'm trying to restart this animation upon button click so I can get a better idea of how to approach css animation in my web app. 我试图在单击按钮时重新启动此动画,以便我可以更好地了解如何在Web应用程序中处理CSS动画。 Unfortunately, it seems a lot harder than I initially thought it be with all the cross browser compatibility issues with some code. 不幸的是,这似乎比起初我认为的要难一些代码的所有跨浏览器兼容性问题都要困难。

HTML HTML

<img src="http://s.cdpn.io/79/sprite-steps.png" />
<div class="hi"></div>

<input type = "button" id = "restart" value = "Restart" /> 

JavaScript JavaScript的

var $ = function(id){
    return document.getElementById(id);
}

$("restart").addEventListener("click", function (e){
    $("div").classList.remove("hi");
    $("div").offsetWidth = $("div").offsetWidth;
    $("div").classList.add("hi")
;}, false);

CSS CSS

.hi {
    width: 50px;
    height: 72px;
    background-image: url("http://s.cdpn.io/79/sprite-steps.png");

    -webkit-animation: play .8s steps(10) 1;
       -moz-animation: play .8s steps(10) 1;
        -ms-animation: play .8s steps(10) 1;
         -o-animation: play .8s steps(10) 1;
            animation: play .8s steps(10) 1;
}

@-webkit-keyframes play {
   from { background-position:    0px; }
     to { background-position: -500px; }
}

@-moz-keyframes play {
   from { background-position:    0px; }
     to { background-position: -500px; }
}

@-ms-keyframes play {
   from { background-position:    0px; }
     to { background-position: -500px; }
}

@-o-keyframes play {
   from { background-position:    0px; }
     to { background-position: -500px; }
}

@keyframes play {
   from { background-position:    0px; }
     to { background-position: -500px; }
}

This is the JSFiddle link. 这是JSFiddle链接。 I added and changed some stuff to what it originally had, but credits out to the original coder(s) always. 我添加了一些内容并将其更改为原始内容,但始终记入原始编码器中。 JQuery answers are appreciated, but I would really like an answer in vanilla JavaScript. jQuery的答案是值得赞赏的,但我真的很想用普通JavaScript给出答案。 Thanks! 谢谢!

http://jsfiddle.net/CGmCe/11555/ http://jsfiddle.net/CGmCe/11555/

Based on the return of your function, return document.getElementById(id); 根据函数的返回值, return document.getElementById(id); your div is missing an id 您的div缺少id

<div id="div" class="hi"></div>

http://jsfiddle.net/f2Lb3x5f/ http://jsfiddle.net/f2Lb3x5f/

Vanilla Javascript: 香草Javascript:

function hasClass(element, myClass) {
  return !!element.className.match(new RegExp('(\\s|^)'+myClass+'(\\s|$)'));
}

function addClass(element, myClass) {
  if (!hasClass(element , myClass)) element.className += " "+myClass;
}

function removeClass(element,myClass) {
  if (hasClass(element, myClass)) {
    var reg = new RegExp('(\\s|^)'+myClass+'(\\s|$)');
    element.className=element.className.replace(reg,'');
  }
}

this can help: 这可以帮助:

your html with addtion of id to that div: 您的html加上id到该div:

<img src="http://s.cdpn.io/79/sprite-steps.png" />
<div class="hi" id="animate"></div>

<input type = "button" id = "restart" value = "Restart" />

javascript: JavaScript的:

element = document.getElementById("restart");
element1 = document.getElementById("animate");

// reset the transition by...
element.addEventListener("click", function(e) {
  e.preventDefault;

  // -> removing the class
  element1.classList.remove("hi");

  // -> triggering reflow /* The actual magic */
  // without this it wouldn't work. Try uncommenting the line and the transition won't be retriggered.
  element1.offsetWidth = element1.offsetWidth;

  // -> and re-adding the class
  element1.classList.add("hi");
}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM