jQuery 缓动函数 —变量理解
jQuery 的缓动函数如何工作?举个例子:
easeInQuad = function (x, t, b, c, d) {
return c*(t/=d)*t + b;
};
这是如何运作的?每个参数包含什么?我将如何为动画实现一些愚蠢的缓动?
另外,我如何将缓动模式附加到 jQuery,将其加载到 $.easing 中是否足够好?
How does the easing function for jQuery work? Take for example:
easeInQuad = function (x, t, b, c, d) {
return c*(t/=d)*t + b;
};
How does that work? What does each parameter hold? How would I implement some dumb easing for an animation?
Also how would I attach an easing pattern to jQuery, is loading it into $.easing good enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个具体的例子,
假设我们的初始值为 1000,我们希望在 3s 内达到 400:
让我们从 0 秒到 3 秒:
所以相比概要:
我们可以推断出:
NB1:一件重要的事情是
elapsed
(t
) 和duration
(d
)应以相同的单位表示,例如:这里对我们来说是“秒”,但也可以是“毫秒”或其他单位。对于initialValue
(b
) 和amountOfChange
(c
) 也是如此,所以总结一下:NB2 :像 @DamonJW 一样,我不知道为什么
x
在这里。它不会出现在彭纳方程中,并且结果中似乎没有使用:让总是将他设置为null
A concrete example,
Suppose our initial value is 1000 and we want to reach 400 in 3s:
Let's go from 0s to 3s:
So compared to the synopsis:
we can deduce:
NB1: One important thing is that
elapsed
(t
) andduration
(d
) should be expressed in the same unit, eg: here 'seconds' for us, but could be 'ms' or whatever. This is also true forinitialValue
(b
) andamountOfChange
(c
), so to sum-up:NB2: Like @DamonJW, I don't know why
x
is here. It does not appear in Penner's equations and does not seem to be used in result: let always set him tonull
t:当前时间,b:起始值,c:从起始值到结束值的变化,d:持续时间。
它的工作原理如下: http://james.padolsey.com/demos/jquery/easing/ (表示 CSS 属性更改时的曲线)。
以下是我如何实现一些愚蠢的缓动: http://www.timotheegroleau.com/Flash /experiments/easing_function_generator.htm(数学不是我的强项)
您可以像以下任何一个一样扩展:http://code.google.com/p/vanitytools/source/browse/trunk/website/js/custom_easing.js ?spec=svn29&r=29 - 足够好!
t: current time, b: start value, c: change from the start value to the end value, d: duration.
Here is how it works: http://james.padolsey.com/demos/jquery/easing/ (curve representing when a CSS property is changed).
Here is how I would implement some dumb easing: http://www.timotheegroleau.com/Flash/experiments/easing_function_generator.htm (math is not my strong suit)
You would extend like any of these: http://code.google.com/p/vanitytools/source/browse/trunk/website/js/custom_easing.js?spec=svn29&r=29 - good enough!
该插件实现了最常见的缓动功能: http://gsgd.co.uk/sandbox/jquery /缓动/
This plugin implements the most common easing functions: http://gsgd.co.uk/sandbox/jquery/easing/
浏览了1.11 jquery代码。 x 参数似乎意味着“百分比”,与初始时间值无关。
因此,x 始终为 (0 <= x <= 1)(表示抽象系数),t 为 x * d(表示经过的时间)。
Looked through the 1.11 jquery code. The x parameter seems to mean 'percent', independent from initial time value.
So, x is always (0 <= x <= 1) (means abstract coefficient), and t is x * d (means time elapsed).
根据jQuery 1.6.2源码,缓动函数的含义如下。该函数在动画期间的不同时间点被调用。在调用它的瞬间,
The easing function should return a floating point number in the range [0,1], call it `r`. jQuery then computes `x=start+r*(end-start)`, where `start` and `end` are the start and end values of the property as specified in the call to animate, and it sets the property value to `x`.
据我所知,jQuery 并没有让您直接控制何时调用动画步骤函数,它只让您说“如果我在时间 t 被调用,那么我应该到目前为止已经完成了整个动画”。因此,例如,当对象移动得更快时,您不能要求更频繁地重绘对象。另外,我不知道为什么其他人说 b 是起始值,c 是变化值——这不是 jQuery 源代码所说的。
例如,如果您想定义自己的缓动函数来执行与 escapeInQuad 相同的操作,
According to the jQuery 1.6.2 source, the meaning of the easing function is as follows. The function is called at various points in time during the animation. At the instants it is called,
The easing function should return a floating point number in the range [0,1], call it `r`. jQuery then computes `x=start+r*(end-start)`, where `start` and `end` are the start and end values of the property as specified in the call to animate, and it sets the property value to `x`.
As far as I can see, jQuery doesn't give you direct control over when the animation step function is called, it only lets you say "if I am called at time t, then I should be thus far through the entire animation." Therefore you cannot, for example, ask for your object to be redrawn more frequently at times when it is moving faster. Also, I don't know why other people say b is the start value and c is the change -- that's not what jQuery source code says.
If you wanted to define your own easing function to do the same as easeInQuad, for example,