@@ -72,6 +72,10 @@ export function getElementDistanceToBottomViewPort(element) {
7272 return document . documentElement . clientHeight - element . getBoundingClientRect ( ) . bottom ;
7373}
7474
75+ export function getElementDistanceToTopViewPort ( element ) {
76+ return element . getBoundingClientRect ( ) . top ;
77+ }
78+
7579/**
7680* A strategy for repeating a template over an array.
7781*/
@@ -102,7 +106,7 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy {
102106 let viewsLength = repeat . viewCount ( ) ;
103107 let first = repeat . _getIndexOfFirstView ( ) ;
104108 // remove unneeded views.
105- while ( viewsLength > repeat . _viewsLength ) {
109+ while ( viewsLength > itemsLength ) {
106110 viewsLength -- ;
107111 repeat . removeView ( viewsLength , true ) ;
108112 }
@@ -448,6 +452,7 @@ export class VirtualRepeat extends AbstractRepeater {
448452 this . bottomBuffer = this . viewStrategy . createBottomBufferElement ( element ) ;
449453 this . itemsChanged ( ) ;
450454 this . scrollListener = ( ) => this . _onScroll ( ) ;
455+ this . distanceToTop = getElementDistanceToTopViewPort ( this . topBuffer . nextElementSibling ) ;
451456 let containerStyle = this . scrollContainer . style ;
452457 if ( containerStyle . overflowY === 'scroll' || containerStyle . overflow === 'scroll' || containerStyle . overflowY === 'auto' || containerStyle . overflow === 'auto' ) {
453458 this . _fixedHeightContainer = true ;
@@ -484,6 +489,7 @@ export class VirtualRepeat extends AbstractRepeater {
484489 this . isLastIndex = false ;
485490 this . scrollContainer = null ;
486491 this . scrollContainerHeight = null ;
492+ this . distanceToTop = null ;
487493 this . removeAllViews ( true ) ;
488494 if ( this . scrollHandler ) {
489495 this . scrollHandler . dispose ( ) ;
@@ -499,7 +505,7 @@ export class VirtualRepeat extends AbstractRepeater {
499505 }
500506 let items = this . items ;
501507 this . strategy = this . strategyLocator . getStrategy ( items ) ;
502- if ( items . length > 0 ) {
508+ if ( items . length > 0 && this . viewCount ( ) === 0 ) {
503509 this . strategy . createFirstItem ( this ) ;
504510 }
505511 this . _calcInitialHeights ( items . length ) ;
@@ -559,7 +565,7 @@ export class VirtualRepeat extends AbstractRepeater {
559565 return ;
560566 }
561567 let itemHeight = this . itemHeight ;
562- let scrollTop = this . _fixedHeightContainer ? this . scrollContainer . scrollTop : pageYOffset - this . topBuffer . offsetTop ;
568+ let scrollTop = this . _fixedHeightContainer ? this . scrollContainer . scrollTop : pageYOffset - this . distanceToTop ;
563569 this . _first = Math . floor ( scrollTop / itemHeight ) ;
564570 this . _first = this . _first < 0 ? 0 : this . _first ;
565571 if ( this . _first > this . items . length - this . elementsInView ) {
@@ -653,17 +659,19 @@ export class VirtualRepeat extends AbstractRepeater {
653659 let items = this . items ;
654660 let index = this . _scrollingDown ? this . _getIndexOfLastView ( ) + 1 : this . _getIndexOfFirstView ( ) - 1 ;
655661 let i = 0 ;
662+ let viewToMoveLimit = length - ( childrenLength * 2 ) ;
656663 while ( i < length && ! isAtFirstOrLastIndex ( ) ) {
657664 let view = this . view ( viewIndex ) ;
658665 let nextIndex = getNextIndex ( index , i ) ;
659666 this . isLastIndex = nextIndex >= items . length - 1 ;
660667 this . _isAtTop = nextIndex <= 0 ;
661668 if ( ! ( isAtFirstOrLastIndex ( ) && childrenLength >= items . length ) ) {
662- rebindAndMoveView ( this , view , nextIndex , this . _scrollingDown ) ;
669+ if ( i > viewToMoveLimit ) {
670+ rebindAndMoveView ( this , view , nextIndex , this . _scrollingDown ) ;
671+ }
663672 i ++ ;
664673 }
665674 }
666-
667675 return length - ( length - i ) ;
668676 }
669677
0 commit comments