Commit 3e6d63565044ea80edb47fe1f4312e331cf199e9
1 parent
65c64ce9
implement basic timer
Showing
2 changed files
with
32 additions
and
2 deletions
Show diff stats
src/components/carousel/carousel.vue
... | ... | @@ -46,6 +46,10 @@ |
46 | 46 | vertical: { |
47 | 47 | type: Boolean, |
48 | 48 | default: false |
49 | + }, | |
50 | + currentIndex: { | |
51 | + type: Number, | |
52 | + default: 0 | |
49 | 53 | } |
50 | 54 | }, |
51 | 55 | data () { |
... | ... | @@ -53,8 +57,10 @@ |
53 | 57 | prefixCls: prefixCls, |
54 | 58 | listWidth: 0, |
55 | 59 | trackWidth: 0, |
60 | + trackLeft: 0, | |
56 | 61 | slides: [], |
57 | - slideInstances: [] | |
62 | + slideInstances: [], | |
63 | + timer: null | |
58 | 64 | } |
59 | 65 | }, |
60 | 66 | // events: before-change(from, to), after-change(current, from) |
... | ... | @@ -69,7 +75,9 @@ |
69 | 75 | }, |
70 | 76 | trackStyles () { |
71 | 77 | return { |
72 | - width: `${this.trackWidth}px` | |
78 | + width: `${this.trackWidth}px`, | |
79 | + transform: `translate3d(-${this.trackLeft}px, 0px, 0px)`, | |
80 | + transition: `transform 500ms ${this.easing}` | |
73 | 81 | }; |
74 | 82 | } |
75 | 83 | }, |
... | ... | @@ -134,6 +142,17 @@ |
134 | 142 | this.listWidth = parseInt(getStyle(this.$el, 'width')); |
135 | 143 | this.updatePos(); |
136 | 144 | }); |
145 | + }, | |
146 | + setAutoplay () { | |
147 | + if (this.autoplay) { | |
148 | + this.timer = window.setInterval(() => { | |
149 | + this.currentIndex ++; | |
150 | + if (this.currentIndex === this.slides.length) this.currentIndex = 0; | |
151 | + this.trackLeft = this.currentIndex * this.listWidth; | |
152 | + }, this.autoplaySpeed); | |
153 | + } else { | |
154 | + window.clearInterval(this.timer); | |
155 | + } | |
137 | 156 | } |
138 | 157 | }, |
139 | 158 | compiled () { |
... | ... | @@ -153,6 +172,14 @@ |
153 | 172 | }); |
154 | 173 | } |
155 | 174 | }, |
175 | + watch: { | |
176 | + autoplay () { | |
177 | + this.setAutoplay(); | |
178 | + }, | |
179 | + current () { | |
180 | + this.switch(this.current); | |
181 | + } | |
182 | + }, | |
156 | 183 | ready () { |
157 | 184 | this.handleResize(); |
158 | 185 | window.addEventListener('resize', this.handleResize, false); | ... | ... |
src/styles/components/carousel.less