有人可以解释为什么这些 webkit 动画功能之一起作用而另一个不起作用。我知道它是 class vs ID,但为什么呢?

Can someone please explain why one of these webkit animation functions works and the other doesn't. I know its the class vs ID but why?

这只是测试代码所以不要考虑太多。但无论如何,我无法理解为什么动画播放状态在使用 class 时可以正常工作并且可以正常更改,但在我使用 ID 时却不能。

使用ID有没有其他规定或者不能使用ID?

任何信息都会很有帮助。

工作版本使用 类

<html>
<head>
<style>
div {
    width: 100px;
    height: 100px;
    background: red;
    position: relative;
    -webkit-animation: mymove 2s infinite;  /* Chrome, Safari, Opera */
    -webkit-animation-play-state: paused;  /* Chrome, Safari, Opera */
    animation: mymove 2s infinite;
    animation-play-state: paused;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes mymove {
    from {left: 0px;}
    to {left: 250px;}
}

@keyframes mymove {
    from {left: 0px;}
    to {left: 250px;}
}
</style>
</head>
<body>
<button onclick="playTest()">Play</button>
<button onclick="playTest2()">Pause</button>

<script>

function playTest(){
    document.getElementById("notMyDIV").style.WebkitAnimationPlayState = "running"; // Code for Chrome, Safari, and Opera
    document.getElementById("notMyDIV").style.animationPlayState = "running";
}

function playTest2(){
    document.getElementById("notMyDIV").style.WebkitAnimationPlayState = "paused"; // Code for Chrome, Safari, and Opera
    document.getElementById("notMyDIV").style.animationPlayState = "paused";
}


</script>
<div id="notMyDIV">
  <div id="myDIV"></div>
</div>
</body>
</html>

非工作 ID 版本

<html>
<head>
<style>
.myDiv {
    width: 100px;
    height: 100px;
    background: red;
    position: relative;
    -webkit-animation: mymove 2s infinite;  /* Chrome, Safari, Opera */
    -webkit-animation-play-state: paused;  /* Chrome, Safari, Opera */
    animation: mymove 2s infinite;
    animation-play-state: paused;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes mymove {
    from {left: 0px;}
    to {left: 250px;}
}

@keyframes mymove {
    from {left: 0px;}
    to {left: 250px;}
}
</style>
</head>
<body>
<button onclick="playTest()">Play</button>
<button onclick="playTest2()">Pause</button>

<script>

function playTest(){
    document.getElementById("notMyDIV").style.WebkitAnimationPlayState = "running"; // Code for Chrome, Safari, and Opera
    document.getElementById("notMyDIV").style.animationPlayState = "running";
}

function playTest2(){
    document.getElementById("notMyDIV").style.WebkitAnimationPlayState = "paused"; // Code for Chrome, Safari, and Opera
    document.getElementById("notMyDIV").style.animationPlayState = "paused";
}


</script>
<div id="notMyDIV">
  <div class="myDiv" id="myDIV"></div>
</div>
</body>
</html>

这个问题不是因为 div 是否有 class。第二个例子失败的原因是 CSS 选择器 (.myDiv) 没有选择 div#notMyDiv,而是选择 child div.myDiv,未应用动画播放状态。

在第一个 div 中,选择器选择了所有 divs (div),这就是它起作用的原因。

这是第二个片段,除了 javascript 将播放状态应用于 child div,由 CSS 选择器选择。

<html>
  <head>
    <style>
    .myDiv {
      width: 100px;
      height: 100px;
      background: red;
      position: relative;
      -webkit-animation: mymove 2s infinite;  /* Chrome, Safari, Opera */
      -webkit-animation-play-state: paused;  /* Chrome, Safari, Opera */
      animation: mymove 2s infinite;
      animation-play-state: paused;
    }

    /* Chrome, Safari, Opera */
    @-webkit-keyframes mymove {
      from {left: 0px;}
      to {left: 250px;}
    }

    @keyframes mymove {
      from {left: 0px;}
      to {left: 250px;}
    }
    </style>
  </head>
  <body>
    <button onclick="playTest()">Play</button>
    <button onclick="playTest2()">Pause</button>
    <script>
    function playTest(){
      document.getElementById("myDIV").style.WebkitAnimationPlayState = "running"; // Code for Chrome, Safari, and Opera
      document.getElementById("myDIV").style.animationPlayState = "running";
    }

    function playTest2(){
      document.getElementById("myDIV").style.WebkitAnimationPlayState = "paused"; // Code for Chrome, Safari, and Opera
      document.getElementById("myDIV").style.animationPlayState = "paused";
    }
    </script>
    <div id="notMyDIV">
      <div class="myDiv" id="myDIV"></div>
    </div>
  </body>
</html>