跳到主要内容前端常用动画库:GSAP、Lottie、Swiper 与 AOS 实战指南 | 极客日志JavaScript大前端
前端常用动画库:GSAP、Lottie、Swiper 与 AOS 实战指南
介绍前端开发中常用的动画库 GSAP、AOS、Lottie 和 Swiper。涵盖 GSAP 的 ScrollTrigger 滚动触发与 SplitText 文字分割用法,AOS 的初始化配置,Lottie 的 JSON 加载与播放控制,以及 Swiper 的轮播图基础与 3D 效果实现。提供完整的 CDN 引入方式及核心代码示例,帮助开发者快速实现页面动效。
佛系玩家2 浏览 GSAP 的 ScrollTrigger 和 SplitText 动画
GSAP Resources:

CDN 引入:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ScrollTrigger.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/SplitText.min.js"></script>
ScrollTrigger 部分用法
gsap.registerPlugin(ScrollTrigger);
ScrollTrigger.create({
trigger: '.pic',
start: 'top top',
end: '+=2000',
: ,
: ,
: ,
: ,
: gsap.()
.(, { : }, { : }, )
.(, { : }, { : }, )
});
微信扫一扫,关注极客日志
微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
相关免费在线工具
- Keycode 信息
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
- Escape 与 Native 编解码
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
- JavaScript / HTML 格式化
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
- JavaScript 压缩与混淆
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
scrub
true
markers
true
pin
true
pinSpacing
true
animation
timeline
fromTo
".pic4"
width
"18.75em"
width
"16.75em"
">"
fromTo
".pic1"
left
"55.125em"
left
"14.3em"
"<"
常用属性
| 属性 | 取值 | 说明 |
|---|
| start | 数字/方位名词 | 触发位置 |
| end | 数字/方位名词 | 结束位置 |
| trigger | Element | 触发元素 |
| animation | Tween/Timeline | 动画对象 |
| scrub | 布尔值/数字 | 是否绑定滚动条 |
| toggleClass | 字符串 | 切换类名 |
| markers | true/false | 开启标注功能 |
| scroller | Element | 滚动容器 |
| pin | Element | 固定元素 |
| direction | 数字 | 滚动方向 (1 向前,-1 向后) |
| isActive | 布尔值 | 是否在触发范围内 |
| horizontal | 布尔型 | 是否水平滚动 |
| progress | 数字 | 进度 (0-1) |
SplitText 文字分割动画
gsap.registerPlugin(SplitText, ScrollTrigger);
document.fonts.ready.then(() => {
gsap.set(".split", { opacity: 1 });
const splitElements = document.querySelectorAll(".split");
splitElements.forEach((element) => {
SplitText.create(element, {
type: "words,lines",
mask: "lines",
onSplit: (self) => {
gsap.from(self.words, {
duration: 1.3,
y: 50,
opacity: 0,
stagger: {
amount: 0.6
},
scrollTrigger: {
trigger: element,
start: "top 88%",
end: "top 20%"
}
});
}
});
});
});
AOS 动画
JS 代码
AOS.init({
offset: 120,
delay: 0,
duration: 400,
easing: 'ease',
once: false,
mirror: false,
anchorPlacement: 'top-bottom'
});
HTML 代码(AOS 使用方法)
<div data-aos="fade-up">内容</div>
Lottie 动画
可添加关键帧自定义动画,并导出 JSON 文件在 web 端使用
<script src="https://unpkg.com/[email protected]/build/player/lottie.min.js"></script>
HTML 代码
<div id="structure_one_container"></div> // 自定义容器选择器名,设置显示动画的容器
JS 代码
const structure_one_container = document.getElementById('structure_one_container');
const structure_one_animation = lottie.loadAnimation({
container: structure_one_container,
renderer: 'svg',
loop: false,
autoplay: false,
path: 'structe_one.json'
});
window.addEventListener('scroll', () => {
const rect = structure_one_container.getBoundingClientRect();
if (rect.top <= window.innerHeight / 1.5 && !structure_one_animation.isPlaying) {
structure_one_animation.play();
}
});
Swiper 轮播图
<script src="https://unpkg.com/swiper@8/swiper-bundle.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/swiper@8/swiper-bundle.min.css" />
HTML 代码(类名要正确)
<div class="swiper course-slider">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-scrollbar"></div>
</div>
CSS 代码控制分页器
.swiper-pagination-bullet-active {
background-color: #00E77F;
}
.swiper-pagination-bullet {
background-color: #fff;
}
JS 代码
var swiper = new Swiper(".course-slider", {
spaceBetween: 20,
grabCursor: true,
loop: true,
pagination: {
el: ".swiper-pagination",
clickable: true
},
breakpoints: {
540: { slidesPerView: 1 },
768: { slidesPerView: 2 },
1024: { slidesPerView: 3 }
}
});
var swiper = new Swiper(".home-slider", {
effect: "coverflow",
grabCursor: true,
centeredSlides: true,
slidesPerView: "auto",
coverflowEffect: {
rotate: 0,
stretch: 0,
depth: 100,
modifier: 2,
slideShadows: true
},
loop: true,
autoplay: {
delay: 3000,
disableOnInteraction: false
}
});
let lastActiveIndex = swiper2.activeIndex;
swiper2.on('slideChange', function() {
var activeIndex = swiper2.activeIndex;
var activeSlide = swiper2.slides[activeIndex];
var currentImage = activeSlide.querySelector('img');
let num = currentImage.id;
var current = document.getElementsByClassName("spc")[num - 1];
if (num - 2 >= 0 && activeIndex > lastActiveIndex) {
var prev = document.getElementsByClassName("spc")[num - 2];
prev.style.color = "black";
prev.style.backgroundColor = "white";
} else {
var prev = document.getElementsByClassName("spc")[5];
prev.style.color = "black";
prev.style.backgroundColor = "white";
}
if (num != 6 && activeIndex < lastActiveIndex) {
var prev = document.getElementsByClassName("spc")[num];
prev.style.color = "black";
prev.style.backgroundColor = "white";
} else {
var prev = document.getElementsByClassName("spc")[0];
prev.style.color = "black";
prev.style.backgroundColor = "white";
}
current.style.color = "white";
current.style.backgroundColor = "black";
lastActiveIndex = activeIndex;
});

Font Awesome 图标字体工具
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
- **用法:**直接在 html 当中嵌入对应的
<i></i> 标签即可(可用 font-size 调整大小)
- JS 添加 active 类切换图标
let num_s = 0;
sound.onclick = () => {
num_s += 1;
if (num_s % 2 == 0) {
sound_icon.classList.remove('fa-volume-xmark');
sound_icon.classList.add('fa-volume-high');
video.muted = false;
sbar.style.width = (video.volume * 100) + "%";
} else {
sound_icon.classList.remove('fa-volume-high');
sound_icon.classList.add('fa-volume-xmark');
video.muted = true;
sbar.style.width = 0 + "%";
}
};