在最后一帧停止 CSS3 动画

发布于 2024-10-05 18:41:56 字数 383 浏览 9 评论 0原文

我有一个 4 部分的 CSS3 动画,点击时播放 - 但动画的最后一部分是为了将其从屏幕上移开。

然而,一旦播放完毕,它总是会回到原来的状态。任何人都知道我如何在最后一个CSS帧(100%)上停止它,或者如何在播放后删除它所在的整个div。

@keyframes colorchange {
  0%   { transform: scale(1.0) rotate(0deg); }
  50%  { transform: rotate(340deg) translate(-300px,0px) }
  100% { transform: scale(0.5) rotate(5deg) translate(1140px,-137px); }
}

I have a 4 part CSS3 animation playing on click - but the last part of the animation is meant to take it off the screen.

However, it always goes back to its original state once it has played. Anyone know how I can stop it on its last css frame (100%), or else how to get rid of the whole div it is in once it has played.

@keyframes colorchange {
  0%   { transform: scale(1.0) rotate(0deg); }
  50%  { transform: rotate(340deg) translate(-300px,0px) }
  100% { transform: scale(0.5) rotate(5deg) translate(1140px,-137px); }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

回忆那么伤 2024-10-12 18:41:56

您正在寻找:

animation-fill-mode: forwards;

有关 MDN 的更多信息canIuse 上的浏览​​器支持列表。

You're looking for:

animation-fill-mode: forwards;

More info on MDN and browser support list on canIuse.

你怎么这么可爱啊 2024-10-12 18:41:56

如果您想将此行为添加到速记 animation 属性定义中,子属性的顺序如下

animation-name - default <代码>无

动画持续时间 - 默认 0s

动画计时功能 - 默认 轻松

动画延迟 - 默认 0s

动画迭代计数 - 默认 1

动画方向 - 默认 正常

动画-fill-mode - 您需要将其设置为 forwards

animation-play-state - 默认 running

因此,在最常见的情况下,结果将是这样的

animation: colorchange 1s ease 0s 1 normal forwards;

请参阅MDN 文档位于此处

If you want to add this behaviour to a shorthand animation property definition, the order of sub-properties is as follows

animation-name - default none

animation-duration - default 0s

animation-timing-function - default ease

animation-delay - default 0s

animation-iteration-count - default 1

animation-direction - default normal

animation-fill-mode - you need to set this to forwards

animation-play-state - default running

Therefore in the most common case, the result will be something like this

animation: colorchange 1s ease 0s 1 normal forwards;

See the MDN documentation here

苏璃陌 2024-10-12 18:41:56
-webkit-animation-fill-mode:转发; /* Safari 4.0 - 8.0 */
动画填充模式:向前;

浏览器支持

  • Chrome 43.0 (4.0 -webkit-)
  • IE 10.0
  • Mozilla 16.0 ( 5.0 -moz-)
  • Shafari 4.0 -webkit-
  • Opera 15.0 -webkit- (12.112.0 -o -)

用法:-

.fadeIn {
  animation-name: fadeIn;
    -webkit-animation-name: fadeIn;

    animation-duration: 1.5s;
    -webkit-animation-duration: 1.5s;

    animation-timing-function: ease;
    -webkit-animation-timing-function: ease;

     animation-fill-mode: forwards;
    -webkit-animation-fill-mode: forwards;
}


@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}
-webkit-animation-fill-mode: forwards; /* Safari 4.0 - 8.0 */
animation-fill-mode: forwards;

Browser Support

  • Chrome 43.0 (4.0 -webkit-)
  • IE 10.0
  • Mozilla 16.0 ( 5.0 -moz-)
  • Shafari 4.0 -webkit-
  • Opera 15.0 -webkit- (12.112.0 -o-)

Usage:-

.fadeIn {
  animation-name: fadeIn;
    -webkit-animation-name: fadeIn;

    animation-duration: 1.5s;
    -webkit-animation-duration: 1.5s;

    animation-timing-function: ease;
    -webkit-animation-timing-function: ease;

     animation-fill-mode: forwards;
    -webkit-animation-fill-mode: forwards;
}


@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}
半仙 2024-10-12 18:41:56

最好的方法似乎是将最终状态放在 css 的主要部分。像这里一样,我将宽度设置为220px,这样它最终就变成了220px。但开始0px;

div.menu-item1 {
  font-size: 20px;
  border: 2px solid #fff;
  width: 220px;
  animation: slide 1s;
  -webkit-animation: slide 1s; /* Safari and Chrome */
}
@-webkit-keyframes slide { /* Safari and Chrome */
  from {width:0px;}
  to {width:220px;}
}  

The best way seems to put the final state at the main part of css. Like here, i put width to 220px, so that it finally becomes 220px. But starting to 0px;

div.menu-item1 {
  font-size: 20px;
  border: 2px solid #fff;
  width: 220px;
  animation: slide 1s;
  -webkit-animation: slide 1s; /* Safari and Chrome */
}
@-webkit-keyframes slide { /* Safari and Chrome */
  from {width:0px;}
  to {width:220px;}
}  
转瞬即逝 2024-10-12 18:41:56

您的问题不是将 webkitAnimationName 设置为空,从而将对象的 CSS 重置回默认状态。如果您只是删除重置状态的 setTimeout 函数,它不会停留在最终位置吗?

Isn't your issue that you're setting the webkitAnimationName back to nothing so that's resetting the CSS for your object back to it's default state. Won't it stay where it ended up if you just remove the setTimeout function that's resetting the state?

二货你真萌 2024-10-12 18:41:56

我刚刚发布了类似的答案,您可能想看看:

http://www.w3.org/TR/css3-animations/#animation-events-

您可以了解动画的各个方面,例如开始和停止,然后说出“停止”事件被解雇了你可以对dom做任何你想做的事。我前一段时间尝试过,它可以工作,但我猜你暂时只能使用 webkit(但你可能已经接受了)。顺便说一句,因为我已经发布了 2 个答案的相同链接,所以我提供一般性建议:查看 W3C - 他们几乎编写了规则并描述了标准。此外,webkit 开发页面也非常关键。

I just posted a similar answer, and you probably want to have a look at:

http://www.w3.org/TR/css3-animations/#animation-events-

You can find out aspects of an animation, such as start and stop, and then, once say the 'stop' event has fired you can do whatever you want to the dom. I tried this out some time ago, and it can work, but I'd guess you're going to be restricted to webkit for the time being (but you've probably accepted that already). Btw, since I've posted the same link for 2 answers, I'd offer this general advice: check out the W3C - they pretty much write the rules and describe the standards. Also, the webkit development pages are pretty key.

喜爱纠缠 2024-10-12 18:41:56

实际上没有人这样做,它的工作方式是 animation-play-state 设置为暂停。

Nobody actualy brought it so, the way it was made to work is animation-play-state set to paused.

╰ゝ天使的微笑 2024-10-12 18:41:56

我今天了解到填充模式有一个限制。这是来自苹果开发者的。传言是 * 左右 * 六,但不确定。
或者,您可以将类的初始状态设置为您希望动画结束的方式,然后在 from / 0% 处初始化它。

I learned today that there is a limit you want to use for the fill-mode. This is from an Apple dev. Rumor is * around * six, but not certain.
Alternatively, you can set the initial state of your class to how you want the animation to end, then * initialize * it at from / 0% .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文