updateAutoHeight.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. console.log(swiper.visibleSlides)
  23. if(swiper.visibleSlides){
  24. for (const visibleKey in swiper.visibleSlides) {
  25. activeSlides.push(swiper.visibleSlides[visibleKey]);
  26. }
  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. for (i = 0; i < activeSlides.length; i += 1) {
  39. if (typeof activeSlides[i] !== 'undefined') {
  40. const size = await activeSlides[i].getSize();
  41. const height = size.height;
  42. newHeight = height > newHeight ? height : newHeight;
  43. }
  44. } // Update Height
  45. if (newHeight || newHeight === 0) swiper.$wrapperEl.css({
  46. height: `${newHeight?newHeight:''}px`
  47. });
  48. }