Commit 3e6d63565044ea80edb47fe1f4312e331cf199e9

Authored by Rijn
1 parent 65c64ce9

implement basic timer

src/components/carousel/carousel.vue
@@ -46,6 +46,10 @@ @@ -46,6 +46,10 @@
46 vertical: { 46 vertical: {
47 type: Boolean, 47 type: Boolean,
48 default: false 48 default: false
  49 + },
  50 + currentIndex: {
  51 + type: Number,
  52 + default: 0
49 } 53 }
50 }, 54 },
51 data () { 55 data () {
@@ -53,8 +57,10 @@ @@ -53,8 +57,10 @@
53 prefixCls: prefixCls, 57 prefixCls: prefixCls,
54 listWidth: 0, 58 listWidth: 0,
55 trackWidth: 0, 59 trackWidth: 0,
  60 + trackLeft: 0,
56 slides: [], 61 slides: [],
57 - slideInstances: [] 62 + slideInstances: [],
  63 + timer: null
58 } 64 }
59 }, 65 },
60 // events: before-change(from, to), after-change(current, from) 66 // events: before-change(from, to), after-change(current, from)
@@ -69,7 +75,9 @@ @@ -69,7 +75,9 @@
69 }, 75 },
70 trackStyles () { 76 trackStyles () {
71 return { 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,6 +142,17 @@
134 this.listWidth = parseInt(getStyle(this.$el, 'width')); 142 this.listWidth = parseInt(getStyle(this.$el, 'width'));
135 this.updatePos(); 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 compiled () { 158 compiled () {
@@ -153,6 +172,14 @@ @@ -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 ready () { 183 ready () {
157 this.handleResize(); 184 this.handleResize();
158 window.addEventListener('resize', this.handleResize, false); 185 window.addEventListener('resize', this.handleResize, false);
src/styles/components/carousel.less
@@ -27,11 +27,14 @@ @@ -27,11 +27,14 @@
27 top: 0; 27 top: 0;
28 left: 0; 28 left: 0;
29 display: block; 29 display: block;
  30 +
  31 + overflow: hidden;
30 } 32 }
31 33
32 &-item { 34 &-item {
33 float: left; 35 float: left;
34 height: 100%; 36 height: 100%;
35 min-height: 1px; 37 min-height: 1px;
  38 + display: block;
36 } 39 }
37 } 40 }