Skip to content
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.

How to properly remove event listener on window.object? #82

Open
@sebastianjung

Description

Hey there,

first of all thank you for the awesome library. Was actually the only one i found which fits the need of the project i'm working on.

I'm using it in conjuction with nuxt 2 / vue 2 and having a hard time making it work correctly.

Reproducing the error:

  1. Entering the site first time: Scroll works as expected
  2. going to another page where asscroll is not imported and used. Then revisit the site with asscroll: Scroll is double the speed
  3. going back in browser once more and revisit the same page once more: scroll is tripled
  4. repeat

For further information you might want to check out the bug yourself here:
https://front.ausbildung.stage.sma.pippis.zone/berufsbilder-und-studiengaenge

Click one of the items and go back in browser history and revisit one item.

When i looked into it more i found out that the event listeners on the window object are not removed when using asscroll.disable() or asscroll.off('update', someRandomFunction)

Instead they get duplicated for each revisit of the site.

Is this intended behaviour? And If so: How to properly disconnect the scroll listener on window object on site-leave / beforeDestroy()

Any help is very much appreciated.

And again: Thank you for the library!

Sebastian

EDIT:
Some more info:
In mounted() i init asscroll with this function:

initAsscrollAndAnimations() {
      import('@ashthornton/asscroll').then((ASScroll) => {
        const constructor = ASScroll.default
        this.asscroll = new constructor({
          containerElement: this.$refs.HorizontalScrollContainer.$el,
          disableRaf: true
        })

        // use gsap.ticker instead of request animation frame
        gsap.ticker.add(this.asscroll.update)

        // setup scrolltrigger scroller proxy
        ScrollTrigger.scrollerProxy(this.asscroll.containerElement, {
          scrollLeft: (value) => {
            return arguments.length
              ? (this.asscroll.currentPos = value)
              : this.asscroll.currentPos
          },
          getBoundingClientRect: () => {
            return {
              top: 0,
              left: 0,
              width: window.innerWidth,
              height: window.innerHeight
            }
          }
        })

        // add event listener on asscroll
        this.asscroll.on('update', this.onAsscrollUpdate)
        // add resize listeners
        ScrollTrigger.addEventListener('refresh', this.asscroll.resize)

        /*
          place gsap animations here
        */
        //  STICKY APPLY NOW BTN
        if (this.jobProfile.attributes.allowApplication) {
          // get apply now button
          this.createStickyFlyOutAnimation()
        }
        // PARALLAXY FLOATING ITEMS
        this.createParallaxAnimations()
        // RELATED JOB PROFILES SECTION
        if (this.hasJobProfileRelatedJobProfiles) {
          this.createRelatedSectionPopoutAnimation()
        }
        /* 
          end gsap animations
        */
        this.asscroll.enable({
          horizontalScroll: true
        })
      })
    },

I think the gsap stuff can be ignored as uncommenting it and using asscroll with its own RAF does not solve the problem.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions