CSS 转换回调
CSS 转换完成后是否可以收到通知(如回调)?
Is it possible to get a notification (like callback) when a CSS transition has been completed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
CSS 转换完成后是否可以收到通知(如回调)?
Is it possible to get a notification (like callback) when a CSS transition has been completed?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
是的,如果浏览器支持此类操作,则转换完成时会触发一个事件。然而,实际事件因浏览器而异:
webkitTransitionEnd
transitionend
msTransitionEnd
oTransitionEnd
但是您应该知道
webkitTransitionEnd
并不总是触发!这已经让我困惑了很多次,并且如果动画对元素没有影响,似乎就会发生这种情况。为了解决这个问题,在未按预期触发事件处理程序的情况下,使用超时来触发事件处理程序是有意义的。有关此问题的博客文章可在此处找到:http://www. cuppadev.co.uk/the-trouble-with-css-transitions/ <-- 500 Internal Server Error考虑到这一点,我倾向于在一大块中使用此事件代码看起来有点像这样:
注意:要获取转换事件结束名称,您可以使用作为答案发布的方法:
如何跨浏览器标准化 CSS3 转换函数? 。
注意:这个问题还涉及:
- CSS3 转换事件
Yes, if such things are supported by the browser, then an event is triggered when the transition completes. The actual event however, differs between browsers:
webkitTransitionEnd
transitionend
msTransitionEnd
oTransitionEnd
However you should be aware that
webkitTransitionEnd
doesn't always fire! This has caught me out a number of times, and seems to occur if the animation would have no effect on the element. To get around this, it makes sense to use a timeout to fire the event handler in the case that it's not been triggered as expected. A blog post about this problem is available here: http://www.cuppadev.co.uk/the-trouble-with-css-transitions/ <-- 500 Internal Server ErrorWith this in mind, I tend to use this event in a chunk of code that looks a bit like this:
Note: to get the transition event end name you can use the method posted as the answer in:
How do I normalize CSS3 Transition functions across browsers?.
Note: this question is also related to:
- CSS3 transition events
我知道 Safari 实现了 webkitTransitionEnd 您可以通过转换直接将其附加到元素的回调。
他们的示例(重新格式化为多行):
I know that Safari implements a webkitTransitionEnd callback that you can attach directly to the element with the transition.
Their example (reformatted to multiple lines):
我使用以下代码,比尝试检测浏览器使用哪个特定结束事件简单得多。
或者,如果您使用 bootstrap 那么您可以简单地执行
此操作,因为它们在 bootstrap.js 中包含以下内容
I am using the following code, is much simpler than trying to detect which specific end event a browser uses.
Alternatively if you use bootstrap then you can simply do
This is becuase they include the following in bootstrap.js
这可以通过
transitionend
事件轻松实现。请参阅此处的文档。一个简单的例子:
This can easily be achieved with the
transitionend
event. See documentation here.A simple example:
jQuery.transit 插件是一个用于 CSS3 转换和过渡的插件,可以从脚本调用 CSS 动画,给您回电。
The jQuery.transit plugin, a plugin for CSS3 transformations and transitions, can call your CSS animations from script and give you a callback.