Blame view

src/components/carousel/carousel.vue 6.96 KB
6c9acb08   Rijn   initialize carousel
1
  <template>
41f83010   Rijn   update props and ...
2
      <div :class="classes">
e9989f2b   Rijn   added horizontal ...
3
4
5
          <button :class="arrowClasses" class="left" @click="add(-1)">
              <Icon type="chevron-left"></Icon>
          </button>
65c64ce9   Rijn   carousel basic po...
6
7
8
9
10
11
          <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>
e9989f2b   Rijn   added horizontal ...
12
13
14
15
16
17
18
19
20
21
22
23
          <button :class="arrowClasses" class="right" @click="add(1)">
              <Icon type="chevron-right"></Icon>
          </button>
          <ul :class="dotsClasses">
              <template v-for="n in slides.length">
                  <li :class="{ [`${prefixCls}-active`]: n === currentIndex }"
                      @click="dotsEvent('click', n)"
                      @mouseover="dotsEvent('hover', n)">
                      <button></button>
                  </li>
              </template>
          </ul>
6c9acb08   Rijn   initialize carousel
24
25
26
      </div>
  </template>
  <script>
41f83010   Rijn   update props and ...
27
      import Icon from '../icon/icon.vue';
65c64ce9   Rijn   carousel basic po...
28
      import { oneOf, getStyle, deepCopy, getScrollBarSize } from '../../utils/assist';
41f83010   Rijn   update props and ...
29
30
  
      const prefixCls = 'ivu-carousel';
6c9acb08   Rijn   initialize carousel
31
32
  
      export default {
41f83010   Rijn   update props and ...
33
34
          name: 'Carousel',
          props: {
e9989f2b   Rijn   added horizontal ...
35
36
37
              arrow: {
                  type: String,
                  default: 'hover'
41f83010   Rijn   update props and ...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
              },
              autoplay: {
                  type: Boolean,
                  default: true
              },
              autoplaySpeed: {
                  type: Number,
                  default: 2000
              },
              easing: {
                  type: String,
                  default: 'ease'
              },
              dots: {
e9989f2b   Rijn   added horizontal ...
52
53
54
55
56
57
                  type: String,
                  default: 'inside'
              },
              trigger: {
                  type: String,
                  default: 'click'
41f83010   Rijn   update props and ...
58
              },
41f83010   Rijn   update props and ...
59
60
61
              vertical: {
                  type: Boolean,
                  default: false
3e6d6356   Rijn   implement basic t...
62
63
64
65
              },
              currentIndex: {
                  type: Number,
                  default: 0
41f83010   Rijn   update props and ...
66
67
              }
          },
65c64ce9   Rijn   carousel basic po...
68
69
70
71
72
          data () {
              return {
                  prefixCls: prefixCls,
                  listWidth: 0,
                  trackWidth: 0,
3e6d6356   Rijn   implement basic t...
73
                  trackLeft: 0,
65c64ce9   Rijn   carousel basic po...
74
                  slides: [],
3e6d6356   Rijn   implement basic t...
75
76
                  slideInstances: [],
                  timer: null
65c64ce9   Rijn   carousel basic po...
77
78
              }
          },
41f83010   Rijn   update props and ...
79
80
81
82
83
84
85
86
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-vertical`]: this.vertical
                      }
                  ];
65c64ce9   Rijn   carousel basic po...
87
88
89
              },
              trackStyles () {
                  return {
3e6d6356   Rijn   implement basic t...
90
91
92
                      width: `${this.trackWidth}px`,
                      transform: `translate3d(-${this.trackLeft}px, 0px, 0px)`,
                      transition: `transform 500ms ${this.easing}`
65c64ce9   Rijn   carousel basic po...
93
                  };
e9989f2b   Rijn   added horizontal ...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
              },
              arrowClasses () {
                  return [
                      `${prefixCls}-arrow`,
                      `${prefixCls}-arrow-${this.arrow}`
                  ]
              },
              dotsClasses () {
                  return [
                      `${prefixCls}-dots`,
                      `${prefixCls}-arrow-${this.dots}`
                  ]
              },
              activeDot (n) {
                  return {
                      [`${prefixCls}-vertical`]: this.currentIndex === n
                  }
65c64ce9   Rijn   carousel basic po...
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
145
146
147
148
149
150
151
152
153
              }
          },
          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;
8f4e2cf0   Rijn   update pos when s...
154
155
  
                  this.updatePos();
65c64ce9   Rijn   carousel basic po...
156
157
158
159
160
161
162
163
164
165
              },
              updatePos () {
                  this.findChild((child) => {
                      child.width = this.listWidth;
                  });
  
                  this.trackWidth = (this.slides.length || 0) * this.listWidth;
              },
              // use when slot changed
              slotChange () {
373dfb3c   Rijn   removed observer
166
167
168
169
170
171
172
                  this.$nextTick(() => {
                      this.slides = [];
                      this.slideInstances = [];
  
                      this.updateSlides(true, true);
                      this.updatePos();
                  });
65c64ce9   Rijn   carousel basic po...
173
174
175
176
177
178
              },
              handleResize () {
                  this.$nextTick(() => {
                      this.listWidth = parseInt(getStyle(this.$el, 'width'));
                      this.updatePos();
                  });
3e6d6356   Rijn   implement basic t...
179
              },
77d460e8   Rijn   added offset func...
180
181
182
              add (offset) {
                  let index = this.currentIndex;
                  index += offset;
5e8be9b3   Rijn   fixed add bug. ad...
183
184
                  while (index < 0) index += this.slides.length;
                  index = index % this.slides.length;
e9989f2b   Rijn   added horizontal ...
185
                  this.$emit('on-change', this.currentIndex, index);
77d460e8   Rijn   added offset func...
186
187
                  this.currentIndex = index;
              },
e9989f2b   Rijn   added horizontal ...
188
189
190
191
192
              dotsEvent (event, n) {
                  if (event === this.trigger) {
                      this.$emit('on-change', this.currentIndex, n);
                      this.currentIndex = n;
                  }
bfc11079   Rijn   updated autoplay ...
193
              },
3e6d6356   Rijn   implement basic t...
194
              setAutoplay () {
bfc11079   Rijn   updated autoplay ...
195
                  window.clearInterval(this.timer);
3e6d6356   Rijn   implement basic t...
196
197
                  if (this.autoplay) {
                      this.timer = window.setInterval(() => {
77d460e8   Rijn   added offset func...
198
                          this.add(1);
3e6d6356   Rijn   implement basic t...
199
                      }, this.autoplaySpeed);
3e6d6356   Rijn   implement basic t...
200
                  }
41f83010   Rijn   update props and ...
201
              }
65c64ce9   Rijn   carousel basic po...
202
203
204
          },
          compiled () {
              this.updateSlides(true);
65c64ce9   Rijn   carousel basic po...
205
          },
3e6d6356   Rijn   implement basic t...
206
207
208
209
          watch: {
              autoplay () {
                  this.setAutoplay();
              },
bfc11079   Rijn   updated autoplay ...
210
211
212
213
214
              autoplaySpeed () {
                  this.setAutoplay();
              },
              currentIndex () {
                  this.$nextTick(() => {
e9989f2b   Rijn   added horizontal ...
215
                      this.trackLeft = this.currentIndex * this.listWidth;
bfc11079   Rijn   updated autoplay ...
216
                  });
3e6d6356   Rijn   implement basic t...
217
218
              }
          },
65c64ce9   Rijn   carousel basic po...
219
220
          ready () {
              this.handleResize();
bfc11079   Rijn   updated autoplay ...
221
              this.setAutoplay();
65c64ce9   Rijn   carousel basic po...
222
223
224
225
              window.addEventListener('resize', this.handleResize, false);
          },
          beforeDestroy () {
              window.removeEventListener('resize', this.handleResize, false);
41f83010   Rijn   update props and ...
226
          }
6c9acb08   Rijn   initialize carousel
227
228
      };
  </script>