updateAutoHeight.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. export default async 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) ===
  15. index)[
  16. 0];
  17. }
  18. return swiper.slides[index];
  19. }; // Find slides currently in view
  20. if (swiper.params.slidesPerView !== 'auto' && swiper.params.slidesPerView > 1) {
  21. if (swiper.params.centeredSlides) {
  22. swiper.visibleSlides.each(slide => {
  23. activeSlides.push(slide);
  24. });
  25. } else {
  26. for (i = 0; i < Math.ceil(swiper.params.slidesPerView); i += 1) {
  27. const index = swiper.activeIndex + i;
  28. if (index > swiper.slides.length && !isVirtual) break;
  29. activeSlides.push(getSlideByIndex(index));
  30. }
  31. }
  32. } else {
  33. activeSlides.push(getSlideByIndex(swiper.activeIndex));
  34. } // Find new height from highest slide in view
  35. for (i = 0; i < activeSlides.length; i += 1) {
  36. if (typeof activeSlides[i] !== 'undefined') {
  37. const size = await activeSlides[i].getSize();
  38. const height = size.height;
  39. newHeight = height > newHeight ? height : newHeight;
  40. }
  41. } // Update Height
  42. if (newHeight || newHeight === 0) swiper.$wrapperEl.css({
  43. height: `${newHeight?newHeight:''}px`
  44. });
  45. }