updateAutoHeight.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. export 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. if (typeof speed === 'number') {
  8. swiper.setTransition(speed);
  9. } else if (speed === true) {
  10. swiper.setTransition(swiper.params.speed);
  11. }
  12. const getSlideByIndex = index => {
  13. if (isVirtual) {
  14. return swiper.slides.filter(el => parseInt(el.getAttribute('data-swiper-slide-index'), 10) === index)[0];
  15. }
  16. return swiper.slides.eq(index)[0];
  17. }; // Find slides currently in view
  18. if (swiper.params.slidesPerView !== 'auto' && swiper.params.slidesPerView > 1) {
  19. if (swiper.params.centeredSlides) {
  20. swiper.visibleSlides.each(slide => {
  21. activeSlides.push(slide);
  22. });
  23. } else {
  24. for (i = 0; i < Math.ceil(swiper.params.slidesPerView); i += 1) {
  25. const index = swiper.activeIndex + i;
  26. if (index > swiper.slides.length && !isVirtual) break;
  27. activeSlides.push(getSlideByIndex(index));
  28. }
  29. }
  30. } else {
  31. activeSlides.push(getSlideByIndex(swiper.activeIndex));
  32. } // Find new height from highest slide in view
  33. for (i = 0; i < activeSlides.length; i += 1) {
  34. if (typeof activeSlides[i] !== 'undefined') {
  35. const height = activeSlides[i].offsetHeight;
  36. newHeight = height > newHeight ? height : newHeight;
  37. }
  38. } // Update Height
  39. if (newHeight || newHeight === 0) swiper.$wrapperEl.css('height', `${newHeight}px`);
  40. }