Giter Site home page Giter Site logo

panjiachen / vue-countto Goto Github PK

View Code? Open in Web Editor NEW
1.4K 20.0 323.0 371 KB

It's a vue component that will count to a target number at a specified duration https://panjiachen.github.io/countTo/demo/

JavaScript 74.59% Vue 25.41%
vue vue-component countup

vue-countto's Introduction

vue-countTo

It's a vue component that will count to a target number at a specified duration

vue2 Gemnasium license npm npm minified gzip

vue-countTo is a dependency-free, lightweight vue component that can be overwrited easingFn by yourself. You can set startVal and endVal ,it will automatic judge count up or count down. It is support vue-ssr. It is learn from countUp.js;

How to use?

npm install vue-count-to

Example

<template>
  <countTo :startVal='startVal' :endVal='endVal' :duration='3000'></countTo>
</template>

<script>
  import countTo from 'vue-count-to';
  export default {
    components: { countTo },
    data () {
      return {
        startVal: 0,
        endVal: 2017
      }
    }
  }
</script>

demo:

demo

Use CDN Script: demo

Options

Property Description type default
startVal the value you want to begin at Number 0
endVal the value you want to arrive at Number 2017
duration duration in millisecond Number 3000
autoplay when mounted autoplay Boolean true
decimals the number of decimal places to show Number 0
decimal the split decimal String .
separator the separator String ,
prefix the prefix String ''
suffix the suffix String ''
useEasing is use easing function Boolean true
easingFn the easing function Function

** notes: when autoplay:true , it will auto start when startVal or endVal change **

Functions

Function Name Description
mountedCallback when mounted will emit mountedCallback
start start the countTo
pause pause the countTo
reset reset the countTo

vue-countto's People

Contributors

luisdanielroviracontreras avatar panjiachen avatar unclelian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-countto's Issues

关于源码 props 中 easingFn问题

easingFn: { type: Function, default(t, b, c, d) { return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b; } }

子组件里 easingFn 只在methods方法里内部调用,为什么不直接在methods写

关于prop 的decimals重定义

vue.runtime.esm.js?2b0e:619 [Vue warn]: Method "decimals" has already been defined as a prop.

found in

---> <CountTo> at node_modules/vue-count-to/src/vue-countTo.vue
       <Box> at src/components/box.vue
         <Home> at src/views/Home.vue
           <App> at src/App.vue
             <Root>

项目中mixin.js 有这个method ,使用的decimal作为命名,会产生这个问题

separator参数无效

按照官网DEMO
separator 加了冒号会报错,不加会无效

<count-to :start-val="0" :end-val="25" :duration="2600" :separator="" class="box-num"/>

自动播放的问题

endVal变化时,需要手动调用start来启动动画。
建议在autoplay时, watch endVal自动开启动画

请教下一个easingFn的问题

easingFn: {
  type: Function,
  default(t, b, c, d) {
    // 类似这一块有啥计算方式,这个计算公司如何可得?
    return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
  }
}

Could not find a declaration file for module 'vue-count-to'.

[{
	"resource": "components/PanelGroup.vue",
	"owner": "_generated_diagnostic_collection_name_#2",
	"code": "7016",
	"severity": 8,
	"message": "Could not find a declaration file for module 'vue-count-to'. 'admin/node_modules/.pnpm/[email protected]/node_modules/vue-count-to/dist/vue-count-to.min.js' implicitly has an 'any' type.\n  Try `npm i --save-dev @types/vue-count-to` if it exists or add a new declaration (.d.ts) file containing `declare module 'vue-count-to';`",
	"source": "Vetur",
	"startLineNumber": 59,
	"startColumn": 21,
	"endLineNumber": 59,
	"endColumn": 35
}]

a little suggestion

use slot,wo we can customized the displayValue
like this:
9S@`K41EPV4PLK$@{}N0IPK
so we can use like this:
<count-to :end-val="3000" v-slot:default="{displayValue}"{{displayValue||pipeline}}<count-to

start()方法调用失败

报错提示:
Uncaught TypeError: this.$refs.refresh1.start is not a function
引入
import countTo from 'vue-count-to'
vue:
<countTo ref="refresh1" :startVal='0' :endVal='3000' :duration='3000'></countTo>
script:
this.$refs.refresh1.start()
其实是想做一个点击不管数据有无变化都会重新开始动画的效果,不知道问题出在哪里。

场景:递增,ease: fasle 的时候,数值不变化

看了代码发现是这里写错了this.printVal = this.localStartVal + (this.localStartVal - this.startVal) * (progress / this.localDuration)
改为:this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration) 运行正常

Server Side Rendering is broken

As you rely on "window" to be present, this little sweet component explodes in frameworks like NuxtJS. Could you try to avoid relying on window? For what reason do you use it anyway?

make update possible

would be very nice to be able to update the values without the need to manually change both the start end endval.

In my setup I have a vote mechanism where I display several options with percentage values. Anytime a user votes, the values update automatically. The issue with this component is that I can't update the values from its current values (like from 50 to 80 percent) without braking it. Any Idea how to manage that usecase?

grafik

By the way, thanks for that cool component. Keep up the good workd ;)

麻烦可以添加LICENSE文件吗

如题,可以添加合适的license文件吗?想要在商业项目中使用此组件,但没看到作者是否允许此行为。

code

c40649f0fa5069c69439c970d5714d12bf827566

About `autoplay`

<count-to :startVal='0' :endVal='reportCount'></count-to>

created () {
   axios.get('/api/admin/report/count')
     .then(response => {
       this.reportCount = response.data.reportCount
     })
}

以上组件会在加载时自动开始向 2017 (默认值) 计数, 即使数据请求尚未返回.

<count-to :startVal='0' :endVal='reportCount' :autoplay='false'></count-to>

这样需要手动触发计数很不方便.

<count-to :startVal='0' :endVal='reportCount'></count-to>

data () {
  reportCount: 0
}

这样其实会在加载时开始计数, 只不过一直是 0, 如果数据返回有延迟, 计数值会出现跳跃.

Workaround:

<count-to :startVal='0' :endVal='reportCount' :autoplay="true" v-if="typeof reportCount !== 'undefined'"></count-to>

这样可以使组件在请求返回后才开始加载.


我向 CountTo 组件添加了一个参数 autoplayWhenChange

props: {
  autoplayWhenChange: {
    type: Boolean,
    required: false,
    default: true
  }
}

并修改监视器

watch: {
  startVal () {
    if (this.autoplay || this.autoplayWhenChange) {
      this.start()
    }
  },
  endVal () {
    if (this.autoplay || this.autoplayWhenChange) {
      this.start()
    }
  }
}

<count-to :startVal='0' :endVal='reportCount' :autoplay="false" :autoplayWhenChange="true"></count-to>

这样可以实现在请求返回后开始计数, 同时避免数字不连续的跳跃.

这样不知道是不是一个合理的 feature.

解决数字匀速递增的问题

<countTo class="count_to" :startVal='startVal-50' :endVal='endVal' :duration='100000' :easingFn="easingFn"></countTo>

methods: {

easingFn(t, b, c, d){

let y = c*t/d+b; //此方法为匀速

return y;

//return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b; //原方案为先快后慢

}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.