horizontal-scroll.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <div class="nut-hor-scroll" ref="wrapper">
  3. <div class="nut-hor-list" ref="list">
  4. <slot name="list"></slot>
  5. <div class="nut-hor-control" v-if="$slots.more && isShowLoadMore">
  6. <slot name="more"></slot>
  7. </div>
  8. <slot name="arrow" v-if="$slots.arrow"></slot>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name:'nut-hor-scroll',
  15. props: {
  16. stretch: {
  17. type: Number,
  18. default: 40
  19. },
  20. scrollTo: {
  21. type: Number,
  22. default: 1
  23. },
  24. listWidth: {
  25. type: Number,
  26. default: 0
  27. }
  28. },
  29. watch: {
  30. 'scrollTo': function(val) {
  31. if (typeof val === 'number' && !isNaN(val) && val <= 0 ) {
  32. this.setTransform(val, null, 500);
  33. this.$emit('scrollToCbk');
  34. }
  35. }
  36. },
  37. data() {
  38. return {
  39. touchParams: {
  40. startX: 0,
  41. endX: 0,
  42. startY: 0,
  43. endY: 0,
  44. startTime: 0,
  45. endTime: 0
  46. },
  47. transformX: 0,
  48. scrollDistance: 0,
  49. timer: null,
  50. isShowLoadMore: false,
  51. isFirstShow: false
  52. }
  53. },
  54. methods: {
  55. isShow() {
  56. let wrapH = this.listWidth ? this.listWidth :
  57. (window.innerWidth ||
  58. document.documentElement.clientWidth ||
  59. document.body.clientWidth);
  60. let listH = this.$refs.list.offsetWidth;
  61. if (wrapH <= listH) {
  62. this.isShowLoadMore = true;
  63. } else {
  64. this.isShowLoadMore = false;
  65. }
  66. },
  67. setTransform(translateX = 0, type, time = 500, unit = 'px') {
  68. this.scrollDistance = translateX;
  69. translateX = translateX + unit;
  70. if (type === 'end') {
  71. this.$refs.list.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)`;
  72. } else {
  73. this.$refs.list.style.webkitTransition = '';
  74. }
  75. this.$refs.list.style.webkitTransform = `translate3d(${translateX}, 0, 0)`;
  76. },
  77. setMove(move, type, time) {
  78. let updateMove = move + this.transformX;
  79. let w = this.listWidth ? this.listWidth :
  80. (window.innerWidth ||
  81. document.documentElement.clientWidth ||
  82. document.body.clientWidth);
  83. let offsetWidth = this.$refs.list.offsetWidth;
  84. if (type === 'end') {
  85. if (updateMove > 0) {
  86. updateMove = 0;
  87. } else if (updateMove < -offsetWidth + w) {
  88. if (-offsetWidth + w <= 0) {
  89. updateMove = -offsetWidth + w;
  90. } else {
  91. updateMove = 0;
  92. }
  93. }
  94. this.setTransform(updateMove, type, time)
  95. } else {
  96. let maxMove = -offsetWidth + w;
  97. if (updateMove > 0 && updateMove > this.stretch) {
  98. updateMove = this.stretch;
  99. } else if (updateMove < maxMove - this.stretch) {
  100. if (maxMove <= 0) {
  101. updateMove = maxMove - this.stretch;
  102. } else {
  103. updateMove = updateMove < -this.stretch ? -this.stretch : updateMove;
  104. }
  105. }
  106. this.setTransform(updateMove, null, null);
  107. }
  108. },
  109. touchStart(event) {
  110. // event.preventDefault();
  111. let changedTouches = event.changedTouches[0];
  112. this.touchParams.startX = changedTouches.pageX;
  113. this.touchParams.startY = changedTouches.pageY;
  114. this.touchParams.startTime = event.timestamp || Date.now();
  115. this.transformX = this.scrollDistance;
  116. },
  117. touchEvent(changedTouches, callback) {
  118. this.touchParams.lastX = changedTouches.pageX;
  119. this.touchParams.lastY = changedTouches.pageY;
  120. let moveY = this.touchParams.lastY - this.touchParams.startY;
  121. let move = this.touchParams.lastX - this.touchParams.startX;
  122. if (!(Math.abs(move) > 20 && Math.abs(move) > Math.abs(moveY))) {
  123. return false;
  124. } else {
  125. let w = this.listWidth ? this.listWidth :
  126. (window.innerWidth ||
  127. document.documentElement.clientWidth ||
  128. document.body.clientWidth);
  129. let maxMove = -this.$refs.list.offsetWidth + w;
  130. callback && callback(move, maxMove, moveY);
  131. }
  132. },
  133. touchMove(event) {
  134. //event.preventDefault();
  135. let changedTouches = event.changedTouches[0];
  136. this.touchParams.lastTime = event.timestamp || Date.now();
  137. let moveTime = this.touchParams.lastTime - this.touchParams.startTime;
  138. this.touchEvent(changedTouches, (move, maxMove, moveY) => {
  139. event.preventDefault();
  140. if (event.cancelable) {
  141. event.preventDefault();
  142. }
  143. if (move > 0 && this.isFirstShow) {
  144. this.isFirstShow = false;
  145. }
  146. this.setMove(move);
  147. });
  148. },
  149. touchEnd(event) {
  150. event.stopPropagation();
  151. let changedTouches = event.changedTouches[0];
  152. this.touchParams.lastTime = event.timestamp || Date.now();
  153. let moveTime = this.touchParams.lastTime - this.touchParams.startTime;
  154. this.touchEvent(changedTouches, (move, maxMove) => {
  155. //if (moveTime <= 300) {
  156. if (Math.abs(move) > 100) {
  157. move = move * 1.5;
  158. }
  159. // 释放跳转之类
  160. if (move < 0 && (move + this.transformX) < maxMove - 20 && this.isFirstShow) {
  161. this.$emit('jump');
  162. }
  163. if (!this.isFirstShow && move < 0 && move + this.transformX < maxMove && this.$slots.more) {
  164. this.isFirstShow = true;
  165. //move = maxMove - this.transformX;
  166. }
  167. if (moveTime <= 300 ) {
  168. moveTime = moveTime + 500;
  169. this.setMove(move, 'end', moveTime);
  170. } else {
  171. this.setMove(move, 'end');
  172. }
  173. });
  174. }
  175. },
  176. mounted() {
  177. this.$nextTick(() => {
  178. this.isShow();
  179. // 监听
  180. this.$el.addEventListener('touchstart', this.touchStart);
  181. this.$el.addEventListener('touchmove', this.touchMove);
  182. this.$el.addEventListener('touchend', this.touchEnd);
  183. });
  184. },
  185. beforeDestroy() {
  186. // 移除监听
  187. this.$el.removeEventListener('touchstart', this.touchStart);
  188. this.$el.removeEventListener('touchmove', this.touchMove);
  189. this.$el.removeEventListener('touchend', this.touchEnd);
  190. clearTimeout(this.timer);
  191. }
  192. }
  193. </script>