为 CSS 转换指定自定义计时函数?
我现在经常使用 CSS 过渡,但发现它仅限于只能访问缓入、缓出等。
贝塞尔曲线选项似乎允许最多的控制,但即使这样也不允许你指定一个实际的缓动方程来模拟弹性缓动等。
是否有另一种选择,或者是否需要求助于 javascript 来执行这种类型的动画?
I use CSS transitions pretty frequently now but find it limiting to only have access to ease-in, ease-out etc.
The bezier-curve
option appears to allow the most control but even this does not allow you to specify an actual easing equation that would simulate elastic easing etc..
Is there another option or does one need to resort to javascript to perform this type of animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 Caesar 为不同的方程生成 CSS。
You can use Caesar to generate CSS for different equations.
我发现没有办法用纯 CSS 进行弹性过渡。不过你可以作弊并制作 CSS 动画。这是苹果在其网站上使用的内容:
这些动画在苹果自己的网站上得到了很大程度的使用:
http://www.apple.com/mac/
显然这在某种程度上是强大的 - 因为动画是百分比,您可以轻松更改持续时间并保留效果。
然而,这仍然是非常静态的。假设您想要单击一个按钮并让它执行弹性缩放动画。没问题,每个按钮都可以反复使用一个动画。但是,假设您希望一个元素能够将其位置弹性地捕捉到用户最近在屏幕上单击或点击的位置。在这种情况下,您需要动态地重新计算关键帧,然后将动画应用到元素。
在撰写本文时,我不知道有什么好的例子可以在 javascript 中动态生成 CSS 动画。事实上,这可能还值得提出另一个问题。总而言之,这是我发现的唯一纯 CSS 方式来渲染复杂的缓动方程,例如纯粹使用 CSS 的弹性缓动。
I found there is no way to do an elastic transition with pure CSS. However you can cheat and do a CSS animation. This is what apple uses on their site:
These animations are used to great extent on Apple's own website:
http://www.apple.com/mac/
Obviously this is powerful to some extent -- since the animations are percentages you can easily change he duration and retain the effect.
However, this is still very static. Let's say you want to click a button and have it perform an elastic scaling animation. No problem, one animation can be used over and over again for each button. But let's say you want to have an element elastically snap it's position to wherever the user most recently clicked or tapped on the screen. Well in this case you'd need to dynamically recalculate the keyframes and then apply the animation to the element.
At the time of this writing I don't know that there are any good examples of dynamically generating CSS animations on the fly inside of javascript. In fact it probably warrants another question. All in all this is the only pure CSS way I found to do render a complex easing equation such as elastic easing purely with CSS.
实际上,您不需要 JavaScript 库来支持复杂的缓动功能,例如bounce-ease-in 或elastic-ease-out。下面是 CSS3 动画生成器工具,它可以为您生成关键帧,并启用 w3c 规范不支持的 12 个附加缓动函数:
http: //www.css3animationgenerator.com/
Actually you don't need a JavaScript library to support complex easing functions like bounce-ease-in or elastic-ease-out. Here's the CSS3 Animation Generator tool which generates the keyframes for you, and enables 12 additional easing functions not supported by the w3c spec:
http://www.css3animationgenerator.com/
我知道,如果您使用 mootools,您可以编写自己的方程式:
http://mootools .net/docs/core/Fx/Fx.Transitions
也许其他库(如 jquery 或 prototype)具有相同的类,可能它们确实具有类似的东西。
祝你好运!
i know that if you're using mootools you could write your own equation:
http://mootools.net/docs/core/Fx/Fx.Transitions
Maybe the other libraries like jquery or prototype have the same class, probably they do have something similar.
Good Luck!
近似值:
根据OP请求“指定一个模拟弹性缓动的实际缓动方程”,现在可以使用
线性()
CSS函数(请参阅支持表)。linear() approximation:
As per OP request to "specify an actual easing equation that would simulate elastic easing" it is now possible using the
linear()
CSS function (see support table).???? MDN has a very detailed page explaining it
For the below demo I have used this online tool which generates CSS
linear()
values for a given javascript mathematical easing function or an SVG visualized representation of an easing graph.Hover the demo to see it in action: