Jump to content
Search Community

infinite slider

Last test
Moderator Tag

Recommended Posts

Can anyone help me transform this carousel into infinity? Every time it reaches the end it returns to the beginning, leaving it infinity? I believe you should use wrap but I couldn't understand how to do that.
 
image.png.26fdf286d7b910352e0e9d199de669ff.png
 
 
'use client';
 
import { useLayoutEffect, useRef } from 'react';
import gsap from 'gsap';
import { ScrollTrigger } from 'gsap/all';
 
const Carousel = () => {
  const slider = useRef(null);
  const firstSlider = useRef(null);
  const secondSlider = useRef(null);
  let xPercent = 0;
  let direction = -1;
 
  useLayoutEffect(() => {
    gsap.registerPlugin(ScrollTrigger);
    gsap.to(slider.current, {
      scrollTrigger: {
        trigger: document.documentElement,
        scrub: 0.25,
        start: 0,
        end: window.innerHeight,
        onUpdate: (e) => (direction = e.direction * -1),
      },
      // x: '-500px',
    });
    requestAnimationFrame(animate);
  }, []);
 
  const animate = () => {
    if (xPercent < -100) {
      xPercent = 0;
    } else if (xPercent > 0) {
      xPercent = -100;
    }
    gsap.set(firstSlider.current, { xPercent: xPercent * -1 });
    gsap.set(secondSlider.current, { xPercent: xPercent });
    requestAnimationFrame(animate);
    xPercent += 0.01 * direction;
  };
 
  return (
    <div
      ref={slider}
      className='flex flex-col items-center justify-center w-full h-full gap-y-10'
    >
      <div
        className='flex gap-x-10'
        ref={firstSlider}
      >
        {Array(20)
          .fill(null)
          .map((item, index) => (
            <div
              key={index}
              className='bg-blue-500 w-36 h-36'
            >
              {index}
            </div>
          ))}
      </div>
 
      <div
        className='flex gap-x-10'
        ref={secondSlider}
      >
        {Array(20)
          .fill(null)
          .map((item, index) => (
            <div
              key={index}
              className='bg-purple-500 w-36 h-36'
            >
              {index}
            </div>
          ))}
      </div>
    </div>
  );
};
 
export default Carousel;
 
 
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

Hi @Last and welcome to the GSAP Forums!

 

Besides echoing the need for a minimal demo I'm super curious about why you're doing this:

const animate = () => {
  if (xPercent < -100) {
    xPercent = 0;
  } else if (xPercent > 0) {
    xPercent = -100;
  }
  gsap.set(firstSlider.current, { xPercent: xPercent * -1 });
  gsap.set(secondSlider.current, { xPercent: xPercent });
  requestAnimationFrame(animate);
  xPercent += 0.01 * direction;
};

Finally maybe you're looking for our Seamless Horizontal Loop helper function:

https://gsap.com/docs/v3/HelperFunctions/helpers/seamlessLoop

 

Here are a couple of demos in React using our useGSAP hook:

https://stackblitz.com/edit/vitejs-vite-auctqy?file=src%2FApp.jsx

 

https://stackblitz.com/edit/vitejs-vite-cljwjs?file=src%2FApp.jsx

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

Thank you, thanks to the links I managed to solve my problem. I just have one last question now, which of the two approaches is more performant using 2 useGSAP or just 1 useGSAP.
I'm currently only using useGSAP but I would like to know if I can start using several useGSAP in my application without problems
 
Thank you for your help🤝
 
 
 // useGSAP(
  //   () => {
  //     const speed = 2.5;

  //     const loop = horizontalLoop('.nft-card-left', {
  //       repeat: -1,
  //       speed: 1.5,
  //       paddingRight: 16,
  //     });

  //     let tl;

  //     Observer.create({
  //       target: window,
  //       type: 'wheel',
  //       onChangeY: (self) => {
  //         tl && tl.kill();
  //         const factor = self.deltaY > 0 ? 1 : -1;
  //         tl = gsap
  //           .timeline()
  //           .to(loop, { timeScale: speed * factor, duration: 0.25 })
  //           .to(loop, { timeScale: 1 * factor, duration: 1 });
  //       },
  //     });
  //   },
  //   {
  //     scope: container,
  //   },
  // );

  // useGSAP(
  //   () => {
  //     const speed = 2.5;

  //     const loop = horizontalLoop('.nft-card', {
  //       reversed: 1,
  //       repeat: -1,
  //       speed: 1.5,
  //       paddingRight: 16,
  //     });

  //     let tl;

  //     Observer.create({
  //       target: window,
  //       type: 'wheel',
  //       onChangeY: (self) => {
  //         tl && tl.kill();
  //         const factor = self.deltaY > 0 ? -1 : 1;
  //         tl = gsap
  //           .timeline()
  //           .to(loop, { timeScale: speed * factor, duration: 0.25 })
  //           .to(loop, { timeScale: 1 * factor, duration: 1 });
  //       },
  //     });
  //   },
  //   {
  //     scope: container,
  //   },
  // );

  useGSAP(
    () => {
      const speed = 2.5;

      const loopLeft = horizontalLoop('.nft-card-left', {
        repeat: -1,
        speed: 1.5,
        paddingRight: 16,
      });

      const loopRight = horizontalLoop('.nft-card', {
        reversed: 1,
        repeat: -1,
        speed: 1.5,
        paddingRight: 16,
      });

      let tlLeft, tlRight;

      Observer.create({
        target: window,
        type: 'wheel',
        onChangeY: (self) => {
          tlLeft && tlLeft.kill();
          tlRight && tlRight.kill();

          const factor = self.deltaY > 0 ? -1 : 1;
          const factorLeft = self.deltaY > 0 ? 1 : -1;

          tlLeft = gsap
            .timeline()
            .to(loopLeft, { timeScale: speed * factorLeft, duration: 0.25 })
            .to(loopLeft, { timeScale: 1 * factorLeft, duration: 1 });

          tlRight = gsap
            .timeline()
            .to(loopRight, { timeScale: speed * factor, duration: 0.25 })
            .to(loopRight, { timeScale: 1 * factor, duration: 1 });
        },
      });
    },
    {
      scope: container,
    },
  );

 

 

 

image.png

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