updateAutoHeight.js 1.5 KB

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