Jump to content
Search Community

ScrollTrigger keeps returning Undefined for pin when navigating to another page and then going back to the original page

JustinLP test
Moderator Tag

Recommended Posts

I keep getting this error when I scroll past a section on a page that uses ScrollTrigger and MagicPin() and then go to another page and then hit the back button.  "ScrollTrigger.js:1384 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'pin')". See the code for the component below. I keep doing console.log, but I can't figure out why the pin parameter is still undefined and it only happens when using the back button. I added "await" to it in "async" and everything.

 

import {Component} from 'bona';
import gsap from 'gsap';
import ScrollTrigger from 'gsap/ScrollTrigger';
import {magicSlideElementAppear} from '../lib/transitions';
import Swiper from 'swiper';
import {Autoplay, Navigation} from 'swiper/modules';

export default class Worth extends Component {
    constructor() {
        super(...arguments);

        this.header = this.el.querySelector('.worth-header');
        this.action = this.el.querySelector('.worth-action');
        this.carousel = this.el.querySelector('.worth-carousel');
        this.items = this.el.querySelector('.worth-items');
        this.item = this.el.querySelectorAll('.worth-item');
        this.main = this.el.querySelector('.worth');
        this.mm = gsap.matchMedia();
    }

    async onInit() {
        await this.magicPin();

        this.magicShow();
        this.initCarousel();
    }

    async onDestroy() {
        this.mm.kill(true);
    }

    magicPin() {
        ScrollTrigger.matchMedia({
            '(min-width:768px)': () => {
                ScrollTrigger.create({
                    trigger: this.main,
                    pin: true,
                    pinSpacing: false,
                });
            },
        });
    }

    initCarousel() {
        this.mm.add('(min-width: 768px)', () => {
            this.worthCarousel = new Swiper(this.carousel, {
                modules: [Autoplay],
                wrapperClass: 'worth-items',
                slideClass: 'worth-item',
                slidesPerView: 'auto',
                speed: 8500,
                loop: true,
                allowTouchMove: false,
                autoplay: {
                    delay: 0,
                    disableOnInteraction: false
                }
            });

            return () => this.worthCarousel.destroy(true, true);
        });
    }

    magicShow() {
        if (this.header) magicSlideElementAppear(this.header);
        if (this.action) magicSlideElementAppear(this.action);
        if (this.carousel) magicSlideElementAppear(this.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

Maybe try using GSAP Context in order to keep everything in just one scope.

 

https://gsap.com/docs/v3/GSAP/gsap.context()

 

Right now you're creating a GSAP MatchMedia instance and a ScrollTrigger.matchMedia instance in separate methods, any particular reason for that? IMHO that's just messy and most likely your ScrollTrigger instance is not being reverted. Also always markers and IDs in your ScrollTrigger instances when debugging an issue, if you add a specific id and markers to your ScrollTrigger instances, most likely you'll keep seeing those markers when you navigate to another route.

 

Happy Tweening!

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