Skip to content

Commit

Permalink
feat(carousel): Transition event with setTimeout fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorehouse committed Aug 13, 2017
1 parent 6746cb1 commit 8e6fc42
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions lib/components/carousel.vue
Expand Up @@ -90,6 +90,9 @@
overlayClass: 'carousel-item-prev'
}
};
// Fallback Transition duration (with a little buffer) in ms
const TRANS_DURATION = 600 + 50;
// Transition Event names
const TransitionEndEvents = {
Expand All @@ -107,7 +110,7 @@
}
}
// fallback
return 'transitionend';
return null;
}
export default {
Expand All @@ -116,7 +119,7 @@
index: this.value || 0,
isSliding: false,
intervalId: null,
transitionEndEvent: 'transitionend',
transitionEndEvent: null,
slides: []
};
},
Expand Down Expand Up @@ -229,7 +232,7 @@
});
this.intervalId = setInterval(() => {
this.next();
}, this.interval);
}, Math.min(1000, this.interval));
},
// Re-Start auto rotate slides when focus/hover leaves the carousel
Expand Down Expand Up @@ -272,16 +275,6 @@
}
},
mounted() {
// Cache current browser transitionend event name
this.transitionEndEvent = getTransisionEndEvent(this.$el);
// Get all slides
this.updateSlides();
// Observe child changes so we can update slide list
observeDom(this.$refs.inner, this.updateSlides.bind(this), {subtree: false});
},
watch: {
value(newVal, oldVal) {
if (newVal !== oldVal) {
Expand Down Expand Up @@ -341,8 +334,16 @@
nextSlide.classList.add(direction.dirClass);
// Transition End handler
let called = false;
const onceTransEnd = (evt) => {
currentSlide.removeEventListener(this.transitionEndEvent, onceTransEnd);
if (called) {
return;
}
called = true;
if (this.transitionEndEvent) {
currentSlide.removeEventListener(this.transitionEndEvent, onceTransEnd);
}
this._animationTimeout = null;
nextSlide.classList.remove(direction.dirClass);
nextSlide.classList.remove(direction.overlayClass);
Expand Down Expand Up @@ -374,11 +375,31 @@
};
// Clear transition classes after transition ends
currentSlide.addEventListener(this.transitionEndEvent, onceTransEnd);
if (this.transitionEndEvent) {
currentSlide.addEventListener(this.transitionEndEvent, onceTransEnd);
}
// Fallback to setTimeout
this._animationTimeout = setTimeout(onceTransEnd, TRANS_DURATION);
}
},
created() {
// Create private non-reactive props
this._animationTimeout = null;
},
mounted() {
// Cache current browser transitionend event name
this.transitionEndEvent = getTransisionEndEvent(this.$el) || null;
// Get all slides
this.updateSlides();
// Observe child changes so we can update slide list
observeDom(this.$refs.inner, this.updateSlides.bind(this), {subtree: false});
},
destroyed() {
clearInterval(this.intervalId);
clearTimeout(this._animationTimeout);
this._animationTimeout = null;
}
};
Expand Down

0 comments on commit 8e6fc42

Please sign in to comment.