写一个基于zepto的计数器

这里是用$.fn扩展一个基于zepto的方法,使用时直接引用即可,使用如下:

html代码如下: <span class="count"></span>

调用时js写法:    

//下面这块直接复制

$.fn.countNum = function(s, e, d, i) {

//s为起始数字,e为结束数字,d为持续时间(ms),i为间隔多少ms执行一次
if (!s) s = 0;
if (!e && e != 0) e = 100;
if (!d) d = 1000;
if (!i) i = 50;
if (s > e) {
var m = e;
e = s;
s = m;
}
var p = (e - s) / (d / i);
var obj = this;
var timer = setInterval(function() {
if (s >= e) {
clearInterval(timer);
obj.html(e);
} else {
obj.html(Math.ceil(s));
s += p;
}
},
p);
}

//这里直接引用即可

$(".count").countNum(1,100,3000);

posted @ 2015-05-06 10:10  雷林007  阅读(64)  评论(0编辑  收藏  举报