Skip to content

Add support for onScroll #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Browser/Events.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ effect module Browser.Events where { subscription = MySub } exposing
( onAnimationFrame, onAnimationFrameDelta
, onKeyPress, onKeyDown, onKeyUp
, onClick, onMouseMove, onMouseDown, onMouseUp
, onResize, onVisibilityChange, Visibility(..)
, onResize, onScroll, onVisibilityChange, Visibility(..)
)

{-| In JavaScript, information about the root of an HTML document is held in
Expand Down Expand Up @@ -31,8 +31,7 @@ If there is something else you need, use [ports] to do it in JavaScript!


# Window

@docs onResize, onVisibilityChange, Visibility
@docs onResize, onScroll, onVisibilityChange, Visibility

-}

Expand Down Expand Up @@ -208,6 +207,21 @@ onResize func =
(Decode.field "innerHeight" Decode.int)


{-| Subscribe to scrolling of the document.

**Note:** This is equivalent to getting events from [`document.onscroll`][scroll].

[scroll]: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onscroll
-}
onScroll : (Float -> Float -> msg) -> Sub msg
onScroll func =
on Window "scroll" <|
Decode.at [ "target", "defaultView" ] <|
Decode.map2 func
(Decode.field "scrollX" Decode.float)
(Decode.field "scrollY" Decode.float)


{-| Subscribe to any visibility changes, like if the user switches to a
different tab or window. When the user looks away, you may want to:

Expand Down