Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ScrollTracker

Class that keeps track of the vertical scroll position of an element.

Hierarchy

  • EventDispatcher
    • ScrollTracker

Implements

  • IDisposable
  • IEventDispatcher

Index

Constructors

constructor

  • new ScrollTracker(element?: HTMLElement | Window, targetAxis?: Axis): ScrollTracker
  • Parameters

    • Default value element: HTMLElement | Window = window
    • Default value targetAxis: Axis = Axis.Y

    Returns ScrollTracker

Properties

Private element

element: HTMLElement | Window

Private lastScrollPosition

lastScrollPosition: number = 0

parent

parent: EventDispatcher

The parent EventDispatcher instance. If this instance has no parent, this value will be set to null. The parent is used in the bubbling and capturing phases of events.

see

dispatchEvent for more information on the bubbling and capturing chain

scrollSize

scrollSize: number = 0

Private targetAxis

targetAxis: Axis

trackingPoints

trackingPoints: Array<ScrollTrackerPoint> = []

viewEnd

viewEnd: number = 0

viewSize

viewSize: number = 0

viewStart

viewStart: number = 0

Static Private _DEFAULT_THROTTLE_RESIZE

_DEFAULT_THROTTLE_RESIZE: number = 200

Static Private _DEFAULT_THROTTLE_SCROLL

_DEFAULT_THROTTLE_SCROLL: number = 1000 / 60

Accessors

axis

targetElement

  • get targetElement(): HTMLElement | Window
  • Returns the target element this ScrollTracker instance is tracking.

    Returns HTMLElement | Window

Methods

addEventListener

  • addEventListener(eventType: string, handler: EventHandler, useCapture?: boolean, priority?: number): EventListenerData
  • Adds a new event listener. The given handler function will be called in the following cases:

    • An event with a [[IEvent.type|type]] that is equal to the given eventType is dispatched on this EventDispatcher instance.
    • An event with a [[IEvent.type|type]] that is equal to the given eventType is dispatched on a child EventDispatcher, and the useCapture parameter is set to true
    • An event with [[IEvent.bubbles|bubbles]] set to true and a [[IEvent.type|type]] that is equal to the given eventType is dispatched on a child EventDispatcher, and the useCapture parameter is set to false
    see

    dispatchEvent for more info on the which event listeners are called during capturing and bubbling

    Parameters

    • eventType: string

      The eventType to listen for

    • handler: EventHandler

      The handler function that will be called when a matching event is dispatched. This function will retrieve the dispatched [[IEvent|event]] as a parameter

    • Optional useCapture: boolean

      Indicates if this handler should be called during the capturing phase of an event chain. If and only if this is set to false will this handler be called during the bubbling phase of an event chain.

    • Optional priority: number

      A number that indicates the priority of this event listener relative to other event listeners of the same type on this EventDispatcher instance. A higher number indicates that this listener will be called earlier.

    Returns EventListenerData

    An object describing the listener that has a [[EventListenerData.dispose|dispose()]] method to remove the listener.

addPoint

  • Adds a new point of which we will detect when it enters and leaves the view.

    Parameters

    • position: number

      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.

    • Default value height: number = 1
    • Default value side: Side = Side.START

      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.

dispatchEvent

  • dispatchEvent(event: IEvent): boolean
  • Dispatches the given event. The dispatch consists of three phases:

    1. The capture phase. We walk through all ancestors of this EventDispatcher, with the top-most instance first and the direct parent of this EventDispatcher last. On each ancestor, we call all event handlers that are added with the useCapture argument set to true and the eventType set to the same [[IEvent.type|type]] as the given event. If this EventDispatcher has no parent, this phase will be skipped.
    2. The target phase. In this phase we call all event handlers on this EventDispatcher instance that listen for the same [[IEvent.type|type]] as the given event.
    3. The bubbling phase. This phase will only be executed if the given event has the [[IEvent.bubbles|bubbles]] property set to true. If so, we will again walk through all ancestors of this EventDispatcher, but in the reverse order: the direct parent of this instance first and the top-most parent last. On every ancestor, we will call all event handlers that are added with the useCapture argument set to false and the eventType set to the same [[IEvent.type|type]] as the given event.

    If any of the event handlers call [[IEvent.stopPropagation|stopPropagation()]], we will skip all event handlers that occur on a target later in the event chain. If an event handler calls [[IEvent.stopImmediatePropagation|stopImmediatePropagation()]], we will also skip any event handlers on the same target in the event chain.

    Parameters

    • event: IEvent

      The event to dispatch

    Returns boolean

    If one of the handlers that have been called during this dispatch called [[IEvent.preventDefault|event.preventDefault()]], this method will return false. If no handlers have been called or none of the handlers have called [[IEvent.preventDefault|event.preventDefault()]], this method will return true.

    Please note: [[IEvent.preventDefault|preventDefault()]] can only be called on events that have their [[IEvent.cancelable|cancelable]] property set to true

dispose

  • dispose(): void
  • Disposes this ScrollTracker and all points created on it. Removes all event handlers.

    Returns void

hasEventListener

  • hasEventListener(eventType: string, handler?: EventHandler, useCapture?: boolean): boolean
  • Checks if an event listener matching the given parameters exists on this EventDispatcher instance.

    Parameters

    • eventType: string

      Will only look for event listeners with this eventType

    • Optional handler: EventHandler

      If set, will only match event listeners that have the same handler function

    • Optional useCapture: boolean

      If set, will only match event listeners that have the same useCapture argument. Please note: if no useCapture argument was provided to addEventListener, it is set to false by default

    Returns boolean

    True if one or more event listeners exist

Private initEvents

  • initEvents(): 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.

    Returns void

isDisposed

  • isDisposed(): boolean
  • After dispose has been called, this method returns true. Use this method to determine whether dispose() should be run again.

    Returns boolean

Private pointEventHandler

  • Handles events thrown by ScrollTrackerPoint instances and bubbles them up to this ScrollTracker instance.

    Parameters

    Returns void

removeAllEventListeners

  • removeAllEventListeners(eventType?: string): void
  • Removes all event listeners that have a [[IEvent.type|type]] of the given eventType from this EventDispatcher instance, regardless of their [[EventListenerData.handler|handler]] or [[EventListenerData.useCapture|useCapture]] property.

    Please note: if you remove an event listener during the dispatch of an event it will not be called anymore, even if it was supposed to be called in the same event chain

    Parameters

    • Optional eventType: string

      The [[IEvent.type|type]] of event to remove. If not provided, all event listeners will be removed regardless of their type.

    Returns void

removeAllPoints

  • removeAllPoints(): void
  • Removes all points from this ScrollTracker instance. They will be destructed and will no longer throw events.

    Returns void

removeEventListener

  • removeEventListener(eventType: string, handler: EventHandler, useCapture?: boolean): void
  • Removes all event listeners that match the given parameters from this EventDispatcher instance.

    Please note: if you remove an event listener during the dispatch of an event it will not be called anymore, even if it was supposed to be called in the same event chain

    Parameters

    • eventType: string

      Only event listeners of that have this eventType are removed

    • handler: EventHandler

      Only event listeners that have this handler function will be removed

    • Optional useCapture: boolean

      Only event listeners that have been added with the same useCapture parameter will be removed. Please note: if no useCapture argument is provided, only event listeners that have useCapture set to false will be removed.

    Returns void

removePoint

  • Removes an existing point from this ScrollTracker. This point will be destructed and will no longer throw events.

    Parameters

    Returns boolean

    Boolean indicating if the point was found and removed successfully.

Private scrollHandler

  • scrollHandler(): void
  • Event handler called when the target element is scrolled. Will detect the new scroll position and call checkInView() on all tracking points.

    Returns void

updateSize

  • updateSize(): void

willTrigger

  • willTrigger(eventType: string): boolean
  • Checks if an event listener with a [[EventListenerData.type|type]] of the given eventType exists on this EventDispatcher or any ancestor EventDispatcher instance.

    Parameters

    • eventType: string

      The event type to check for

    Returns boolean

    true if a matching listener is found

Private windowResizeHandler

  • windowResizeHandler(): void
  • Event handler called when the window resizes. Only used when the target of this ScrollTracker instance is the window object.

    Returns void

Generated using TypeDoc