Jump to content
Search Community

Getting seemingly random jumps after scrolling past pinned ScrollTrigger

James0r test
Moderator Tag

Recommended Posts

My code looks like 

 

 this.scrollTrigger = ScrollTrigger.create({
          markers: false,
          trigger: this.componentEl,
          start: `top top`,
          pin: true,
          normalizeScroll: true,
          end: function() {
            return '+=' + window.innerHeight * items.length
          },
          toggleClass: 'is-active',
          onEnter: function() {
            items[0].classList.add('is-active')
            componentEl.dataset.activeIndex = 0
          },
          onLeaveBack: function() {
            items[0].classList.remove('is-active')
            componentEl.removeAttribute('data-active-index')
          },
          onUpdate: function(self) {
            const previousIndex = currentIndex

            currentIndex = Math.floor(self.progress * items.length)

            if (previousIndex >= items.length || currentIndex >= items.length)
              return

            if (previousIndex !== currentIndex) {
              items[previousIndex].classList.remove('is-active')
              items[currentIndex].classList.add('is-active')
              componentEl.dataset.activeIndex = currentIndex
            }
          },
        })

The trigger is working as expected on mobile but once I get past the scrollTrigger section and to the accordion section below it and I tap one of the buttons the section expands and it intermittently up or down. 

I can try a minimal reproduction but just wondering if there are any known fixes with the information provided.

 

Thanks!

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

I was able to determine that my scroll trigger container size is being recalculated when the bottom address bar appears on iPhone. Here's the full component. 

 

<?php
$eyebrow_heading = $section['eyebrow_heading'] ?? null;
$blurb = $section['blurb'] ?? null;
$items = $section['items'] ?? null;

$section_wrapper_classes = [];

$section_inner_wrapper_classes = [
  'tw-bg-rd-dark-green',
  'tw-text-white',
  'tw-rd-container-padding',
  'tw-h-[102vh]',
  'tw-py-20',
  'sm:tw-py-0',
];
?>

<div
  id="scroll-trigger-component"
  x-data="rendevorDifference"
  x-intersect.margin.400px.once="refreshScrollTrigger()"
  x-intersect:leave="console.log('left')"
  class="<?php echo implode(' ', $section_wrapper_classes); ?>"
>
  <div class="<?php echo implode(' ', $section_inner_wrapper_classes); ?>">
    <div class="tw-container tw-flex tw-flex-col tw-justify-center tw-h-full">
      <?php if ($eyebrow_heading) : ?>
      <h2 class="tw-font-medium tw-tracking-widest tw-text-fluid-step-0 tw-mb-8 sm:tw-mb-14 tw-mt-4 sm:tw-mt-0">
        <?php echo $eyebrow_heading; ?>
      </h2>
      <?php endif; ?>
      <div class="tw-block tw-flex tw-flex-col sm:tw-flex-row sm:tw-items-center sm:tw-h-[460px]">
        <div
          data-blurb
          class="sm:tw-flex-[0_0_50%] tw-mb-10 sm:tw-mb-0"
        >
          <?php if ($blurb) : ?>
          <h3 class="tw-text-fluid-step-6 tw-leading-[1.3]">
            <?php echo $blurb; ?>
          </h3>
          <?php endif; ?>
        </div>
        <?php
        $reveal_pane_classes = [
          'tw-relative',
          'sm:tw-flex-[0_0_50%]',
          'sm:tw-pl-[min(152px,_8vw)]',
          'lg:tw-pl-[min(152px,_12vw)]',
          'sm:tw-border-l',
          'sm:tw-flex',
          'tw-flex-col',
          'tw-justify-center',
          'tw-overflow-hidden',
          'tw-h-[32vh]',
          'sm:tw-h-full',
          'tw-mt-10',
          'sm:tw-mt-0',
        ];
        ?>
        <div class="rd-difference-divider tw-block tw-h-[1px] tw-bg-white sm:tw-hidden"></div>
        <div
          data-reveal-pane
          class="<?php echo implode(' ', $reveal_pane_classes); ?>"
        >
          <?php foreach ($items as $index => $item) : ?>
          <style>
          [data-active-index="<?php echo $index; ?>"] .highlight-<?php echo $index+1;

          ?> {
            transition: background-color 0.5s, color 0.5s;
            background-color: <?php echo $item['accent_color'];
            ?>;
            color: var(--color-rd-dark-green-500);
          }
          </style>
          <div class="rd-reveal-item">
            <?php if ($item['title'] ?? null) : ?>
            <h4
              class="tw-font-medium tw-tracking-widest tw-text-fluid-step-0 tw-mb-4 tw-uppercase"
              style="color: <?php echo $item['accent_color']; ?>;"
            >
              <?php echo $item['title']; ?>
            </h4>
            <?php endif; ?>
            <?php if ($item['body_text'] ?? null) : ?>
            <div class="tw-text-fluid-step--1 sm:tw-text-fluid-step-0 sm:tw-max-w-[280px] tw-font-display">
              <?php echo $item['body_text']; ?>
            </div>
            <?php endif; ?>
          </div>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
  </div>
</div>

<?php if ($section_is_first_instance) : ?>
<style>
:root {
  scroll-behavior: smooth;
}

.rd-reveal-item {
  opacity: 0;
  transition: opacity 0.5s;
  position: absolute;
}

.rd-reveal-item.is-active {
  opacity: 1;
}
</style>

<script>
document.addEventListener('alpine:init', () => {
  Alpine.data('rendevorDifference', function() {
    return {
      componentEl: this.$el,
      scrollTrigger: null,
      init() {
        this.initScrollTrigger();
        // document.addEventListener('scroll', _.throttle(this.refreshScrollTrigger.bind(this), 500))
      },
      refreshScrollTrigger() {
        console.log('refreshing scroll trigger')

        this.scrollTrigger.refresh()
      },
      initScrollTrigger() {
        const items = gsap.utils.toArray(".rd-reveal-item")
        const componentEl = this.componentEl
        let currentIndex = 0

        this.scrollTrigger = ScrollTrigger.create({
          markers: false,
          trigger: this.componentEl,
          start: `top top`,
          pin: true,
          normalizeScroll: true,
          end: function() {
            return '+=' + window.innerHeight * items.length
          },
          toggleClass: 'is-active',
          onEnter: function() {
            items[0].classList.add('is-active')
            componentEl.dataset.activeIndex = 0
          },
          onLeaveBack: function() {
            items[0].classList.remove('is-active')
            componentEl.removeAttribute('data-active-index')
          },
          onUpdate: function(self) {
            const previousIndex = currentIndex

            currentIndex = Math.floor(self.progress * items.length)

            if (previousIndex >= items.length || currentIndex >= items.length)
              return

            if (previousIndex !== currentIndex) {
              items[previousIndex].classList.remove('is-active')
              items[currentIndex].classList.add('is-active')
              componentEl.dataset.activeIndex = currentIndex
            }
          },
        })
      }
    }
  })
})
</script>
<?php endif; ?>

 

Link to comment
Share on other sites

No no, we need an functional minimal demo, like a CodePen or Stackblitz that illustrates the issue so that we can tinker with it and save a fork to show you. Just looking at a huge copy/paste of your code isn't very helpful. 

 

Definitely do NOT do this: 

scroll-behavior: smooth;

And don't apply any CSS transitions to things that GSAP affects because those will totally interfere. Every time GSAP tries to make a change to a property, the browser will say "NOPE! I'm gonna drag that change out over time..."

 

If you still need help, please make sure you provide a minimal demo (CodePen or Stackblitz). Also, make sure you're using the latest version of GSAP/ScrollTrigger. 

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