UNPKG

1.52 kBJavaScriptView Raw
1export default function updateAutoHeight(speed) {
2 const swiper = this;
3 const activeSlides = [];
4 const isVirtual = swiper.virtual && swiper.params.virtual.enabled;
5 let newHeight = 0;
6 let i;
7
8 if (typeof speed === 'number') {
9 swiper.setTransition(speed);
10 } else if (speed === true) {
11 swiper.setTransition(swiper.params.speed);
12 }
13
14 const getSlideByIndex = index => {
15 if (isVirtual) {
16 return swiper.slides.filter(el => parseInt(el.getAttribute('data-swiper-slide-index'), 10) === index)[0];
17 }
18
19 return swiper.slides.eq(index)[0];
20 }; // Find slides currently in view
21
22
23 if (swiper.params.slidesPerView !== 'auto' && swiper.params.slidesPerView > 1) {
24 if (swiper.params.centeredSlides) {
25 swiper.visibleSlides.each(slide => {
26 activeSlides.push(slide);
27 });
28 } else {
29 for (i = 0; i < Math.ceil(swiper.params.slidesPerView); i += 1) {
30 const index = swiper.activeIndex + i;
31 if (index > swiper.slides.length && !isVirtual) break;
32 activeSlides.push(getSlideByIndex(index));
33 }
34 }
35 } else {
36 activeSlides.push(getSlideByIndex(swiper.activeIndex));
37 } // Find new height from highest slide in view
38
39
40 for (i = 0; i < activeSlides.length; i += 1) {
41 if (typeof activeSlides[i] !== 'undefined') {
42 const height = activeSlides[i].offsetHeight;
43 newHeight = height > newHeight ? height : newHeight;
44 }
45 } // Update Height
46
47
48 if (newHeight || newHeight === 0) swiper.$wrapperEl.css('height', `${newHeight}px`);
49}
\No newline at end of file