From 3e6d63565044ea80edb47fe1f4312e331cf199e9 Mon Sep 17 00:00:00 2001 From: Rijn Date: Mon, 16 Jan 2017 04:12:46 -0600 Subject: [PATCH] implement basic timer --- src/components/carousel/carousel.vue | 31 +++++++++++++++++++++++++++++-- src/styles/components/carousel.less | 3 +++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/components/carousel/carousel.vue b/src/components/carousel/carousel.vue index e048061..458b60e 100644 --- a/src/components/carousel/carousel.vue +++ b/src/components/carousel/carousel.vue @@ -46,6 +46,10 @@ vertical: { type: Boolean, default: false + }, + currentIndex: { + type: Number, + default: 0 } }, data () { @@ -53,8 +57,10 @@ prefixCls: prefixCls, listWidth: 0, trackWidth: 0, + trackLeft: 0, slides: [], - slideInstances: [] + slideInstances: [], + timer: null } }, // events: before-change(from, to), after-change(current, from) @@ -69,7 +75,9 @@ }, trackStyles () { return { - width: `${this.trackWidth}px` + width: `${this.trackWidth}px`, + transform: `translate3d(-${this.trackLeft}px, 0px, 0px)`, + transition: `transform 500ms ${this.easing}` }; } }, @@ -134,6 +142,17 @@ this.listWidth = parseInt(getStyle(this.$el, 'width')); this.updatePos(); }); + }, + setAutoplay () { + if (this.autoplay) { + this.timer = window.setInterval(() => { + this.currentIndex ++; + if (this.currentIndex === this.slides.length) this.currentIndex = 0; + this.trackLeft = this.currentIndex * this.listWidth; + }, this.autoplaySpeed); + } else { + window.clearInterval(this.timer); + } } }, compiled () { @@ -153,6 +172,14 @@ }); } }, + watch: { + autoplay () { + this.setAutoplay(); + }, + current () { + this.switch(this.current); + } + }, ready () { this.handleResize(); window.addEventListener('resize', this.handleResize, false); diff --git a/src/styles/components/carousel.less b/src/styles/components/carousel.less index 87557dc..a33e20f 100644 --- a/src/styles/components/carousel.less +++ b/src/styles/components/carousel.less @@ -27,11 +27,14 @@ top: 0; left: 0; display: block; + + overflow: hidden; } &-item { float: left; height: 100%; min-height: 1px; + display: block; } } -- libgit2 0.21.4