I have a project in Astro, and I'm using smooth scrolling and scroll trigger on various animations in different components of my homepage. However, animations that involve horizontal movements (I'm not sure if this is relevant or not) break when I resize the browser.
Working code:
<script type="module" is:inline>
import gsap from "https://cdn.skypack.dev/gsap";
import ScrollTrigger from "https://cdn.skypack.dev/gsap/ScrollTrigger";
document.addEventListener("astro:page-load", () => {
gsap.registerPlugin(ScrollTrigger);
// Banner index animation
const items = document.querySelectorAll(".sections ul li");
items.forEach((item) => {
gsap.fromTo(
item,
{ opacity: 0, y: 100 },
{
opacity: 1,
y: 0,
ease: "power1.out",
scrollTrigger: {
trigger: item,
start: "top bottom",
end: "bottom-=100px bottom",
scrub: 2,
},
}
);
});
});
</script>
And this animations breaks on resize and kills any animation below him:
<script type="module" is:inline>
import gsap from "https://cdn.skypack.dev/gsap";
import SplitText from "../../node_modules/gsap/SplitText.js";
import ScrollTrigger from "../../node_modules/gsap/ScrollTrigger.js";
document.addEventListener("astro:page-load", () => {
gsap.registerPlugin(ScrollTrigger, SplitText);
function animation() {
// Dividir el texto en líneas y aplicar una clase a cada línea
const splitText = new SplitText("#highlightText .text", {
type: "lines",
linesClass: "lineClass",
});
const splitTextClone = new SplitText("#highlightText .text.--clone", {
type: "lines",
linesClass: "lineClassClone",
});
const container = document.querySelector(
"#highlightText .js-text-animated"
);
gsap.utils
.toArray("#highlightText .lineClassClone", container)
.forEach((line) => {
const childLine = line.querySelector("#highlightText .lineClass");
// Definimos la animación con ScrollTrigger
gsap.fromTo(
line,
{ x: -line.offsetWidth },
{
x: 0,
ease: "power3.out",
scrollTrigger: {
trigger: line,
start: "top bottom",
end: "top top",
scrub: 2,
},
}
);
// Animación para el hijo en dirección opuesta
gsap.fromTo(
childLine,
{ x: childLine.offsetWidth },
{
x: 0,
ease: "power3.out",
scrollTrigger: {
trigger: line,
start: "top bottom",
end: "top top",
scrub: 2,
},
}
);
});
// Configuración de ScrollTrigger para separator-main
gsap.to("#highlightText .js-section-separator-main", {
x: -1000, // Cambiar según la distancia deseada
scrollTrigger: {
trigger: ".js-section-separator",
start: "top bottom",
end: "bottom top",
ease: "power1.out",
scrub: 2,
},
});
// Configuración de ScrollTrigger para separator-secondary
gsap.to("#highlightText .js-section-separator-secondary", {
x: -1000,
scrollTrigger: {
trigger: ".js-section-separator",
start: "top bottom",
end: "bottom top",
ease: "power1.out",
scrub: 2.5,
},
});
}
animation();
});
</script>
Thank you in advance!!