原生js平滑滚动 【我写的很菜】

    /**
* 平滑滚动
* @param target[document.Element] 滚动目标
* @param lengthTo[number] 滚动到达
* @param type[string] 滚动x/y轴
* @param callback[function] 滚动后回调
*/
function roll(target,lengthTo,type,callback){
  var type = type || "x";
  var dd = target,
      dnow = (type == "x") ? dd.scrollLeft : dd.scrollTop;
      dmin = 0,
      dmax = (type == "x") ? dd.scrollWidth : dd.scrollHeight,
      dto = (lengthTo > dmin) ? ( ( lengthTo < dmax ) ? lengthTo : dmax ): dmin,
      step = 20,
      distance = Math.abs (dto - dnow),
      count = 1 ,
      direction = (dto > dnow)? 1 : -1 ;
       (function() {
         var timePre = count / step;
         var dnow = (type == "x") ? dd.scrollLeft : dd.scrollTop;
         var speed = Math.floor( distance / step ) ;
         // speed = Tween.Quad.easeIn(1,0, speed * 1.5  , 0.5 + 0.1 * count); //需要有缓动函数支持
         var move = dnow + direction * speed ;

// console.log(distance,speed , move);

         if(timePre > 1 || Math.abs(move - dto ) <= Math.abs(speed)){
           (type == "x") ? target.scrollLeft = dto :  target.scrollTop = dto;
                     if(typeof callback =="function"){
                         callback();
                     }
           return ;
         }
         (type == "x") ? target.scrollLeft = move : target.scrollTop = move;

         count++;
         setTimeout(arguments.callee, 40);
       })();
}