Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a use-case where I want to change CSS class dynamically based on scroll postiion, which is described here.

I was thinking of using vue-scroll for this. I made following changes:

Installing using npm:

npm install vue-scroll --save

made following changes in main.js:

import Vue from 'vue'
import vScroll from 'vue-scroll'
Vue.use(vScroll)

added following in one of the component, where I want to use it:

<div  v-scroll="onScroll">
   ...
   ...
   <p>scrollTop:<span>{{position &&  position.scrollTop}}</span></p>
   ...
   ...
</div>

and following changes in the component:

export default {
  data () {
    return {
      position: null
    }
  },
 methods: {
    onScroll (e, position) {
      console.log('comes here ' + position)
      this.position = position
    }
},

However this is not working, I also tried to register this as a directive in main.js, like following, but this also did not help.

const app = new Vue({
  router,
  store,
  el: '#app',
  template: '<App/>',
  components: { App }, // Object spread copying everything from App.vue
  directives: {
    'v-scroll': vScroll  // tried 'vScroll': vScroll as well
  }
})

What can be possible reason of this not working.

share|improve this question
    
I think you should set v-scroll="onScroll" on body tag – Belmin Bedak Dec 12 at 8:23
    
@BelminBedak I tried that as well, didn't help. – saurabh Dec 12 at 8:35

This lib doesn't seems to be in a very stable state, I managed to make it work using the @scroll='onScroll' or v-on:scroll='onScroll syntax. The position arg don't work tough, you need to use element.scrollTop.

It looks like the documentation is outdated, probably the syntax shown in the documentation is regarding vue 1.x.

EDITED

Actually, I was completly wrong about this, v-on:scroll and @scroll will be handled by vue itself, no need for the vue-scroll lib. The vue-scroll lib is actually no working in the example I linked.

I guess you can use vue's built in scroll instead of the vue-scroll lib

You can see a working example here

share|improve this answer
    
This is not working, neither I found something like this in vue documentation. – saurabh Dec 15 at 6:49
    
Is the example not working? The link I posted works for me, I tested with vue 2.x and it works too. – tiagohngl Dec 15 at 10:20
    
I didn't find anything regarding v-on:scroll in vue docs, but the v-on or @ syntax works by adding this handler in the current element, so this will basically do addEventListener('scroll', onScroll) in the current element. Maybe your element is not scrollable and what you want is to add the event on the window object, then this approach will not work. – tiagohngl Dec 15 at 10:33
    
Yes, thats what I also just concluded, you code work as height of div is less than section, and you got that scroll location, this worked for me. – saurabh Dec 15 at 10:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.