@@ -702,13 +702,17 @@ export const Carousel = defineComponent({
702702 let accumulatedSize = 0
703703 let index = 0 - clonedSlidesCount . value . before
704704 const offset = Math . abs ( scrolledOffset . value + clonedSlidesOffset . value )
705+ let iterations = 0
706+ const maxIterations = slides . length * 2
705707
706- while ( accumulatedSize <= offset ) {
708+ while ( accumulatedSize <= offset && iterations < maxIterations ) {
707709 const normalizedIndex =
708710 ( ( index % slides . length ) + slides . length ) % slides . length
709- accumulatedSize +=
710- slidesRect . value [ normalizedIndex ] ?. [ dimension . value ] + config . gap
711+ const slideSize = slidesRect . value [ normalizedIndex ] ?. [ dimension . value ] || 0
712+ if ( slideSize <= 0 ) break
713+ accumulatedSize += slideSize + config . gap
711714 index ++
715+ iterations ++
712716 }
713717 minIndex = index - 1
714718 }
@@ -717,6 +721,9 @@ export const Carousel = defineComponent({
717721 {
718722 let index = minIndex
719723 let accumulatedSize = 0
724+ let iterations = 0
725+ const maxIterations = slides . length * 2
726+
720727 if ( index < 0 ) {
721728 accumulatedSize =
722729 slidesRect . value
@@ -731,12 +738,14 @@ export const Carousel = defineComponent({
731738 Math . abs ( scrolledOffset . value )
732739 }
733740
734- while ( accumulatedSize < viewportRect . value [ dimension . value ] ) {
741+ while ( accumulatedSize < viewportRect . value [ dimension . value ] && iterations < maxIterations ) {
735742 const normalizedIndex =
736743 ( ( index % slides . length ) + slides . length ) % slides . length
737- accumulatedSize +=
738- slidesRect . value [ normalizedIndex ] ?. [ dimension . value ] + config . gap
744+ const slideSize = slidesRect . value [ normalizedIndex ] ?. [ dimension . value ] || 0
745+ if ( slideSize <= 0 ) break
746+ accumulatedSize += slideSize + config . gap
739747 index ++
748+ iterations ++
740749 }
741750 maxIndex = index - 1
742751 }
0 commit comments