user431619
user431619

Reputation:

Vue2 window addEventListener scroll doesnt fire?

I have a component, and I simply cannot get

window.addEventListener("scroll", function(){
   console.log('scrolling');
});

to fire - I've tried to attach it to the created and mounted lifecycle hooks, however, it doesn't print to the console when scrolling.

At the moment I have the following, but still no luck. It fires the console.log("My Method") but not the scroll :(

 export default {
  data() {
      return {

      }
  },
  components: {

  },
  methods: {
    myMethod(){
      console.log("my method");
      window.addEventListener("scroll", function(){
        console.log('scrolling');
      });
    }
  },
  created() {
     console.log("created");

  },
  mounted(){
    console.log("mounted");
    this.myMethod();
  }
}

Upvotes: 5

Views: 2172

Answers (1)

Romick
Romick

Reputation: 163

Have you tried this:

window.addEventListener("scroll", function () {
  console.log("scroll");
}, true);

?

Upvotes: 1

Related Questions