@blinkk/degu
    Preparing search index...

    Function getElementScrolledPercent

    • Given an element, returns the amount that the element has been scrolled through in the the window.

      Example: This method is particularly useful when you have a 100vh sticky element and want a relative distance travelled percentage.

      HTML
      <div class="parent">
      <div class="child"><div class="content"></div>
      </div>

      SASS
      .parent
      position: relative
      height: 300vh // Should be more than 100vh
      .child
      position: sticky
      height: 100vh
      width: 100%
      top: 0
      .contents
      position: relative
      height: 100vh

      JS

      window.addEventListener('scroll', () => {
      let progress =
      dom.getElementScrolledPercent(this.stickyParent_);
      // 0 when the above the element,
      // increases as you scroll thorugh
      // 1 when the bottom of the element reaches the bottom of the screen.
      console.log(progress);
      });

      Top and bottom offset. You can offset the top and bottom so that the calculations are shifted.


      let startOffset = 100;
      // Now progress will start when the element is 100px inview.
      // But still will complete normally at the bottom.
      let progress =
      mathf.getElementScrolledPercent(this.element, startOffset, 0);

      let startOffset = window.innerHeight;
      // Now progress when the top of the element hits the top of the screen.
      // But still will complete normally at the bottom.
      let progress =
      mathf.getElementScrolledPercent(this.element, startOffset, 0);

      let heightOffset = -100;
      // Entry as normal - 0 when the element first comes into view.
      // Now progress will complete get to 0 when there is still 100px
      // of the element still to be shown since it is virtually 100px shorter.
      let progress =
      mathf.getElementScrolledPercent(this.element, 0, heightOffset);

      // Now progress will complete when the element has scrolled past
      // the center of the screen since we virually extend the height by
      // 50vh.
      let heightOffset = window.innerHeight * 0.5;
      let progress =
      mathf.getElementScrolledPercent(this.element, 0, heightOffset);

      // Now progress will complete when the element has scrolled past
      // the very top of the screen.
      let heightOffset = window.innerHeight * 1;
      let progress =
      mathf.getElementScrolledPercent(this.element, 0, heightOffset);

      Provided the element is larger than the viewport height, this will return 0% when the element is above the screen, it increases in value as the user scrolls through the element and finally when the BOTTOM of the element just passes the bottom of the screen it returns 1.

      This method assumes that the tracked element is atleast more than 100vh.

      Parameters

      • element: HTMLElement

        The root element.

      • startOffset: number = 0

        A positive value (pixel) to offset the top position of the element. The start offset value should never be negative as technically, offseting to before the element comes into view doesn't have an effect and will only complicate things for you. Changing this value will affect the poitn at which your progress hits 0 (starts).

      • heightOffset: number = 0

        A value (pixel) to add to the total height of the element. Can be positive or negative values. Think of this, shortening or growing your element virually. Changing this value will affect the point at which your progress hits 1 (ends).

      • noClamp: boolean = false
      • innerHeight: number = 0

      Returns number

      percent The amount in percentage that the user has scrolled in the element.