Jump to content
Search Community

Fix a broken auto-play, draggable & inertia, marquee animation for SvelteKit project

Krause test
Moderator Tag

Recommended Posts

Hi there beloved community.

 

I'm in the process of launching a new portfolio but there's a bug with my GSAP-based marquee which showcases my projects in a gallery. From what I can see it looks like my code doesn't calculate the proper width & height of all of the media's inside of the marquee. And sometimes on load it only loads some of the actual content inside.

 

Another thing: the marquee height is clamped, and inside of my runMarquee function it looks like the height doesn't adapt when resizing.

 

I use:

SvelteKit (framework)

Hygraph (GraphQL)

 

Another issue I'm facing is that most of my content inside of the marquee's has different aspect-ratios, especially the videos doesn't load in the proper format. So please also look for a solution to this.

 

Preview link of website:

https://krause-ew9w4q45a-asgerkrause.vercel.app/

sometimes it works and sometimes it doesn't. I need it to be bullet-proof.

 

If you have a stronger code for the same results, please let me know!

 

HTML structure:

<div class="marquee">
  <div class="track">
    <!-- Media will go here -->
    <video autoplay loop muted src={url} type="video/mp4" />
    <img src={url} alt="" />
  </div>
</div>

 

CSS:

.marquee {
  height: clamp(18.75rem, 12.5rem + 16.6667vw, 25rem);
  position: relative;
  overflow: hidden;
  display: block;
  margin-left: calc(var(--space) * -1);
  width: 100vw;
}

.marquee .track {
  height: 100%;
  transform-origin: 0 0;
  display: block;
  position: relative;
}

.marquee .track > * {
  height: 100%;
  width: auto;
  padding-left: 4px;
  position: absolute;
  object-fit: cover;
}

 

JS:

onMount(() => {
  function runMarquee() {
    const allMarquees = document.querySelectorAll('.marquee');
    allMarquees.forEach((marquee, index) => {
      marquee.querySelector('.track');
      const allItems = marquee.querySelectorAll('.marquee>.track>*'),
            proxy = document.createElement('div');
      allItems.length;
      let totalX = 0,
          marqueeH = 0;
      marquee.offsetWidth;
      allItems.forEach((item, i) => {
        const itemW = item.offsetWidth,
              itemH = item.offsetHeight;
        (totalX += itemW),
          gsap.set(item, {
          x: totalX,
          width: itemW,
          height: itemH
        }),
          itemH > marqueeH && (marqueeH = itemH);
      });
      const marqueeVal = gsap.utils.wrap(0, totalX),
            marqueeProgress = gsap.utils.wrap(0, 1);
      gsap.set([marquee], {
        height: marqueeH
      });
      const stringX = `-=${totalX}`,
            animation = gsap.to(allItems, {
              repeat: -1,
              duration: 300,
              x: stringX,
              ease: 'none',
              paused: !0,
              modifiers: {
                x: function (x, target) {
                  return `${(x =
                             ((parseInt(x) - totalX) % totalX) + (totalX - target.offsetWidth))}px`;
                }
              }
            });

      function updateProgress() {
        const dragValue = marqueeVal((this.deltaX / 2) * -1) / totalX,
              currentProgressAnim = animation.progress(),
              endProgress = marqueeProgress(currentProgressAnim + dragValue);
        animation.progress(endProgress);
      }
      Draggable.create(proxy, {
        type: 'x',
        trigger: marquee,
        inertia: !0,
        onDrag: updateProgress,
        onThrowUpdate: updateProgress
      }),
        window.addEventListener('resize', function resize() {
        animation.render(animation.time(), !1, !0);
      }),
        animation.play();
    });
  }
  runMarquee();
});

 

Thanks :-)

CleanShot 2023-08-16 at 1.41.42.gif

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates the issue? We can't really troubleshoot a live site (I saw you provided a link).

 

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 dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

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

 

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