horizontal-scroll.vue 6.5 KB

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