Blame view

src/components/carousel/carousel.vue 5.91 KB
6c9acb08   Rijn   initialize carousel
1
  <template>
41f83010   Rijn   update props and ...
2
      <div :class="classes">
65c64ce9   Rijn   carousel basic po...
3
4
5
6
7
8
9
10
          <!-- button -->
          <div :class="[prefixCls + '-list']">
              <div :class="[prefixCls + '-track']" :style="trackStyles" v-el:slides>
                  <!-- opacity: 1; width: 4480px; transform: translate3d(-1120px, 0px, 0px); -->
                  <slot></slot>
              </div>
          </div>
          <!-- button -->
6c9acb08   Rijn   initialize carousel
11
12
13
      </div>
  </template>
  <script>
41f83010   Rijn   update props and ...
14
      import Icon from '../icon/icon.vue';
65c64ce9   Rijn   carousel basic po...
15
      import { oneOf, getStyle, deepCopy, getScrollBarSize } from '../../utils/assist';
41f83010   Rijn   update props and ...
16
17
  
      const prefixCls = 'ivu-carousel';
6c9acb08   Rijn   initialize carousel
18
19
  
      export default {
41f83010   Rijn   update props and ...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
          name: 'Carousel',
          props: {
              arrows: {
                  type: Boolean,
                  default: false
              },
              autoplay: {
                  type: Boolean,
                  default: true
              },
              autoplaySpeed: {
                  type: Number,
                  default: 2000
              },
              easing: {
                  type: String,
                  default: 'ease'
              },
              dots: {
                  type: Boolean,
                  default: true
              },
              fade: {
                  type: Boolean,
                  default: false
              },
              vertical: {
                  type: Boolean,
                  default: false
3e6d6356   Rijn   implement basic t...
49
50
51
52
              },
              currentIndex: {
                  type: Number,
                  default: 0
41f83010   Rijn   update props and ...
53
54
              }
          },
65c64ce9   Rijn   carousel basic po...
55
56
57
58
59
          data () {
              return {
                  prefixCls: prefixCls,
                  listWidth: 0,
                  trackWidth: 0,
3e6d6356   Rijn   implement basic t...
60
                  trackLeft: 0,
65c64ce9   Rijn   carousel basic po...
61
                  slides: [],
3e6d6356   Rijn   implement basic t...
62
63
                  slideInstances: [],
                  timer: null
65c64ce9   Rijn   carousel basic po...
64
65
              }
          },
41f83010   Rijn   update props and ...
66
67
68
69
70
71
72
73
74
          // events: before-change(from, to), after-change(current, from)
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-vertical`]: this.vertical
                      }
                  ];
65c64ce9   Rijn   carousel basic po...
75
76
77
              },
              trackStyles () {
                  return {
3e6d6356   Rijn   implement basic t...
78
79
80
                      width: `${this.trackWidth}px`,
                      transform: `translate3d(-${this.trackLeft}px, 0px, 0px)`,
                      transition: `transform 500ms ${this.easing}`
65c64ce9   Rijn   carousel basic po...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                  };
              }
          },
          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);
                          });
                      }
                  };
  
                  if (this.slideInstances.length) {
                      this.slideInstances.forEach((child) => {
                          find(child);
                      });
                  } else {
                      this.$children.forEach((child) => {
                          find(child);
                      });
                  }
              },
              updateSlides (init, slot = false) {
                  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;
  
                  // this.updateSlideWidth();
              },
              updatePos () {
                  this.findChild((child) => {
                      child.width = this.listWidth;
                  });
  
                  this.trackWidth = (this.slides.length || 0) * this.listWidth;
              },
              // use when slot changed
              slotChange () {
                  this.slides = [];
                  this.slideInstances = [];
              },
              handleResize () {
                  this.$nextTick(() => {
                      this.listWidth = parseInt(getStyle(this.$el, 'width'));
                      this.updatePos();
                  });
3e6d6356   Rijn   implement basic t...
145
              },
bfc11079   Rijn   updated autoplay ...
146
147
148
              slide () {
                  this.trackLeft = this.currentIndex * this.listWidth;
              },
3e6d6356   Rijn   implement basic t...
149
              setAutoplay () {
bfc11079   Rijn   updated autoplay ...
150
                  window.clearInterval(this.timer);
3e6d6356   Rijn   implement basic t...
151
152
                  if (this.autoplay) {
                      this.timer = window.setInterval(() => {
bfc11079   Rijn   updated autoplay ...
153
154
155
156
                          let index = this.currentIndex;
                          index ++;
                          if (index === this.slides.length) index = 0;
                          this.currentIndex = index;
3e6d6356   Rijn   implement basic t...
157
                      }, this.autoplaySpeed);
3e6d6356   Rijn   implement basic t...
158
                  }
41f83010   Rijn   update props and ...
159
              }
65c64ce9   Rijn   carousel basic po...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
          },
          compiled () {
              this.updateSlides(true);
  
              // watch slot changed
              if (MutationObserver) {
                  this.observer = new MutationObserver(() => {
                      this.slotChange();
                      this.updateSlides(true, true);
                  });
  
                  this.observer.observe(this.$els.slides, {
                      childList: true,
                      characterData: true,
                      subtree: true
                  });
              }
          },
3e6d6356   Rijn   implement basic t...
178
179
180
181
          watch: {
              autoplay () {
                  this.setAutoplay();
              },
bfc11079   Rijn   updated autoplay ...
182
183
184
185
186
187
188
              autoplaySpeed () {
                  this.setAutoplay();
              },
              currentIndex () {
                  this.$nextTick(() => {
                      this.slide();
                  });
3e6d6356   Rijn   implement basic t...
189
190
              }
          },
65c64ce9   Rijn   carousel basic po...
191
192
          ready () {
              this.handleResize();
bfc11079   Rijn   updated autoplay ...
193
              this.setAutoplay();
65c64ce9   Rijn   carousel basic po...
194
195
196
197
              window.addEventListener('resize', this.handleResize, false);
          },
          beforeDestroy () {
              window.removeEventListener('resize', this.handleResize, false);
41f83010   Rijn   update props and ...
198
          }
6c9acb08   Rijn   initialize carousel
199
200
      };
  </script>