GSAP ScrollTrigger - 为每个部分创建时间轴
GSAP ScrollTrigger - Create a Timeline for Every Section
我有一个“项目”页面,我在其中展示项目作品,我希望每个作品都在滚动中淡入。我正在尝试使用以下 HTML 和 JavaScript:
为每个项目实施时间表
<!-- Page contains multiple projects that follow this template -->
<div id="project-trigger">
<div class="project">
<img class="project-image" src="image1" alt="">
<div class="description">
<h3 class="project-text">Lorem Ipsum</h3>
<p class="project-text">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Fuga impedit numquam modi accusamus.</p>
</div>
</div>
</div>
let projectTrigger = document.querySelectorAll('#project-trigger');
projectTrigger.forEach(project => {
let timeline = gsap.timeline({
scrollTrigger:{
trigger:"#project-trigger",
start:"center bottom",
ease:"power2.easeOut",
toggleActions: "play none none reverse",
}
})
timeline.from(".project-image",{
x:-200,
opacity:0,
duration:0.5
}).from(".project-text",{
x:200,
opacity:0,
duration:0.5,
stagger:0.2
}, "-=0.5")
}
)
我希望图像从左侧淡入,文本从右侧淡入。目前,它不起作用 - 图像淡入一半然后停止,文本根本不滚动。
是否可以实现我所追求的?如果是这样,我哪里出错了?
代码笔:https://codepen.io/jacobcollinsdev/pen/jOrpKja?editors=1010
您的HTML无效。不能有多个具有相同 ID 的元素。如果你需要做类似的事情,你应该使用 class 代替。
您还制作了 most common ScrollTrigger mistakes: using generic selectors when you want to use scoped ones. I write more about how to do this in my article about animating efficiently 之一。
修复这些错误,您应该得到如下内容:
const projectTriggers = document.querySelectorAll(".project-trigger");
projectTriggers.forEach(addTimeline);
function addTimeline(project, index) {
const image = project.querySelector(".project-image");
const text = project.querySelector(".project-text");
const timeline = gsap.timeline({
scrollTrigger: {
trigger: project,
start: "center bottom",
ease: "power2",
toggleActions: "play none none reverse"
}
})
.from(image, {
x: -200,
opacity: 0,
duration: 0.5
})
.from(text, {
x: 200,
opacity: 0,
duration: 0.5,
stagger: 0.2
}, "-=0.5");
}
Demo
要消除开头无样式内容的闪烁,请参阅 this post。
我有一个“项目”页面,我在其中展示项目作品,我希望每个作品都在滚动中淡入。我正在尝试使用以下 HTML 和 JavaScript:
为每个项目实施时间表<!-- Page contains multiple projects that follow this template -->
<div id="project-trigger">
<div class="project">
<img class="project-image" src="image1" alt="">
<div class="description">
<h3 class="project-text">Lorem Ipsum</h3>
<p class="project-text">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Fuga impedit numquam modi accusamus.</p>
</div>
</div>
</div>
let projectTrigger = document.querySelectorAll('#project-trigger');
projectTrigger.forEach(project => {
let timeline = gsap.timeline({
scrollTrigger:{
trigger:"#project-trigger",
start:"center bottom",
ease:"power2.easeOut",
toggleActions: "play none none reverse",
}
})
timeline.from(".project-image",{
x:-200,
opacity:0,
duration:0.5
}).from(".project-text",{
x:200,
opacity:0,
duration:0.5,
stagger:0.2
}, "-=0.5")
}
)
我希望图像从左侧淡入,文本从右侧淡入。目前,它不起作用 - 图像淡入一半然后停止,文本根本不滚动。
是否可以实现我所追求的?如果是这样,我哪里出错了?
代码笔:https://codepen.io/jacobcollinsdev/pen/jOrpKja?editors=1010
您的HTML无效。不能有多个具有相同 ID 的元素。如果你需要做类似的事情,你应该使用 class 代替。
您还制作了 most common ScrollTrigger mistakes: using generic selectors when you want to use scoped ones. I write more about how to do this in my article about animating efficiently 之一。
修复这些错误,您应该得到如下内容:
const projectTriggers = document.querySelectorAll(".project-trigger");
projectTriggers.forEach(addTimeline);
function addTimeline(project, index) {
const image = project.querySelector(".project-image");
const text = project.querySelector(".project-text");
const timeline = gsap.timeline({
scrollTrigger: {
trigger: project,
start: "center bottom",
ease: "power2",
toggleActions: "play none none reverse"
}
})
.from(image, {
x: -200,
opacity: 0,
duration: 0.5
})
.from(text, {
x: 200,
opacity: 0,
duration: 0.5,
stagger: 0.2
}, "-=0.5");
}
Demo
要消除开头无样式内容的闪烁,请参阅 this post。