Skip to content
riccoarntz edited this page Feb 22, 2019 · 36 revisions

Description

The ScrollTrackerComponentManager is a Class that tracks whether a component is within your viewport based on your scroll position. It will/can handle the following for you:

  • Trigger methods such as enterView or leaveView, once you component enters/leaves your viewport.
  • Update your component with a progress value between 0 and 1. This is the progress of you components visibility.
  • Enable smooth-scroll if needed.

Constructing and adding components.

  const components = [new DummyFoo(), new DummyFoo2()];
  const scrollTrackerComponentManager = new ScrollTrackerComponentManager();
 
  scrollTrackerComponentManager.addComponentsToScrollTracker(components);

Configuration - IScrollTrackerComponentManagerOptions.

Methods/properties of the component that are triggered which can be overwritten to add components with a different/custom interface.

  const scrollTrackerComponentManager = new ScrollTrackerComponentManager({
    container: window,
    element: 'element',

    enterView: 'enterView',
    leaveView: 'leaveView',
    beyondView: 'beyondView',
    inViewProgress: 'inViewProgress',

    inViewProgressThreshold: 'inViewProgressThreshold',
    enterViewThreshold: 'enterViewThreshold',
    componentId: 'componentId',
    hasEntered: 'hasEntered',
    currentViewProgress: 'currentViewProgress',

    inViewProgressEnabled: false,
    setDebugLabel: true,
    debugBorderColor: 'red',
    scrollThrottle: 100,
    resizeDebounce: 100,

    enableSmoothScroll: false,
    smoothScrollOptions: {
      damping: 0.1,
      thumbMinSize: 20,
      renderByPixels: true,
      alwaysShowTracks: false,
      continuousScrolling: true,
      wheelEventTarget: null,
      plugins: {},
    },
  });
  • container - the container that will be the scroll-area, default is the window.
  • element - PropertyName of the component that contains the HTMLElement of the component.
  • enterView - MethodName of the component which is triggered when the component is within the viewport.
  • leaveView - MethodName of the component which is triggered when the component has left the viewport.
  • beyondView - MethodName of the component which is triggered everytime it is already scrolled passed a component, or when you would load a page while the scrollbar is already at the bottom or passed a component.
  • inViewProgress - Method name of the component which is triggered every time the scroll-position changes. This method will have the parameter progress which is a number between 0-1.
  • enterViewThreshold - PropertyName of the component that contains a number between 0 - 1. Setting this number to for example 0.5 will trigger the enterView method when the component is already visible for 50% within your viewport.
  • inViewProgressThreshold - same as enterViewThreshold but then used as an offset from when your inViewProgress will start.
  • hasEntered - PropertyName of the component that should is by default set to false. Will be set to value if it has passed the viewport once already.
  • currentViewProgress - PropertyName of the component where we will store the progress of its visibility.
  • inViewProgressEnabled - When this is set to true we will call the method inViewProgress(progress). By default this is set to false.
  • componentId - PropertyName of the component that should contain a unique string for each added component.
  • setDebugLabel - Enable/Disable visible scroll-tracker-points for each component, this will allow to you see when the transitionIn/Out is called.
  • debugBorderColor - Color of the scroll-tracker-points (top/bottom line).
  • resizeDebounce - Number in milliseconds to update the scroll-tracker-points with a debounce if the window is being resized.
  • scrollThrottle - Number in milliseconds used to throttle the scroll event.
  • enableSmoothScroll - Enable/Disable smooth-scroll, see documentation below. By default this is set to false
  • smoothScrollOptions - See documentation below.

Smoothscroll ( EXAMPLE )

In case you want to enable smooth-scroll, read their documentation for all the possibilities.

Removing components from the scrollTrackerComponentManager

  scrollTrackerComponentManager.removeComponentsFromScrollTracker(components);

Disposing scrollTrackerComponentManager

  scrollTrackerComponentManager.dispose();

Example component

export default abstract class AbstractScrollComponent {
  public componentId: string;
  public enterViewThreshold: number = 0;
  public inViewProgressThreshold: number = 0;
  public currentViewProgress: number = 0;
  static eventNamespaceCount: number = 10000000;
  public eventNamespace: string = '';

  constructor(private element: HTMLElement) {
    this.eventNamespace = '.' + ++AbstractScrollComponent.eventNamespaceCount;
    this.componentId = this.displayName + this.eventNamespace;
  }

  public inViewProgress(progress: number): void {
  }

  public enterView(): void {
  }

  public leaveView(): void {
  }

  public beyondView(): void {
  }

  public get displayName() {
    return this.element.getAttribute(`data-component`);
  }
}
Clone this wiki locally