Skip to content

Commit

Permalink
feat(carousel): Use transitionend event instead of setTimeout
Browse files Browse the repository at this point in the history
setTimeout for timing transitions can be iffy at best (especially when view port is obscured)

This fix removes the setTimeout and replaces it with a "once" `transitionend` event
  • Loading branch information
tmorehouse committed Aug 12, 2017
1 parent 0869ffd commit 192dfb8
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions lib/components/carousel.vue
Expand Up @@ -77,18 +77,16 @@


<script> <script>
import { from as arrayFrom } from '../utils/array'; import { from as arrayFrom } from '../utils/array';
import {observeDom} from '../utils'; import {addEventListenerOnce, observeDom} from '../utils';
const DIRECTION = { const DIRECTION = {
next: { next: {
current: 'carousel-item-left', dirClass: 'carousel-item-left',
next: 'carousel-item-left', overlayClass: 'carousel-item-next'
overlay: 'carousel-item-next'
}, },
prev: { prev: {
current: 'carousel-item-right', dirClass: 'carousel-item-right',
next: 'carousel-item-right', overlayClass: 'carousel-item-prev'
overlay: 'carousel-item-prev'
} }
}; };
Expand Down Expand Up @@ -253,10 +251,6 @@
} }
}, },
created() {
// Create private properties
this._carouselAnimation = null;
},
mounted() { mounted() {
// Get all slides // Get all slides
this.updateSlides(); this.updateSlides();
Expand Down Expand Up @@ -314,28 +308,29 @@
// Update v-model // Update v-model
this.$emit('input', this.index); this.$emit('input', this.index);
nextSlide.classList.add(direction.overlay); nextSlide.classList.add(direction.overlayClass);
// Trigger a reflow of next slide // Trigger a reflow of next slide
// eslint-ignore-next-line no-void // eslint-ignore-next-line no-void
void(nextSlide.offsetHeight); void(nextSlide.offsetHeight);
currentSlide.classList.add(direction.current); currentSlide.classList.add(direction.dirClasst);
nextSlide.classList.add(direction.next); nextSlide.classList.add(direction.dirClass);
// Clear transition classes after 0.6s transition duration // Clear transition classes after transition ends
this._carouselAnimation = setTimeout(() => { addEventListenerOnce(currentSlide, 'transitionend', (evt) => {
nextSlide.classList.remove(direction.next); nextSlide.classList.remove(direction.dirClass);
nextSlide.classList.remove(direction.overlay); nextSlide.classList.remove(direction.overlayClass);
nextSlide.classList.add('active'); nextSlide.classList.add('active');
currentSlide.classList.remove('active'); currentSlide.classList.remove('active');
currentSlide.classList.remove(direction.current); currentSlide.classList.remove(direction.dirClass);
currentSlide.classList.remove(direction.overlay); currentSlide.classList.remove(direction.overlayClass);
currentSlide.setAttribute('aria-current', 'false'); currentSlide.setAttribute('aria-current', 'false');
nextSlide.setAttribute('aria-current', 'true'); nextSlide.setAttribute('aria-current', 'true');
currentSlide.setAttribute('aria-hidden', 'true'); currentSlide.setAttribute('aria-hidden', 'true');
nextSlide.setAttribute('aria-hidden', 'false'); nextSlide.setAttribute('aria-hidden', 'false');
currentSlide.tabIndex = -1; currentSlide.tabIndex = -1;
nextSlide.tabIndex = -1; nextSlide.tabIndex = -1;
Expand All @@ -346,15 +341,14 @@
nextSlide.focus(); nextSlide.focus();
}); });
} }
this.isSliding = false; this.isSliding = false;
// Notify ourselves that we're done sliding (slid) // Notify ourselves that we're done sliding (slid)
this.$emit('slid', val); this.$nextTick(() => this.$emit('slid', val));
});
}, 601);
} }
}, },
destroyed() { destroyed() {
clearTimeout(this._carouselAnimation);
clearInterval(this.intervalId); clearInterval(this.intervalId);
} }
}; };
Expand Down

0 comments on commit 192dfb8

Please sign in to comment.