extensions/amp-position-observer/0.1/amp-position-observer.js
/**
   * Called by position observer.
   * It calculates visibility and progress, and triggers the appropriate events.
   * @param {!../../../src/service/position-observer-impl.PositionInViewportEntryDef} entry PositionObserver entry
   * @private
   */
Called by position observer.
It calculates visibility and progress, and triggers the appropriate events.
Called by position observer . It calculates visibility and progress and triggers the appropriate events .
positionChanged_(entry) {
    const wasVisible = this.isVisible_;
    const prevViewportHeight = this.viewportRect_ && this.viewportRect_.height;
    this.viewportRect_ = entry.viewportRect;
    if (prevViewportHeight != entry.viewportRect.height) {
      this.recalculateMargins_();
    }
    const adjViewportRect = this.applyMargins_(entry.viewportRect);
    const positionRect = entry.positionRect;
    let relPos;
    if (!positionRect) {
      this.isVisible_ = false;
      relPos = entry.relativePos;
    } else {
      relPos = layoutRectsRelativePos(positionRect, adjViewportRect);
      this.updateVisibility_(positionRect, adjViewportRect, relPos);
    }
    if (wasVisible && !this.isVisible_) {
      this.scrollProgress_ = relPos == RelativePositions.BOTTOM ? 0 : 1;
      this.triggerScroll_();
      this.triggerExit_();
    }
    if (!wasVisible && this.isVisible_) {
      this.triggerEnter_();
    }
    if (this.isVisible_) {
      this.updateScrollProgress_(positionRect, adjViewportRect);
      this.triggerScroll_();
    }
  }
positionChanged_ ( entry ) { const wasVisible = this . isVisible_ ; const prevViewportHeight = this . viewportRect_ && this . viewportRect_ . height ; this . viewportRect_ = entry . viewportRect ; if ( prevViewportHeight != entry . viewportRect . height ) { this . recalculateMargins_ ( ) ; } const adjViewportRect = this . applyMargins_ ( entry . viewportRect ) ; const positionRect = entry . positionRect ; let relPos ; if ( ! positionRect ) { this . isVisible_ = false ; relPos = entry . relativePos ; } else { relPos = layoutRectsRelativePos ( positionRect , adjViewportRect ) ; this . updateVisibility_ ( positionRect , adjViewportRect , relPos ) ; } if ( wasVisible && ! this . isVisible_ ) { this . scrollProgress_ = relPos == RelativePositions . BOTTOM ? 0 : 1 ; this . triggerScroll_ ( ) ; this . triggerExit_ ( ) ; } if ( ! wasVisible && this . isVisible_ ) { this . triggerEnter_ ( ) ; } if ( this . isVisible_ ) { this . updateScrollProgress_ ( positionRect , adjViewportRect ) ; this . triggerScroll_ ( ) ; } }
Called by position observer.
Called by position observer .
// Margins support viewport sizing. // Adjust viewport based on exclusion margins // Relative position of the element to the adjusted viewport. // Send final scroll progress state before exiting to handle fast-scroll. // Send scroll progress if visible.