GreenSock GSAP 3.0 最新版 所有内容创建于2020年4月4日

【1】 tween

gsap有三个常用的方法来创建一个Tweens对象,Tweens之间是并发的,如无特殊约束则同时触发:

  • gsap.to(targets, vars={}),to表示从一个HTML元素应有的的默认状态到目标状态。
  • gsap.from(targets, vars={}),from表示从目标状态到一个HTML元素应有的的默认状态。
  • gsap.fromTo(targets, vars={}),我也没用过。

第一个参数是一个选择器,等价于querySelector(targets),这意味着你选择的可能是一个HTML元素,也有可能是一组。

【1.1】vars速查表

属性描述
callbackScopeThe scope to be used for all of the callbacks (onStart, onUpdate, onComplete, etc.).
dataAssign arbitrary data to this property (a string, a reference to an object, whatever) and it gets attached to the tween instance itself so that you can reference it later like yourTween.data.
delayAmount of delay before the animation should begin (in seconds).
durationThe duration of the animation (in seconds). Default: 0.5.
easeControls the rate of change during the animation, giving it a specific feel. For example, “elastic” or “strong.inOut”. See the Ease Visualizer for a list of all of the options. ease can be a String (most common) or a function that accepts a progress value between 0 and 1 and returns a converted, similarly normalized value. Default: “power1.out”.
idAllows you to (optionally) assign a unique identifier to your tween instance so that you can find it later with gsap.getById() and it will show up in GSDevTools with that id.
immediateRenderNormally a tween waits to render for the first time until the very next tick (update cycle) unless you specify a delay. Set immediateRender: true to force it to render immediately upon instantiation. Default: false for to() tweens, true for from() and fromTo() tweens.
inheritNormally tweens inherit from their parent timeline’s defaults object (if one is defined), but you can disable this on a per-tween basis by setting inherit: false.
lazyWhen a tween renders for the very first time and reads its starting values, GSAP will try to delay writing of values until the very end of the current “tick” which can improve performance because it avoids the read/write/read/write layout thrashing that browsers dislike. To disable lazy rendering for a particular tween, set lazy: false. In most cases, there’s no need to set lazy. To learn more, watch this video. Default: true (except for zero-duration tweens).
onCompleteA function to call when the animation has completed.
onCompleteParamsAn Array of parameters to pass the onComplete function. For example, gsap.to(".class", {x:100, onComplete:myFunction, onCompleteParams:[“param1”, “param2”]});.
onInterruptA function to call when the animation is interrupted, meaning if/when the tween is killed before it completes. This could happen because its kill() method is called or due to overwriting.
onInterruptParamsAn Array of parameters to pass the onInterrupt function. For example, gsap.to(".class", {x:100, onInterrupt:myFunction, onInterruptParams:[“param1”, “param2”]});.
onRepeatA function to call each time the animation enters a new iteration cycle (repeats). Obviously this only occurs if you set a non-zero repeat.
onRepeatParamsAn Array of parameters to pass the onRepeat function.
onReverseCompleteA function to call when the animation has reached its beginning again from the reverse direction (excluding repeats).
onReverseCompleteParamsAn Array of parameters to pass the onReverseComplete function.
onStartA function to call when the animation begins (when its time changes from 0 to some other value which can happen more than once if the tween is restarted multiple times).
onStartParamsAn Array of parameters to pass the onStart function.
onUpdateA function to call every time the animation updates (on each “tick” that moves its playhead).
onUpdateParamsAn Array of parameters to pass the onUpdate function.
overwriteIf true, all tweens of the same targets will be killed immediately regardless of what properties they affect. If “auto”, when the tween renders for the first time it hunt down any conflicts in active animations (animating the same properties of the same targets) and kill only those parts of the other tweens. Non-conflicting parts remain intact. If false, no overwriting strategies will be employed. Default: false.
pausedIf true, the animation will pause itself immediately upon creation. Default: false.
repeatHow many times the animation should repeat. So repeat: 1 would play a total of two iterations. Default: 0.
repeatDelayAmount of time to wait between repeats (in seconds). Default: 0.
repeatRefreshSetting repeatRefresh: true causes a repeating tween to invalidate() and re-record its starting/ending values internally on each full iteration (not including yoyo’s). This is useful when you use dynamic values (relative, random, or function-based). For example, x: “random(-100, 100)” would get a new random x value on each repeat. duration, delay, and stagger do NOT refresh.
reversedIf true, the animation will start out with its playhead reversed, meaning it will be oriented to move toward its start. Since the playhead begins at a time of 0 anyway, a reversed tween will appear paused initially because its playhead cannot move backward past the start.
runBackwardsIf true, the animation will invert its starting and ending values (this is what a from() tween does internally), though the ease doesn’t get flipped. In other words, you can make a to() tween into a from() by setting runBackwards: true.
staggerIf multiple targets are defined, you can easily stagger the start times for each by setting a value like stagger: 0.1 (for 0.1 seconds between each start time). Or you can get much more advanced staggers by using a stagger object. For more information, see the stagger documentation.
startAtDefines starting values for any properties (even if they’re not animating). For example, startAt: {x: -100, opacity: 0}
yoyoIf true, every other repeat iteration will run in the opposite direction so that the tween appears to go back and forth. This has no affect on the reversed property though. So if repeat is 2 and yoyo is false, it will look like: start - 1 - 2 - 3 - 1 - 2 - 3 - 1 - 2 - 3 - end. But if yoyo is true, it will look like: start - 1 - 2 - 3 - 3 - 2 - 1 - 1 - 2 - 3 - end. Default: false.
yoyoEaseAllows you to alter the ease in the tween’s yoyo phase. Set it to a specific ease like “power2.in” or set it to true to simply invert the tween’s normal ease. Note: GSAP is smart enough to automatically set yoyo: true if you define any yoyoEase, so there’s less code for you to write. Default: false.
keyframesTo animate the targets to various states, use keyframes - an array of vars objects that serve as to() tweens. For example, keyframes: [{x:100, duration:1}, {y:100, duration:0.5}]. All keyframes will be perfectly sequenced back-to-back, but you can define a delay value to add spacing between each step (or a negative delay would create an overlap).

【1.2】基于函数的动态动画

通过对任意的属性使用函数来获得不可思议的动态动画,我们之前提到,创建tween时使用的css选择器有可能选择多个HTML元素,因此,如果我们想为每一个HTML元素应用不同的、有规律变化的动画就可以使用基于函数的动态动画。

gsap.to(".class", {
    x: 100, // 静态值
    y: function(index, target, targets) { 
    	//function-based value
    	index从0到targets.length - 1
    	每一个target都是当前处理的HTML元素
        return index * 50; 
    },
    duration: 1
});

【1.3】stagger

这也是一个很常用的属性,如果你给一组HTML元素应用动画时,希望他们之间有一个延迟,可以用stagger属性。

【1.4】Sequencing 序列化

序列化是指,让某些动画按顺序依次执行,除了使用delay以外,我们十分推荐你使用后面会讲到的timeline。

【1.5】keyframes 关键帧

如果你打算让同一个HTML对象依次执行多个动画的话,使用关键帧是一个不错的选择。

gsap.to(".class", {keyframes: [
  {x: 100, duration: 1},
  {y: 200, duration: 1, delay: 0.5}, //create a 0.5 second gap
  {rotation: 360, duration: 2} //overlap by 0.25 seconds
], ease: "none"});

上面的代码会让.class先执行右平移,延迟0.5s后执行下平移,最后执行旋转360度,注意,当上一个动作执行完后才会执行下一个,当然你也可以用timeline实现。

【3】 timeline

timeline是tweens的容器,它可以按照添加顺序依次执行每一个tweens,这样我们就可以不用考虑执行顺序,交给timeline来处理。

let tl = gsap.timeline(); //create the timeline
tl.to(".class1", {x: 100}) //start sequencing
  .to(".class2", {y: 100, ease: "elastic"})
  .to(".class3", {rotation: 180});

【ease速查表】

对于下表的所有动画函数的名称name都有扩展的,name.in,和name.inout。

名称描述
none线性
power1形如log函数
power2形如更凸的log函数
power3形如更更凸的log函数
power4形如更更更凸的log函数
back有一个超出目标位置再回来的动作
elastic在目标位置左右摇摆后停下
bounce弹跳
rough乱摇
slow形如中心对称的sigmoid函数
steps阶梯式
circ形如第二象限的圆弧
expo夸张版的circ
sine收敛版的power1
Custom自定义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值