出现在屏幕区域时 数字从0累加到指定数字

function setTimer(targetNum,totalTimer,el) {
    var timer =null;
    window.clearInterval(timer);
    function change(targetNum,totalTimer,el) {
        var _space = totalTimer/targetNum;
        var intNum =0;
        var clientH = $(window).height(),
            scrollT = $(window).scrollTop(),
            elH = el.height(),
            offT = el.offset().top;
        if(offT+elH-scrollT>0 && offT+elH-scrollT<clientH){
            if(timer===null){
                timer=window.setInterval(function() {
                    intNum+=1;
                    if(intNum >= targetNum){
                        window.clearInterval(timer);
                        timer=null;
                    }
                    el.html(intNum)
                },_space)
            }
        }else{
            window.clearInterval(timer);
            timer=null;
            el.html(0)
        }
    }
    change(targetNum,totalTimer,el);
    $(window).on('scroll',function () {
        change(targetNum,totalTimer,el);
    })
}
 $('.summary span').each(function () {
        setTimer($(this).html(),8000,$(this))
    })
function setTimer(from,to,timmer,el) {
         var len=to - from,
         _space = timmer/len;
         var timer =null;
         clearInterval(timer);
          var intNum =from;
          timer=setInterval(function() {
              intNum+=1;
              if(intNum >= len){
                  clearInterval(timer);
                  timer=null;
              }
              el.html(intNum)
          },_space)
        }
不判断是否出现在屏幕区域 直接执行

 

posted @ 2018-09-06 13:25  dongxiaolei  阅读(712)  评论(0编辑  收藏  举报