Jump to content
Search Community

Horizontal scroll with scroll trigger on React JS

SGRMHDK51 test
Moderator Tag

Recommended Posts

Hi I am trying to create Horizontal scroll component with React JS.

 

So far I have managed to get the Images scrolling horizontally, and have tried putting in scrolltrigger to animate the Images as they enter the viewport,

 

I am not able to set the "Start" position of trigger , which allow me to achieve the effect.

 

Effect : Remove blur of the image i.e from 2px to 0px once the image comes into view port from right.

Slider Component:     

<MainContainer>
        <SliderContainer>
          {PiattoImages.map((product, index) => {
            return (
              <>
                {' '}
                <img
                  ref={addToRefs}
                  key={index}
                  style={{
                    width: '280px',
                    height: '200px',
                    borderRadius: '25px',
                    aspectRatio: '16:9',
                    imageRendering: 'crisp-edges',
                    padding: '10px'
                  }}
                  src={product.productImageURL}
                />
              </>
            );
          })}
        </SliderContainer>
      </MainContainer>
UseEffect to run the scrolltrigger :

  useEffect(() => {
    imageRefs.current.forEach((el, index) => {
      gsap.fromTo(
        el,
        {
          filter: 'blur(2px)'
        },
        {
          duration: 1.2,
          ease: 'power2',
          filter: 'blur(0px)',
          scale: 1.2,
          scrollTrigger: {
            id: `section-${index + 1}`,
            trigger: el,
            start: 'left',
            toggleActions: 'play none none none',
            horizontal: true
          }
        }
      );
    });
  }, []);
Styled components:

export const SliderContainer = styled.div`
  display: flex;
  flex: row;
  justify-content: flex-start;
  flex-wrap: nowrap;
  width: 375px;
  overflow-x: scroll;
`;

export const MainContainer = styled.div`
  overflow-x: hidden;
  max-width: 100%;
`;
Add to refs function: 

  const addToRefs = el => {
    if (el && !imageRefs.current.includes(el)) {
      imageRefs.current.push(el);
      console.log(imageRefs);
    }
  };

  Please help with the same, Thank you in advance ..

Link to comment
Share on other sites

@Ihatetomatoes and others Apologies for creating the one while posting.

 

https://codesandbox.io/s/loving-waterfall-2ceus

 

Please test the same using inspect for iphone..

 

Objective is to be able to scroll and swipe, And as the element enters the viewport from right it should be faded in..Currrently I am not able to set the trigger and start to get the desired effect.

 

Hope I am able to summarize the issue.

 

 

Link to comment
Share on other sites

Not sure if this is the effect you are trying to create but the horizontal scrolling and triggering is working for me.

 

https://codesandbox.io/s/infallible-black-7prsh

 

By default ScrollTrigger uses the viewport as the scroller (the element that has the scrollbar). In your case I had to add .scroller class to your SliderContainer and add it to the scrollTrigger config.

 

scrollTrigger: {
    id: `section-${index + 1}`,
    scroller: ".scroller",
    trigger: el,
    start: "left",
    toggleActions: "play none none none",
    horizontal: true
}

Sorry I can't view this on a phone right now. Hope it helps.

  • Like 3
Link to comment
Share on other sites

2 minutes ago, Ihatetomatoes said:

I think there is an issue on the first load because you are not preloading the images. That means that they have no width when ScrollTrigger is initiated.

How shall I go about it, any article or video I can refer to I have just started learning the React and few days back  GSAP ;-) .. Its fun!

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