rl-king/elm-inview/1.1.0

NOTE: the latest version is 2.0.1

InView

Detect

Detect if an element is visible in the current viewport.

check : String -> State -> Maybe Bool

True if the element with the given id is in the current viewport.

note: this is a Maybe because the element might not be on the page at all.

check

checkWithOffset : String -> Float -> Float -> State -> Maybe Bool

True if the element with the given id is in the current viewport but with an x and y offset. A positive offset will make the viewport smaller and vice versa.

note: this is a Maybe because the element might not be on the page at all.

checkWithOffset

checkAlt : String -> State -> Maybe Bool

True if the element with the given id is in or above the current viewport.

note: this is a Maybe because the element might not be on the page at all.

checkAlt

checkAltWithOffset : String -> Float -> Float -> State -> Maybe Bool

True if the element with the given id is in or above the current viewport but with an x and y offset. A positive offset will make the viewport smaller and vice versa.

note: this is a Maybe because the element might not be on the page at all.

checkAltWithOffset

Detect Custom

checkCustom : (Viewport -> Element -> Bool) -> String -> State -> Maybe Bool

Write your own check function.

For example checkAltWithOffset is implemented like:

checkAltWithOffset : String -> Float -> Float -> State -> Maybe Bool
checkAltWithOffset id offsetX offsetY state =
    let
        calc { viewport } { element } =
            (viewport.y - offsetY + viewport.height > element.y)
                && viewport.x - offsetX + viewport.width > element.x)
    in
    checkCustom calc id state

note: this is a Maybe because the element might not be on the page at all.

Definitions

type State

Keeps track of viewport position, viewport dimensions and element positions.

type Msg

A message type for the state to update.

Init

init : List String -> ( State, Cmd Msg )

Takes the list of element ids you want to keep track of and attempts to find them in the DOM.

Update

update : Msg -> State -> ( State, Cmd Msg )

Update viewport size and element positions.

updateViewportOffset : Float -> Float -> State -> State

Update current viewport x and y offset. Use this function to update the viewport scroll position.

Subscribes to browser resize events and recalculates element positions.

addElements : List String -> Cmd Msg

Add elements you'd like to be able to detect after you've initialized the state.