gridsome: Cannot addEventListener (scroll)

Description

I am attempting to add eventlisteners for scrolling, something that I am familiar with in VueJS at least, but I cannot get it to work in Gridsome.

Steps to reproduce

I am using the below code in my Template. I have tried this code in a page component as well, and even created a custom App.vue to see if I had to put it there.

created () { window.addEventListener('scroll', this.handleScroll); }, destroyed () { window.removeEventListener('scroll', this.handleScroll); }, methods: { handleScroll (event) { console.log("Scrolling") } }

Expected result

Logging “Scrolling” when the user is scrolling.

Actual result

Nothing is being logged. If I register this.handleScroll() instead the method is being called once (obviously I guess, dont know if that says anything though).

Environment

System: OS: Linux 4.15 Ubuntu 18.04.4 LTS (Bionic Beaver) CPU: (4) x64 Intel® Core™ i7-7500U CPU @ 2.70GHz Binaries: Node: 12.16.3 - /usr/bin/node Yarn: 1.22.4 - /usr/bin/yarn npm: 6.14.4 - /usr/local/bin/npm Browsers: Chrome: 81.0.4044.129 Firefox: 75.0 npmPackages: @gridsome/source-graphql: ^0.1.0 => 0.1.0 gridsome: ^0.7.0 => 0.7.13 gridsome-plugin-simple-analytics: ^1.1.0 => 1.1.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 26

Most upvoted comments

By the way, you may want to set overflow-y to auto in case you don’t want vertical scrollbar when there’s content that does not cover the viewport height.

Actually I just remembered that is exactly what I want, since otherwise the layout will jump whenever the vertical scrollbar appears.

No problem. I’ll create a simple fiddle to test out scroll event bubbling since I have some doubts on it as well, and share here in a bit.

Does that work on dev server, but you see a window not defined error during build?

If so, try switching that created() to mounted().