From 43efab2ed4270c02d38293949cd7c03e16f4fe67 Mon Sep 17 00:00:00 2001 From: Berend de Boer Date: Thu, 26 Sep 2019 20:19:40 +1200 Subject: [PATCH] Add support for onScroll --- src/Browser/Events.elm | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Browser/Events.elm b/src/Browser/Events.elm index 178240a..5c0b037 100644 --- a/src/Browser/Events.elm +++ b/src/Browser/Events.elm @@ -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(..) ) @@ -25,7 +25,7 @@ If there is something else you need, use [ports][] to do it in JavaScript! @docs onClick, onMouseMove, onMouseDown, onMouseUp # Window -@docs onResize, onVisibilityChange, Visibility +@docs onResize, onScroll, onVisibilityChange, Visibility -} @@ -184,6 +184,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: