特性
- 较小的文件大小
- 新语法简化API
Part1 - 简化的API
使用新版本的GreenSock,我们不再需要决定是否要使用TweenMax,TweenLite,TimelineMax或TimelineLite。现在,所有内容都在一个简化的API中
<script>
/*
不再区分TweenLite、TimelineLite、TweenMax、TimelineMax
全部合成gsap.
以前的持续时间也将整合为duration
*/
gsap.from(".box-200", {
duration: 3,
y: 500,
background: 'red'
})
</script>
以前,动画的持续时间在目标元素之后直接定义为其自己的参数。
# 2.x版本
TweenMax.to('.box', 1, {})
在新版本中,持续时间与您要设置动画的其余属性在同一vars对象中定义,因此更加明确。
gsap.to(".box",{
duration: 2,
});
Part2 - 时间轴用法
创建时间表也很容易。现在,无需使用新的TimelineMax()或新的TimelineLite()来创建时间线,而只需使用gsap.timeline()
<script>
const time = gsap.timeline();
time.to(".box-200", {
duration: 2,
x: 300,
})
.to(".box-100", {
duration: 1,
y: 100
})
</script>
可以发现相同的事情写了多遍,那么有什么简便的方法吗?
通过传入default属性
<script>
const time = gsap.timeline({
defaults: { // Main
duration: 3
}
});
time.to(".box-200", {
x: 300,
})
.to(".box-100", {
y: 100
})
</script>
在时间轴中使用前缀代替标签
通常,在创建时间轴时,我会创建标签,然后使用它们添加延迟或设置补间的位置。
gsap.timeline()
.add("s")
.to(“.box1", { ... }, "s")
.to(“.box2", { ... }, "s")
.to(“.box3", { ... }, "s+=0.8")
.to(“.box4", { ... }, "s+=0.8”);
Part3
传统的stagger 、staggerTo、staggerForm
<script>
gsap.to(".box-200", {
duration: 2,
x: 500,
ease: Power0.easeIn,
stagger: 0.5 // 延迟秒数
})
</script>
Part4 - 函数功能
random(start,end) 使用字符串包起来
<script>
gsap.to(".box-200", {
duration: 'random(0.3,1.2)',
x: 'random(100,500)',
ease: Power0.easeIn,
stagger: 0.5 // 延迟秒数
})
</script>
Part5 - 关键帧keyframes
引入了一个关键帧的数组属性
<script>
gsap.to(".box-200", {
keyframes: [{ # Main
duration: 1,
x: 200
}, {
duration: 1.5,
y: 200
}, {
duration: 1,
x: 0,
rotation: '360deg'
}, {
duration: 1,
y: 0,
background: 'red'
}]
})
</script>
Part6 - repeatRefresh
默认使用repeat选择重复运行时,它会回到开始位置。重新开始
那么使用repeatRefresh:true时,会选择动画最后位置,再开始运动
<script>
gsap.to(".box-200", {
duration: 0.3,
x: "+=100",
repeat: 5,
repeatRefresh: false
})
</script>
Part7 - 动画控制
<script>
/*
全局的自动控制 & 设定
gsap.globalTimeline.timeScale(0.1) // 控制动画转化速率 0~1
gsap.globalTimeline.pause(); // 停止动画
gsap.globalTimeline.play(); // 开始动画
gsap.globalTimeline.paused(); // 目前状态
*/
gsap.timeline()
.to(".box-200", {
duration: 5,
x: 500
})
console.log(gsap.globalTimeline.paused());
var start = document.querySelector(".start")
start.onclick = function() {
gsap.globalTimeline.play();
}
var stop = document.querySelector(".stop");
stop.onclick = function() {
gsap.globalTimeline.pause();
}
</script>
参考资料
https://tympanus.net/codrops/2019/11/14/the-new-features-of-gsap-3/