简体   繁体   English

jQuery缓动函数 - 变量的理解

[英]jQuery easing function — variables' comprehension

How does the easing function for jQuery work? jQuery的缓动函数如何工作? 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? 另外我如何将缓动模式附加到jQuery,将它加载到$ .easing中足够好?

According to the jQuery 1.6.2 source, the meaning of the easing function is as follows. 根据jQuery 1.6.2源代码,缓动函数的含义如下。 The function is called at various points in time during the animation. 在动画期间的各个时间点调用该函数。 At the instants it is called, 在它被称为的瞬间,

  • x and t both say what the time is now, relative to the start of the animation. x和t都表示现在的时间,相对于动画的开始。 x is expressed as a floating point number in the range [0,1], where 0 is the start and 1 is the end. x表示为[0,1]范围内的浮点数,其中0是开始,1是结束。 t is expressed in milliseconds since the start of the animation. t表示自动画开始以来的毫秒数。
  • d is the duration of the animation, as specified in the animate call, in milliseconds. d是动画的持续时间,在动画调用中指定,以毫秒为单位。
  • b=0 and c=1. b = 0且c = 1。

The easing function should return a floating point number in the range [0,1], call it r . 缓动函数应该返回[0,1]范围内的浮点数,称之为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计算x=start+r*(end-start) ,其中startend是对animate调用中指定的属性的起始值和结束值,并将属性值设置为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." 据我所知,jQuery并没有让你直接控制动画步骤函数的调用时间,它只会让你说“如果我在时间t被调用,那么我应该在整个动画中走得很远”。 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. 另外,我不知道为什么其他人说b是起始值而c是变化 - 这不是jQuery源代码所说的。

If you wanted to define your own easing function to do the same as easeInQuad, for example, 例如,如果您想要定义自己的缓动函数来执行与easeInQuad相同的操作,

$.extend(jQuery.easing,{myfunc:function(x,t,b,c,d) { return x*x; }})

$('#marker').animate({left:'800px'},'slow','myfunc');

A concrete example, 一个具体的例子,

Suppose our initial value is 1000 and we want to reach 400 in 3s : 假设我们的初始值是1000 ,我们希望在3s内达到400

var initialValue = 1000,
    destinationValue = 400,
    amountOfChange = destinationValue - initialValue, // = -600
    duration = 3,
    elapsed;

Let's go from 0s to 3s: 让我们从0到3:

elapsed = 0
$.easing.easeInQuad(null, elapsed, initialValue, amountOfChange, duration)
//> 1000

elapsed = 1
$.easing.easeInQuad(null, elapsed, initialValue, amountOfChange, duration)
//> 933.3333333333334

elapsed = 2
$.easing.easeInQuad(null, elapsed, initialValue, amountOfChange, duration)
//> 733.3333333333334

elapsed = 3
$.easing.easeInQuad(null, elapsed, initialValue, amountOfChange, duration)
//> 400

So compared to the synopsis: 所以与概要相比:

$.easing.easeInQuad = function (x, t, b, c, d) {...}

we can deduce: 我们可以推断:

 x       t          b              c            d
 |       |          |              |            |
null  elapsed  initialValue  amountOfChange  duration

NB1: One important thing is that elapsed ( t ) and duration ( d ) should be expressed in the same unit, eg: here 'seconds' for us, but could be 'ms' or whatever. NB1:一个重要的事情是, elapsedt )和durationd )应该以相同的单位表示,例如:这里的'秒'对我们而言,但可能是'ms'或其他什么。 This is also true for initialValue ( b ) and amountOfChange ( c ), so to sum-up: 对于initialValueb )和amountOfChangec )也是如此,所以总结一下:

 x       t          b              c            d
 |       |          |              |            |
null  elapsed  initialValue  amountOfChange  duration
         ^          ^              ^            ^
         +----------|----=unit=----|------------+
                    +----=unit=----+

NB2: Like @DamonJW , I don't know why x is here. NB2:就像@DamonJW一样,我不知道为什么x在这里。 It does not appear in Penner's equations and does not seem to be used in result: let always set him to null 它没有出现在Penner方程中 ,似乎没有在结果中使用:让我们总是将它设置为null

t: current time, b: start value, c: change from the start value to the end value, d: duration. t:当前时间,b:起始值,c:从起始值到结束值的变化,d:持续时间。

Here is how it works: http://james.padolsey.com/demos/jquery/easing/ (curve representing when a CSS property is changed). 以下是它的工作原理: http//james.padolsey.com/demos/jquery/easing/ (表示CSS属性更改时的曲线)。

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) 以下是我将如何实施一些愚蠢的缓和: http//www.timotheegroleau.com/Flash/experiments/easing_function_generator.htm (数学不是我的强项)

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//code.google.com/p/vanitytools/source/browse/trunk/website/js/custom_easing.js?specs = snn29&r = 29 - 足够好!

Looked through the 1.11 jquery code. 查看了1.11 jquery代码。 The x parameter seems to mean 'percent', independent from initial time value. x参数似乎意味着'百分比',与初始时间值无关。 So, x is always (0 <= x <= 1) (means abstract coefficient), and t is x * d (means time elapsed). 因此,x总是(0 <= x <= 1)(表示抽象系数),t是x * d(表示经过的时间)。

这个插件实现了最常见的缓动功能: http//gsgd.co.uk/sandbox/jquery/easing/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM