Jump to content
Search Community

Some animations break when I resize the browser

Hetsahk test
Moderator Tag

Recommended Posts

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!!

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...