seng-scroll-tracker
Version:
Class that keeps track of the vertical scroll position of an element.
84 lines (83 loc) • 3.46 kB
TypeScript
import sengEvent from 'seng-event';
import ScrollTrackerPoint from './ScrollTrackerPoint';
import Axis from './enum/Axis';
import Side from './enum/Side';
/**
* Class that keeps track of the vertical scroll position of an element.
*/
export default class ScrollTracker extends sengEvent {
private element;
private targetAxis;
private static _DEFAULT_THROTTLE_SCROLL;
private static _DEFAULT_THROTTLE_RESIZE;
trackingPoints: Array<ScrollTrackerPoint>;
viewSize: number;
scrollSize: number;
viewStart: number;
viewEnd: number;
protected lastScrollPosition: number;
constructor(element?: HTMLElement | Window, targetAxis?: Axis);
/**
* Returns which axis this ScrollTracker instance is tracking.
*/
readonly axis: Axis;
/**
* Returns the target element this ScrollTracker instance is tracking.
*/
readonly targetElement: HTMLElement | Window;
/**
* Updates the size of the viewport of the target element.
*/
updateSize(): void;
/**
* Adds a new point of which we will detect when it enters and leaves the view.
* @param position The position of this points in pixels. This is the distance from the start
* or end of the target element depending on the 'side' parameter, measured horizontally or
* vertically depending on the axis of this ScrollTracker instance.
* @param side The side from which the 'position' parameter is defined. Side.START measures the
* position from the top or left edge and Side.END will measure the position from the bottom
* or right edge.
* @returns {ScrollTrackerPoint} A reference to a ScrollTrackerPoint instance that can be
* used to bind events, remove or update the point added.
*/
addPoint(position: number, height?: number, side?: Side): ScrollTrackerPoint;
/**
* Removes an existing point from this ScrollTracker. This point will be destructed and will
* no longer throw events.
* @param point The ScrollTrackerPoint instance to remove.
* @returns {boolean} Boolean indicating if the point was found and removed successfully.
*/
removePoint(point: ScrollTrackerPoint): boolean;
/**
* Removes all points from this ScrollTracker instance. They will be destructed and will
* no longer throw events.
*/
removeAllPoints(): void;
/**
* Initialize scroll and resize events using jQuery. Resize events will only be used when
* the target of ScrollTracker is 'window'. If the target is not window, updateSize() has
* to be called manually to update the view size.
*/
protected initEvents(): void;
/**
* Handles events thrown by ScrollTrackerPoint instances and bubbles them up to this
* ScrollTracker instance.
* @param event The event thrown.
*/
private pointEventHandler;
protected updateScrollPosition(): void;
/**
* Event handler called when the target element is scrolled. Will detect the new scroll
* position and call checkInView() on all tracking points.
*/
protected scrollHandler: () => void;
/**
* Event handler called when the window resizes. Only used when the target of this ScrollTracker
* instance is the window object.
*/
protected windowResizeHandler: () => void;
/**
* Disposes this ScrollTracker and all points created on it. Removes all event handlers.
*/
dispose(): void;
}