悬停并播放一次动画而不是循环播放

hover and play animation once instead of looping

如果您查看我的代码并 运行 它。

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

@-webkit-keyframes inner-circle {
    from { transform:rotate(0deg); }
    to { transform:rotate(-180deg); }
}

#one {
    position: absolute;
    left: 500px;
    top: 200px;
    width:100px;
    height:100px;
    border-radius: 50px;
    background-color: #000000;
}

#two {
    width:100px;
    height:100px;
    margin: 0px auto 0;
    color:orange;
    font-size:100px;
    line-height:1;

    transform-origin:50% 200px;
}


 #one:hover > div {
     animation: circle 1s linear infinite;
    -webkit-animation: circle 1s linear infinite;
 }
#one:hover > div > div {
     animation: inner-circle 1s linear infinite;
    -webkit-animation: inner-circle 1s linear infinite;
 }
</style>

    <div id="one">
        <div id="two"><div id="three">☻</div></div>
    </div>

你会注意到笑脸一直在循环旋转180度的动画。我不想要这个。每次我将鼠标悬停在黑色圆圈上时,我只希望它做一次动画。我该怎么做?

如果不希望动画无限出现,请从动画中移除infinite shorthand。

此外,您还需要添加 forwards 以便 prevent the animation from resetting each time. When adding forwards to the animation shorthand, you are essentially changing the property animation-fill-mode 从默认值 none 变为 forwards

From MDN:

The animation-fill-mode CSS property specifies how a CSS animation should apply styles to its target before and after it is executing.

#one:hover > div {
    animation: circle 1s linear forwards;
    -webkit-animation: circle 1s linear forwards;
}
#one:hover > div > div {
    animation: inner-circle 1s linear forwards;
    -webkit-animation: inner-circle 1s linear forwards;
}

将所有无限值更改为您希望动画循环的次数。在您的情况下,它将是一次,因此您想将无限更改为 1。