我有一个这样的css:

#turbo_fire {
  transform-box: fill-box;
  position: absolute;
  animation-name: burning;
  animation-duration: .005s;
  animation-direction: alternate;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  transform-origin: center center;
}
.burning-pause {
  animation-play-state: paused;
}
.burning-run {
  animation-play-state: running;
}

我还有我的JS代码,它可以监听ckick事件,并在.burning-pause.burning-run类之间切换.

animation-play-stateprops 只有两种状态,如pauserunning.所以它没有任何停止动画值.

因此,问题是如何在click帧时停止动画,并将其返回到第一帧?

推荐答案

There is no real problem for resetting an animation on first position.
I'm wondering in which kind of confusion you set yourself,
-because this is not a problem,
-and you didn't take any second for answering my comment; even just a 'no'.

const btn = 
  { Xanim  : document.querySelector('#btn-anim')
  , Xpause : document.querySelector('#btn-pause')
  , Xrun   : document.querySelector('#btn-run')
  , Xreset : document.querySelector('#btn-reset')
  }

setBtnsOff( false,true,true,true );

btn.Xanim.onclick =_=> 
  { 
  bob.classList.add('anim');
  setBtnsOff( true,false,true,false );
  }
btn.Xpause.onclick =_=>
  {
  bob.classList.remove('movin-run'); 
  bob.classList.add('movin-pause');
  setBtnsOff(true,true,false,false);
  }
btn.Xrun.onclick =_=>
  {
  bob.classList.replace('movin-pause','movin-run'); 
  setBtnsOff(true,false,true,false);
  }
btn.Xreset.onclick =_=>
  {
  bob.classList.remove('anim','movin-pause','movin-run');
  setBtnsOff( false,true,true,true );
  }
 
function setBtnsOff( x_anim, x_pause, x_run, x_reset  )
  {
  btn.Xanim .disabled = x_anim;
  btn.Xpause.disabled = x_pause;
  btn.Xrun  .disabled = x_run;
  btn.Xreset.disabled = x_reset;
  }
#bob {
  margin  : 20px;
  width         : 100px;
  height        : 100px;
  background    : darkorange;
  border-radius : 50px;
  }
.anim {
  animation-name            : movin;
  animation-duration        : 2s;
  animation-timing-function : linear;
  animation-iteration-count : infinite;
  }
@keyframes movin {
    0% { transform: translateX(0); }
   50% { transform: translateX(300px); }
  100% { transform: translateX(0); }
  }

.movin-pause {
  animation-play-state: paused;
  }
.movin-run {
  animation-play-state: running;
  }

button { width : 5em; }
<button id="btn-anim"  > mov it </button>
<button id="btn-pause" > pause  </button>
<button id="btn-run"   > run    </button>
<button id="btn-reset" > reset  </button>

<div id="bob"></div>

Css相关问答推荐

实现前景渐变到背景的平滑混合

使用间距时奇怪的MUI网格行为

将CSS栅格行高设置为其余视口高度

位置:固定元素只是不固定

不明白为什么 div 不均匀并且不填满页面

为什么我的 React slick 轮播响应能力不起作用?

使用文本溢出约束内部 div:没有最大宽度的省略号(或类似的)

如何在Bootstrap 5上使用移动设备居中内容?

解释 "document.styleSheets[0].cssRules[0].style;"

如何在变量中插入 SCSS 变量?

Flex & space-around 与内容相交

tailwind 类的别名?

使用 styled-components,我如何定位组件的子类?

为什么我的 CSS 需要一个它似乎不需要的大括号?

你如何调试可打印的 CSS?

Bootstrap 4 img-circle 类似乎不存在

填充剩余的垂直空间 - 仅 CSS

范围内的 CSS 未在组件中应用

在 CSS 中结合border-top、border-right、border-left、border-bottom

如何在页面上居中 twitter bootstrap 选项卡?