jquery.dragscroll.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * jquery.dragscroll v1.0.0
  3. * author 735126858@qq.com
  4. * https://github.com/YuTingtao/dragscroll.js
  5. */
  6. ;(function(factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. define(['jquery'], factory);
  9. } else if (typeof exports !== 'undefined') {
  10. module.exports = factory(require('jquery'));
  11. } else {
  12. factory(jQuery);
  13. }
  14. }(function($){
  15. var methods = {
  16. init: function(options) {
  17. var defaults = {
  18. direction: null,
  19. onStart: function($this) {},
  20. onMove: function($this) {},
  21. onEnd: function($this) {}
  22. };
  23. var opt = $.extend({}, defaults, options);
  24. return this.each(function() {
  25. var $this = $(this);
  26. var _dir = opt.direction;
  27. var _onStart = opt.onStart;
  28. var _onMove = opt.onMove;
  29. var _onEnd = opt.onEnd;
  30. var left0, top0, x0, y0, flag = false;
  31. // 鼠标点击
  32. $this.on('mousedown', function(e) {
  33. e.preventDefault();
  34. var e = e || window.event;
  35. flag = true;
  36. x0 = e.clientX;
  37. y0 = e.clientY;
  38. left0 = $(this).parent().scrollLeft();
  39. top0 = $(this).parent().scrollTop();
  40. _onStart && _onStart.call(this, $this);
  41. });
  42. // 鼠标移动
  43. $this.on('mousemove', function(e) {
  44. e.preventDefault();
  45. var e = e || window.event;
  46. if (flag) {
  47. setTimeout(function() {
  48. var moveX = e.clientX - x0;
  49. var moveY = e.clientY - y0;
  50. if (_dir == 'scrollLeft') {
  51. $this.parent().scrollLeft(left0 - moveX);
  52. } else if (_dir == 'scrollTop') {
  53. $this.parent().scrollTop(top0 - moveY);
  54. } else {
  55. $this.parent().scrollLeft(left0 - moveX);
  56. $this.parent().scrollTop(top0 - moveY);
  57. }
  58. }, 10);
  59. _onMove && _onMove.call(this, $this);
  60. }
  61. });
  62. // 鼠标松开或离开
  63. $this.on('mouseup mouseleave', function() {
  64. if (flag) {
  65. _onEnd && _onEnd.call(this, $this);
  66. }
  67. flag = false;
  68. });
  69. // 移动端
  70. $this.on('touchstart', function(e) {
  71. var e = e.originalEvent.targetTouches[0];
  72. flag = true;
  73. x0 = e.clientX;
  74. y0 = e.clientY;
  75. left0 = $(this).parent().scrollLeft();
  76. top0 = $(this).parent().scrollTop();
  77. _onStart && _onStart.call(this, $this);
  78. });
  79. $this.on('touchmove', function(e) {
  80. e.stopPropagation();
  81. var e = e.originalEvent.targetTouches[0];
  82. if (flag) {
  83. setTimeout(function() {
  84. var moveX = e.clientX - x0;
  85. var moveY = e.clientY - y0;
  86. if (_dir == 'scrollLeft') {
  87. $this.parent().scrollLeft(left0 - moveX);
  88. } else if (_dir == 'scrollTop') {
  89. $this.parent().scrollTop(top0 - moveY);
  90. } else {
  91. $this.parent().scrollLeft(left0 - moveX);
  92. $this.parent().scrollTop(top0 - moveY);
  93. }
  94. }, 10);
  95. _onMove && _onMove.call(this, $this);
  96. }
  97. });
  98. $this.on('touchend', function(e) {
  99. if (flag) {
  100. _onEnd && _onEnd.call(this, $this);
  101. }
  102. flag = false;
  103. });
  104. });
  105. },
  106. destroy: function() {
  107. return $(this).each(function() {
  108. var $this = $(this);
  109. $this.off('mousedown mousemove mouseup mouseleave');
  110. $this.off('touchstart touchmove touchend')
  111. });
  112. }
  113. };
  114. $.fn.dragscroll = function(method) {
  115. if (methods[method]) {
  116. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  117. } else if (typeof method === 'object' || !method) {
  118. return methods.init.apply(this, arguments);
  119. } else {
  120. $.error('method ' + method + ' does not exist on jquery.dragscroll.js');
  121. }
  122. }
  123. }));