qscrollTo.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * 滚动条
  3. * @param options 自定义参数
  4. */
  5. $.fn.scrollTo =function(options){
  6. var defaults = {
  7. toT : 0, //滚动目标位置
  8. durTime : 500, //过渡动画时间
  9. delay : 30, //定时器时间
  10. callback:null //回调函数
  11. };
  12. var opts = $.extend(defaults,options),
  13. timer = null,
  14. _this = this,
  15. curTop = _this.scrollTop(),//滚动条当前的位置
  16. subTop = opts.toT - curTop, //滚动条目标位置和当前位置的差值
  17. index = 0,
  18. dur = Math.round(opts.durTime / opts.delay),
  19. smoothScroll = function(t){
  20. index++;
  21. var per = Math.round(subTop/dur);
  22. if(index >= dur){
  23. _this.scrollTop(t);
  24. window.clearInterval(timer);
  25. if(opts.callback && typeof opts.callback == 'function'){
  26. opts.callback();
  27. }
  28. return;
  29. }else{
  30. _this.scrollTop(curTop + index*per);
  31. }
  32. };
  33. timer = window.setInterval(function(){
  34. smoothScroll(opts.toT);
  35. }, opts.delay);
  36. return _this;
  37. };