Jump to content
Search Community

Horizantal Scroll automatically scrolls without touching the mouse wheel

ty byers test
Moderator Tag

Recommended Posts

I'm adding code for a horizontal scroller with 4 sections.  For some reason it instantly scrolls to the last section before I even touch the mouse wheel.  When I refresh the page, I can see all 4 sections quickly scroll by.


import React, { useRef, useEffect } from 'react';
import gsap from "gsap";
import ScrollTrigger from "gsap/dist/ScrollTrigger";
import "../index.css";

const ScrollSection = () => {
  const sectionRef = useRef(null);
  const triggerRef = useRef(null);


  useEffect(() => {
    const pin = gsap.fromTo(sectionRef.current, {
      translateX: 0
    }, {
      translateX: '-300vw',
      ease: 'none',
      duration: 1,
      scrollTrigger: {
        trigger: triggerRef.current,
        start: 'top top',
        end: '2000 top',
        scrub: 0.6,
        pin: true
    return () => {
  }, [])
  return (
    <section className='scroll-section-outer'>
      <div ref={triggerRef}>
        <div ref={sectionRef} className='scroll-section-inner'>
          <div className='scroll-section'>
            <h3>Section 1</h3>
          <div className='scroll-section'>
            <h3>Section 2</h3>
          <div className='scroll-section'>
            <h3>Section 3</h3>
          <div className='scroll-section'>
            <h3>Section 4</h3>


export default ScrollSection;


Link to comment
Share on other sites

Hi there! I see you're using React -

Proper animation cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.


In GSAP 3.11, we introduced a new gsap.context() feature that helps make animation cleanup a breeze. All you need to do is wrap your code in a context call. All GSAP animations and ScrollTriggers created within the function get collected up in that context so that you can easily revert() ALL of them at once.


Here's the structure:

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  return () => ctx.revert(); // <- cleanup!
}, []);

This pattern follows React's best practices, and one of the React team members chimed in here if you'd like more background.


We strongly recommend reading the React information we've put together at https://greensock.com/react


If you still need help, please make sure you provide a minimal demo, like in Stackblitz, that clearly illustrates the issue so we can see what's going on. 👍

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