index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import { use } from '../utils';
  2. import { on, off, preventDefault } from '../utils/event';
  3. import { TouchMixin } from '../mixins/touch';
  4. const [sfc, bem] = use('swipe');
  5. export default sfc({
  6. mixins: [TouchMixin],
  7. props: {
  8. width: Number,
  9. height: Number,
  10. autoplay: Number,
  11. vertical: Boolean,
  12. initialSwipe: Number,
  13. indicatorColor: String,
  14. loop: {
  15. type: Boolean,
  16. default: true
  17. },
  18. touchable: {
  19. type: Boolean,
  20. default: true
  21. },
  22. showIndicators: {
  23. type: Boolean,
  24. default: true
  25. },
  26. duration: {
  27. type: Number,
  28. default: 500
  29. }
  30. },
  31. data() {
  32. return {
  33. computedWidth: 0,
  34. computedHeight: 0,
  35. offset: 0,
  36. active: 0,
  37. deltaX: 0,
  38. deltaY: 0,
  39. swipes: [],
  40. swiping: false
  41. };
  42. },
  43. mounted() {
  44. this.initialize();
  45. if (!this.$isServer) {
  46. on(window, 'resize', this.onResize, true);
  47. }
  48. },
  49. activated() {
  50. if (this.rendered) {
  51. this.initialize();
  52. }
  53. this.rendered = true;
  54. },
  55. destroyed() {
  56. this.clear();
  57. if (!this.$isServer) {
  58. off(window, 'resize', this.onResize, true);
  59. }
  60. },
  61. watch: {
  62. swipes() {
  63. this.initialize();
  64. },
  65. initialSwipe() {
  66. this.initialize();
  67. },
  68. autoplay(autoplay) {
  69. if (!autoplay) {
  70. this.clear();
  71. } else {
  72. this.autoPlay();
  73. }
  74. }
  75. },
  76. computed: {
  77. count() {
  78. return this.swipes.length;
  79. },
  80. delta() {
  81. return this.vertical ? this.deltaY : this.deltaX;
  82. },
  83. size() {
  84. return this[this.vertical ? 'computedHeight' : 'computedWidth'];
  85. },
  86. trackSize() {
  87. return this.count * this.size;
  88. },
  89. activeIndicator() {
  90. return (this.active + this.count) % this.count;
  91. },
  92. isCorrectDirection() {
  93. const expect = this.vertical ? 'vertical' : 'horizontal';
  94. return this.direction === expect;
  95. },
  96. trackStyle() {
  97. const mainAxis = this.vertical ? 'height' : 'width';
  98. const crossAxis = this.vertical ? 'width' : 'height';
  99. return {
  100. [mainAxis]: `${this.trackSize}px`,
  101. [crossAxis]: this[crossAxis] ? `${this[crossAxis]}px` : '',
  102. transitionDuration: `${this.swiping ? 0 : this.duration}ms`,
  103. transform: `translate${this.vertical ? 'Y' : 'X'}(${this.offset}px)`
  104. };
  105. },
  106. indicatorStyle() {
  107. return {
  108. backgroundColor: this.indicatorColor
  109. };
  110. }
  111. },
  112. methods: {
  113. // initialize swipe position
  114. initialize(active = this.initialSwipe) {
  115. clearTimeout(this.timer);
  116. if (this.$el) {
  117. const rect = this.$el.getBoundingClientRect();
  118. this.computedWidth = this.width || rect.width;
  119. this.computedHeight = this.height || rect.height;
  120. }
  121. this.swiping = true;
  122. this.active = active;
  123. this.offset = this.count > 1 ? -this.size * this.active : 0;
  124. this.swipes.forEach(swipe => {
  125. swipe.offset = 0;
  126. });
  127. this.autoPlay();
  128. },
  129. onResize() {
  130. this.initialize(this.activeIndicator);
  131. },
  132. onTouchStart(event) {
  133. if (!this.touchable) return;
  134. this.clear();
  135. this.swiping = true;
  136. this.touchStart(event);
  137. this.correctPosition();
  138. },
  139. onTouchMove(event) {
  140. if (!this.touchable || !this.swiping) return;
  141. this.touchMove(event);
  142. if (this.isCorrectDirection) {
  143. preventDefault(event, true);
  144. this.move({ offset: Math.min(Math.max(this.delta, -this.size), this.size) });
  145. }
  146. },
  147. onTouchEnd() {
  148. if (!this.touchable || !this.swiping) return;
  149. if (this.delta && this.isCorrectDirection) {
  150. const offset = this.vertical ? this.offsetY : this.offsetX;
  151. this.move({
  152. pace: offset > 0 ? (this.delta > 0 ? -1 : 1) : 0,
  153. emitChange: true
  154. });
  155. }
  156. this.swiping = false;
  157. this.autoPlay();
  158. },
  159. move({ pace = 0, offset = 0, emitChange }) {
  160. const { delta, active, count, swipes, trackSize } = this;
  161. const atFirst = active === 0;
  162. const atLast = active === count - 1;
  163. const outOfBounds =
  164. !this.loop &&
  165. ((atFirst && (offset > 0 || pace < 0)) || (atLast && (offset < 0 || pace > 0)));
  166. if (outOfBounds || count <= 1) {
  167. return;
  168. }
  169. if (swipes[0]) {
  170. swipes[0].offset = atLast && (delta < 0 || pace > 0) ? trackSize : 0;
  171. }
  172. if (swipes[count - 1]) {
  173. swipes[count - 1].offset = atFirst && (delta > 0 || pace < 0) ? -trackSize : 0;
  174. }
  175. if (pace && active + pace >= -1 && active + pace <= count) {
  176. this.active += pace;
  177. if (emitChange) {
  178. this.$emit('change', this.activeIndicator);
  179. }
  180. }
  181. this.offset = Math.round(offset - this.active * this.size);
  182. },
  183. swipeTo(index) {
  184. this.swiping = true;
  185. this.resetTouchStatus();
  186. this.correctPosition();
  187. setTimeout(() => {
  188. this.swiping = false;
  189. this.move({
  190. pace: (index % this.count) - this.active,
  191. emitChange: true
  192. });
  193. }, 30);
  194. },
  195. correctPosition() {
  196. if (this.active <= -1) {
  197. this.move({ pace: this.count });
  198. }
  199. if (this.active >= this.count) {
  200. this.move({ pace: -this.count });
  201. }
  202. },
  203. clear() {
  204. clearTimeout(this.timer);
  205. },
  206. autoPlay() {
  207. const { autoplay } = this;
  208. if (autoplay && this.count > 1) {
  209. this.clear();
  210. this.timer = setTimeout(() => {
  211. this.swiping = true;
  212. this.resetTouchStatus();
  213. this.correctPosition();
  214. setTimeout(() => {
  215. this.swiping = false;
  216. this.move({
  217. pace: 1,
  218. emitChange: true
  219. });
  220. this.autoPlay();
  221. }, 30);
  222. }, autoplay);
  223. }
  224. }
  225. },
  226. render(h) {
  227. const { count, activeIndicator } = this;
  228. const Indicator =
  229. this.slots('indicator') ||
  230. (this.showIndicators && count > 1 && (
  231. <div class={bem('indicators', { vertical: this.vertical })}>
  232. {Array(...Array(count)).map((empty, index) => (
  233. <i
  234. class={bem('indicator', { active: index === activeIndicator })}
  235. style={index === activeIndicator ? this.indicatorStyle : null}
  236. />
  237. ))}
  238. </div>
  239. ));
  240. return (
  241. <div class={bem()}>
  242. <div
  243. ref="track"
  244. style={this.trackStyle}
  245. class={bem('track')}
  246. onTouchstart={this.onTouchStart}
  247. onTouchmove={this.onTouchMove}
  248. onTouchend={this.onTouchEnd}
  249. onTouchcancel={this.onTouchEnd}
  250. >
  251. {this.slots()}
  252. </div>
  253. {Indicator}
  254. </div>
  255. );
  256. }
  257. });