vue-countup-v2 icon indicating copy to clipboard operation
vue-countup-v2 copied to clipboard

How to start countup only when scrolled?

Open sofyanlabs opened this issue 4 years ago • 7 comments

I want to start Countup Event when div scrolled, not on page load.

how to implement this?

sofyanlabs avatar May 12 '20 04:05 sofyanlabs

When div scrolled, call instance.update().

xlsdg avatar Jun 01 '20 03:06 xlsdg

can you give me example of code for this one?

sofyanlabs avatar Jun 01 '20 08:06 sofyanlabs

you can use :delay="-1" and vue-in-viewport-mixin to trigger instance.start().

unek avatar Jun 18 '20 17:06 unek

I have same issue. Can you share any live example?

uzzal-ht avatar Jun 28 '20 10:06 uzzal-ht

would appreciate live example as well

babyradJiri avatar Jul 16 '20 19:07 babyradJiri

I have same issue. Can you share any live example? would appreciate live example as well can you give me example of code for this one? When div scrolled, call instance.update().

Hello, I was having problems of this type and finally I solved it as follows.

First add the scroll directive before your Vue instance:

Vue.directive ('scroll', {
  inserted: function (el, binding) {
    let f = function (evt) {
      if (binding.value (evt, el)) {
        window.removeEventListener ('scroll', f)
      }
    }
    window.addEventListener ('scroll', f)
  }
})

Then, where they place the component, they add the attribute ref = "firstcomponent" (change the name of the ref) and v-scroll = "onReady" (method that will update the counter) to each component that they want to "update" to the time to scroll.

Example:

<div class="iCountUp">
<ICountUp :delay="delay" :endVal="endVal" :options="options" ref="firstcomponent" v-scroll="onReady"/>
</div>

Finally, in the method, they can do an if of this type:

if (window.scrollY> 800) {
                this.$refs.firstcomponent.update(that.endVal + 100);
 }

By doing this, each time the scrollY is greater than 800, each component will be updated with the assigned value, in this case "100".

ghost avatar Jul 18 '20 19:07 ghost

There is already a scroll spy in the countup.js library, but because instance.start() is always called, this option (enableScrollSpy) doesn't work with vue-countup-v2. PR needs to be made to only run .start() when enableScrollSpy is disabled

laurensV avatar Jun 14 '22 10:06 laurensV