Blame view

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