【发布时间】:2021-03-09 07:33:36
【问题描述】:
我希望我的无限 svg 动画在悬停时暂停在其初始或最终状态。我尝试使用 JS 删除类或在 CSS 中将animation-iteration-count: unset; 设置为悬停或设置animation: none;,但在每种情况下,转换都是突然的。我希望它顺利。这是代码:
HTML
<div class="button">
<svg version="1.1" id="sound--icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 100 100" >
<rect class="rect-1 rect-anim" x="10" fill="currentColor"></rect>
<rect class="rect-2 rect-anim" x="30" fill="currentColor"></rect>
<rect class="rect-3 rect-anim" x="50" fill="currentColor"></rect>
<rect class="rect-4 rect-anim" x="70" fill="currentColor"></rect>
</svg>
</div>
CSS
#sound--icon .rect-anim{
animation-name: sound-icon-anim;
animation-duration: .8s;
animation-timing-function: cubic-bezier(.97,0,0,1);
animation-iteration-count: infinite;
}
/* #sound--icon:hover .rect-anim{
animation-iteration-count: unset;
}
*/
#sound--icon .rect-1{
animation-delay: .1s;
}
#sound--icon .rect-2{
animation-delay: .3s;
}
#sound--icon .rect-3{
animation-delay: .5s;
}
#sound--icon .rect-4{
animation-delay: .7s;
}
@keyframes sound-icon-anim{
0%{
height: 30%;
}
10%{
height: 70%;
}
20%{
height: 50%;
}
50%{
height: 80%;
}
60%{
height: 100%;
}
70%{
height: 70%;
}
100%{
height: 30%;
}
}
我最终的目标是:
- 动画正在运行
- 当 div 悬停/动画暂停时,高度平滑地达到 50%
- 当点击 div/动画暂停时,高度平滑地达到 30%
- 当 div 再次悬停/动画仍然暂停时,高度平滑地达到 50%
- 再次单击 div 时动画会重新运行(因此从 50% 开始,而不是从 0% 动画状态状态的 30% 开始)
在纯 CSS 中这对您来说是否可行?
【问题讨论】:
标签: css animation svg hover css-transitions