Last active
March 23, 2021 21:21
-
-
Save vhoyer/1867ca18cf1a83e4f13dcb70450b600c to your computer and use it in GitHub Desktop.
A functional/stateless vue component for carousel using render function with vue-awesome-swiper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- npm install vue-awesome-swiper --> | |
<script> | |
import Vue from 'vue'; | |
import VueAwesomeSwiper from 'vue-awesome-swiper/dist/ssr'; | |
Vue.use(VueAwesomeSwiper); | |
export default { | |
name: 'MyCarousel', | |
functional: true, | |
render(h, { children }) { | |
const swiperOptions = { | |
breakpoints: { | |
900: { | |
slidesPerView: 'auto', | |
slidesPerGroup: 1, | |
}, | |
}, | |
slidesPerView: 4, | |
spaceBetween: 16, | |
slidesPerGroup: 4, | |
simulateTouch: false, | |
pagination: { | |
el: '.js-swiper-pagination', | |
clickable: true, | |
}, | |
}; | |
const slides = children.map((child) => { | |
console.warn(child); | |
if (!child.data) child.data = {}; | |
if (!child.data.class) child.data.class = []; | |
child.data.class.push('swiper-slide'); | |
return child; | |
}); | |
const swiperWrapper = h('div', { | |
class: ['swiper-wrapper'], | |
}, slides); | |
const swiperPagination = h('div', { | |
class: 'carousel__pagination swiper-pagination swiper-pagination-bullets js-swiper-pagination', | |
}); | |
const swiperPreviusButton = h('div', { class: 'swiper-button-prev' }); | |
const swiperNextButton = h('div', { class: 'swiper-button-next' }); | |
return h('div', { | |
directives: [ | |
{ | |
name: 'swiper', | |
arg: 'mySwiper', | |
value: swiperOptions, | |
}, | |
], | |
}, [ | |
swiperWrapper, | |
swiperPagination, | |
swiperPreviusButton, | |
swiperNextButton, | |
]); | |
}, | |
}; | |
</script> | |
<style scoped> | |
.carousel__pagination { | |
position: static; | |
margin-top: 16px; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment