reset autoplay timer after trigger

add prop: autoplay direction
This commit is contained in:
Rijn 2017-01-20 11:54:25 -06:00
parent 1cc0661228
commit 62808b2b03
2 changed files with 21 additions and 2 deletions

View file

@ -47,6 +47,13 @@
type: Number, type: Number,
default: 2000 default: 2000
}, },
autoplayDirection: {
type: String,
default: 'left',
validator (value) {
return oneOf(value, ['left', 'right']);
}
},
easing: { easing: {
type: String, type: String,
default: 'ease' default: 'ease'
@ -175,11 +182,13 @@
this.updateSlides(true, true); this.updateSlides(true, true);
this.updatePos(); this.updatePos();
this.updateOffset();
}); });
}, },
handleResize () { handleResize () {
this.listWidth = parseInt(getStyle(this.$el, 'width')); this.listWidth = parseInt(getStyle(this.$el, 'width'));
this.updatePos(); this.updatePos();
this.updateOffset();
}, },
add (offset) { add (offset) {
let index = this.currentIndex; let index = this.currentIndex;
@ -191,19 +200,20 @@
dotsEvent (event, n) { dotsEvent (event, n) {
if (event === this.trigger && this.currentIndex !== n) { if (event === this.trigger && this.currentIndex !== n) {
this.currentIndex = n; this.currentIndex = n;
// Reset autoplay timer when trigger be activated
this.setAutoplay();
} }
}, },
setAutoplay () { setAutoplay () {
window.clearInterval(this.timer); window.clearInterval(this.timer);
if (this.autoplay) { if (this.autoplay) {
this.timer = window.setInterval(() => { this.timer = window.setInterval(() => {
this.add(1); this.add(this.autoplayDirection === 'left' ? 1 : -1);
}, this.autoplaySpeed); }, this.autoplaySpeed);
} }
}, },
updateOffset () { updateOffset () {
this.$nextTick(() => { this.$nextTick(() => {
this.handleResize();
this.trackOffset = this.currentIndex * this.listWidth; this.trackOffset = this.currentIndex * this.listWidth;
}); });
} }

View file

@ -11,6 +11,13 @@
<i-col span="4"> <i-col span="4">
Speed <Slider :value.sync="autoplaySpeed" :min="300" :max="5000"></Slider> Speed <Slider :value.sync="autoplaySpeed" :min="300" :max="5000"></Slider>
</i-col> </i-col>
<i-col span="4">
<p>Direction</p>
<Button-group>
<i-button @click="autoplayDirection = 'left'">Left</i-button>
<i-button @click="autoplayDirection = 'right'">Right</i-button>
</Button-group>
</i-col>
<i-col span="4"> <i-col span="4">
Switch To Switch To
<Button-group> <Button-group>
@ -59,6 +66,7 @@
:current-index.sync="currentIndex" :current-index.sync="currentIndex"
:autoplay="autoplay" :autoplay="autoplay"
:autoplay-speed="autoplaySpeed" :autoplay-speed="autoplaySpeed"
:autoplay-direction="autoplayDirection"
:dots="dots" :dots="dots"
:trigger="trigger" :trigger="trigger"
:arrow="arrow" :arrow="arrow"
@ -185,6 +193,7 @@
currentIndex: 0, currentIndex: 0,
autoplay: true, autoplay: true,
autoplaySpeed: 2000, autoplaySpeed: 2000,
autoplayDirection: 'left',
remove: false, remove: false,
pushItem: [], pushItem: [],
arrow: 'hover', arrow: 'hover',