Blame view

src/components/carousel/carousel.vue 11.3 KB
6c9acb08   Rijn   initialize carousel
1
  <template>
41f83010   Rijn   update props and ...
2
      <div :class="classes">
932db623   Rijn   remove autoplay d...
3
          <button :class="arrowClasses" class="left" @click="arrowEvent(-1)">
e9989f2b   Rijn   added horizontal ...
4
5
              <Icon type="chevron-left"></Icon>
          </button>
65c64ce9   Rijn   carousel basic po...
6
          <div :class="[prefixCls + '-list']">
ccf544dc   houyl   Merge branch 'mas...
7
              <div :class="[prefixCls + '-track', showCopyTrack ? '' : 'higher']" :style="trackStyles" ref="originTrack">
65c64ce9   Rijn   carousel basic po...
8
9
                  <slot></slot>
              </div>
ccf544dc   houyl   Merge branch 'mas...
10
11
              <div :class="[prefixCls + '-track', showCopyTrack ? 'higher' : '']" :style="copyTrackStyles" ref="copyTrack" v-if="loop">
              </div>
65c64ce9   Rijn   carousel basic po...
12
          </div>
932db623   Rijn   remove autoplay d...
13
          <button :class="arrowClasses" class="right" @click="arrowEvent(1)">
e9989f2b   Rijn   added horizontal ...
14
15
16
17
              <Icon type="chevron-right"></Icon>
          </button>
          <ul :class="dotsClasses">
              <template v-for="n in slides.length">
bb71140e   梁灏   support Carousel
18
19
20
                  <li :class="[n - 1 === currentIndex ? prefixCls + '-active' : '']"
                      @click="dotsEvent('click', n - 1)"
                      @mouseover="dotsEvent('hover', n - 1)">
ccf544dc   houyl   Merge branch 'mas...
21
                      <button :class="[radiusDot ? 'radius' : '']"></button>
e9989f2b   Rijn   added horizontal ...
22
23
24
                  </li>
              </template>
          </ul>
6c9acb08   Rijn   initialize carousel
25
26
27
      </div>
  </template>
  <script>
41f83010   Rijn   update props and ...
28
      import Icon from '../icon/icon.vue';
53e38f9c   Rijn   added prop valida...
29
      import { getStyle, oneOf } from '../../utils/assist';
1dad8a57   梁灏   update Carousel
30
      import { on, off } from '../../utils/dom';
41f83010   Rijn   update props and ...
31
32
  
      const prefixCls = 'ivu-carousel';
6c9acb08   Rijn   initialize carousel
33
34
  
      export default {
41f83010   Rijn   update props and ...
35
          name: 'Carousel',
50f4ac70   Rijn   Merge branch 'mas...
36
          components: { Icon },
41f83010   Rijn   update props and ...
37
          props: {
e9989f2b   Rijn   added horizontal ...
38
39
              arrow: {
                  type: String,
53e38f9c   Rijn   added prop valida...
40
41
42
43
                  default: 'hover',
                  validator (value) {
                      return oneOf(value, ['hover', 'always', 'never']);
                  }
41f83010   Rijn   update props and ...
44
45
46
              },
              autoplay: {
                  type: Boolean,
5139233b   梁灏   update Carousel
47
                  default: false
41f83010   Rijn   update props and ...
48
49
50
51
52
              },
              autoplaySpeed: {
                  type: Number,
                  default: 2000
              },
ccf544dc   houyl   Merge branch 'mas...
53
54
55
56
              loop: {
                  type: Boolean,
                  default: false
              },
41f83010   Rijn   update props and ...
57
58
59
60
61
              easing: {
                  type: String,
                  default: 'ease'
              },
              dots: {
e9989f2b   Rijn   added horizontal ...
62
                  type: String,
53e38f9c   Rijn   added prop valida...
63
64
65
66
                  default: 'inside',
                  validator (value) {
                      return oneOf(value, ['inside', 'outside', 'none']);
                  }
e9989f2b   Rijn   added horizontal ...
67
              },
ccf544dc   houyl   Merge branch 'mas...
68
69
70
71
              radiusDot: {
                  type: Boolean,
                  default: false
              },
e9989f2b   Rijn   added horizontal ...
72
73
              trigger: {
                  type: String,
53e38f9c   Rijn   added prop valida...
74
75
76
77
                  default: 'click',
                  validator (value) {
                      return oneOf(value, ['click', 'hover']);
                  }
41f83010   Rijn   update props and ...
78
              },
bb71140e   梁灏   support Carousel
79
              value: {
3e6d6356   Rijn   implement basic t...
80
81
                  type: Number,
                  default: 0
9cd69375   Rijn   added height props
82
83
84
              },
              height: {
                  type: [String, Number],
53e38f9c   Rijn   added prop valida...
85
86
                  default: 'auto',
                  validator (value) {
0b1b650d   Rijn   fix #269.
87
                      return value === 'auto' || Object.prototype.toString.call(value) === '[object Number]';
53e38f9c   Rijn   added prop valida...
88
                  }
41f83010   Rijn   update props and ...
89
90
              }
          },
65c64ce9   Rijn   carousel basic po...
91
92
93
94
95
          data () {
              return {
                  prefixCls: prefixCls,
                  listWidth: 0,
                  trackWidth: 0,
9cd69375   Rijn   added height props
96
                  trackOffset: 0,
ccf544dc   houyl   Merge branch 'mas...
97
98
                  trackCopyOffset: 0,
                  showCopyTrack: false,
65c64ce9   Rijn   carousel basic po...
99
                  slides: [],
3e6d6356   Rijn   implement basic t...
100
                  slideInstances: [],
9cd69375   Rijn   added height props
101
                  timer: null,
bb71140e   梁灏   support Carousel
102
                  ready: false,
ccf544dc   houyl   Merge branch 'mas...
103
104
105
106
                  currentIndex: this.value,
                  trackIndex: this.value,
                  copyTrackIndex: this.value,
                  hideTrackPos: -1, // 默认左滑
50f4ac70   Rijn   Merge branch 'mas...
107
              };
65c64ce9   Rijn   carousel basic po...
108
          },
41f83010   Rijn   update props and ...
109
110
111
          computed: {
              classes () {
                  return [
9cd69375   Rijn   added height props
112
                      `${prefixCls}`
41f83010   Rijn   update props and ...
113
                  ];
65c64ce9   Rijn   carousel basic po...
114
              },
ccf544dc   houyl   Merge branch 'mas...
115
116
117
118
119
              listStyle () {
                  return {
                      width: `${this.trackWidth * 2}px`,
                  };
              },
65c64ce9   Rijn   carousel basic po...
120
121
              trackStyles () {
                  return {
3e6d6356   Rijn   implement basic t...
122
                      width: `${this.trackWidth}px`,
ccf544dc   houyl   Merge branch 'mas...
123
                      transform: `translate3d(${-this.trackOffset}px, 0px, 0px)`,
3e6d6356   Rijn   implement basic t...
124
                      transition: `transform 500ms ${this.easing}`
65c64ce9   Rijn   carousel basic po...
125
                  };
e9989f2b   Rijn   added horizontal ...
126
              },
ccf544dc   houyl   Merge branch 'mas...
127
128
129
130
131
132
133
134
135
              copyTrackStyles () {
                  return {
                      width: `${this.trackWidth}px`,
                      transform: `translate3d(${-this.trackCopyOffset}px, 0px, 0px)`,
                      transition: `transform 500ms ${this.easing}`,
                      position: 'absolute',
                      top: 0
                  };
              },
e9989f2b   Rijn   added horizontal ...
136
137
138
139
              arrowClasses () {
                  return [
                      `${prefixCls}-arrow`,
                      `${prefixCls}-arrow-${this.arrow}`
50f4ac70   Rijn   Merge branch 'mas...
140
                  ];
e9989f2b   Rijn   added horizontal ...
141
142
143
144
              },
              dotsClasses () {
                  return [
                      `${prefixCls}-dots`,
8738f4d3   Rijn   added outside dots
145
                      `${prefixCls}-dots-${this.dots}`
50f4ac70   Rijn   Merge branch 'mas...
146
                  ];
65c64ce9   Rijn   carousel basic po...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
              }
          },
          methods: {
              // find option component
              findChild (cb) {
                  const find = function (child) {
                      const name = child.$options.componentName;
  
                      if (name) {
                          cb(child);
                      } else if (child.$children.length) {
                          child.$children.forEach((innerChild) => {
                              find(innerChild, cb);
                          });
                      }
                  };
  
9cd69375   Rijn   added height props
164
                  if (this.slideInstances.length || !this.$children) {
65c64ce9   Rijn   carousel basic po...
165
166
167
168
169
170
171
172
173
                      this.slideInstances.forEach((child) => {
                          find(child);
                      });
                  } else {
                      this.$children.forEach((child) => {
                          find(child);
                      });
                  }
              },
ccf544dc   houyl   Merge branch 'mas...
174
175
176
177
178
179
              // copy trackDom
              initCopyTrackDom () {
                  this.$nextTick(() => {
                      this.$refs.copyTrack.innerHTML = this.$refs.originTrack.innerHTML;
                  });
              },
bb71140e   梁灏   support Carousel
180
              updateSlides (init) {
65c64ce9   Rijn   carousel basic po...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
                  let slides = [];
                  let index = 1;
  
                  this.findChild((child) => {
                      slides.push({
                          $el: child.$el
                      });
                      child.index = index++;
  
                      if (init) {
                          this.slideInstances.push(child);
                      }
                  });
  
                  this.slides = slides;
8f4e2cf0   Rijn   update pos when s...
196
                  this.updatePos();
65c64ce9   Rijn   carousel basic po...
197
198
199
200
              },
              updatePos () {
                  this.findChild((child) => {
                      child.width = this.listWidth;
9cd69375   Rijn   added height props
201
                      child.height = typeof this.height === 'number' ? `${this.height}px` : this.height;
65c64ce9   Rijn   carousel basic po...
202
203
204
205
206
207
                  });
  
                  this.trackWidth = (this.slides.length || 0) * this.listWidth;
              },
              // use when slot changed
              slotChange () {
373dfb3c   Rijn   removed observer
208
209
210
211
212
213
                  this.$nextTick(() => {
                      this.slides = [];
                      this.slideInstances = [];
  
                      this.updateSlides(true, true);
                      this.updatePos();
62808b2b   Rijn   reset autoplay ti...
214
                      this.updateOffset();
373dfb3c   Rijn   removed observer
215
                  });
65c64ce9   Rijn   carousel basic po...
216
217
              },
              handleResize () {
9cd69375   Rijn   added height props
218
219
                  this.listWidth = parseInt(getStyle(this.$el, 'width'));
                  this.updatePos();
62808b2b   Rijn   reset autoplay ti...
220
                  this.updateOffset();
3e6d6356   Rijn   implement basic t...
221
              },
ccf544dc   houyl   Merge branch 'mas...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
              updateTrackPos (index) {
                  if (this.showCopyTrack) {
                      this.trackIndex = index;
                  } else {
                      this.copyTrackIndex = index;
                  }
              },
              updateTrackIndex (index) {
                  if (this.showCopyTrack) {
                      this.copyTrackIndex = index;
                  } else {
                      this.trackIndex = index;
                  }
              },
77d460e8   Rijn   added offset func...
236
              add (offset) {
ccf544dc   houyl   Merge branch 'mas...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
                  // 获取单个轨道的图片数
                  let slidesLen = this.slides.length;
                  // 如果是无缝滚动,需要初始化双轨道位置
                  if (this.loop) {
                      if (offset > 0) {
                          // 初始化左滑轨道位置
                          this.hideTrackPos = -1;
                      } else {
                          // 初始化右滑轨道位置
                          this.hideTrackPos = slidesLen;
                      }
                      this.updateTrackPos(this.hideTrackPos);
                  }
                  // 获取当前展示图片的索引值
                  let index =  this.showCopyTrack ? this.copyTrackIndex : this.trackIndex;
77d460e8   Rijn   added offset func...
252
                  index += offset;
ccf544dc   houyl   Merge branch 'mas...
253
254
255
256
257
258
259
260
261
262
263
                  while (index < 0) index += slidesLen;
                  if (((offset > 0 && index === slidesLen) || offset < 0 && index === slidesLen - 1) && this.loop) {
                      // 极限值(左滑:当前索引为总图片张数, 右滑:当前索引为总图片张数 - 1)切换轨道
                      this.showCopyTrack = !this.showCopyTrack;
                      this.trackIndex += offset;
                      this.copyTrackIndex += offset;
                  } else {
                      if (!this.loop) index = index % this.slides.length;
                      this.updateTrackIndex(index);
                  }
                  this.$emit('input', index === this.slides.length ? 0 : index);
77d460e8   Rijn   added offset func...
264
              },
932db623   Rijn   remove autoplay d...
265
266
267
268
              arrowEvent (offset) {
                  this.setAutoplay();
                  this.add(offset);
              },
e9989f2b   Rijn   added horizontal ...
269
              dotsEvent (event, n) {
ccf544dc   houyl   Merge branch 'mas...
270
271
272
                  let curIndex = this.showCopyTrack ? this.copyTrackIndex : this.trackIndex;
                  if (event === this.trigger && curIndex !== n) {
                      this.updateTrackIndex(n);
bb71140e   梁灏   support Carousel
273
                      this.$emit('input', n);
62808b2b   Rijn   reset autoplay ti...
274
275
                      // Reset autoplay timer when trigger be activated
                      this.setAutoplay();
e9989f2b   Rijn   added horizontal ...
276
                  }
bfc11079   Rijn   updated autoplay ...
277
              },
3e6d6356   Rijn   implement basic t...
278
              setAutoplay () {
bfc11079   Rijn   updated autoplay ...
279
                  window.clearInterval(this.timer);
3e6d6356   Rijn   implement basic t...
280
281
                  if (this.autoplay) {
                      this.timer = window.setInterval(() => {
932db623   Rijn   remove autoplay d...
282
                          this.add(1);
3e6d6356   Rijn   implement basic t...
283
                      }, this.autoplaySpeed);
3e6d6356   Rijn   implement basic t...
284
                  }
9cd69375   Rijn   added height props
285
286
287
              },
              updateOffset () {
                  this.$nextTick(() => {
ccf544dc   houyl   Merge branch 'mas...
288
289
290
291
                      /* hack: revise copyTrack offset (1px) */
                      let ofs = this.copyTrackIndex > 0 ? -1 : 1;
                      this.trackOffset = this.trackIndex * this.listWidth;
                      this.trackCopyOffset = this.copyTrackIndex * this.listWidth + ofs;
9cd69375   Rijn   added height props
292
                  });
41f83010   Rijn   update props and ...
293
              }
65c64ce9   Rijn   carousel basic po...
294
          },
3e6d6356   Rijn   implement basic t...
295
296
297
298
          watch: {
              autoplay () {
                  this.setAutoplay();
              },
bfc11079   Rijn   updated autoplay ...
299
300
301
              autoplaySpeed () {
                  this.setAutoplay();
              },
c1af3fac   Rijn   added on-change e...
302
              currentIndex (val, oldVal) {
50f4ac70   Rijn   Merge branch 'mas...
303
                  this.$emit('on-change', oldVal, val);
ccf544dc   houyl   Merge branch 'mas...
304
305
306
307
308
              },
              trackIndex () {
                  this.updateOffset();
              },
              copyTrackIndex () {
9cd69375   Rijn   added height props
309
310
311
312
                  this.updateOffset();
              },
              height () {
                  this.updatePos();
bb71140e   梁灏   support Carousel
313
314
315
              },
              value (val) {
                  this.currentIndex = val;
3e6d6356   Rijn   implement basic t...
316
317
              }
          },
bb71140e   梁灏   support Carousel
318
319
          mounted () {
              this.updateSlides(true);
65c64ce9   Rijn   carousel basic po...
320
              this.handleResize();
bfc11079   Rijn   updated autoplay ...
321
              this.setAutoplay();
1dad8a57   梁灏   update Carousel
322
323
  //            window.addEventListener('resize', this.handleResize, false);
              on(window, 'resize', this.handleResize);
65c64ce9   Rijn   carousel basic po...
324
325
          },
          beforeDestroy () {
1dad8a57   梁灏   update Carousel
326
327
  //            window.removeEventListener('resize', this.handleResize, false);
              off(window, 'resize', this.handleResize);
41f83010   Rijn   update props and ...
328
          }
6c9acb08   Rijn   initialize carousel
329
330
      };
  </script>