Created
January 12, 2016 09:02
-
-
Save etaque/b49980ba90f93f790859 to your computer and use it in GitHub Desktop.
Mouse wheel with Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onMouseWheel : Address a -> (Float -> a) -> Attribute | |
onMouseWheel address toAction = | |
let | |
handler v = Signal.message address (toAction v) | |
in | |
onWithOptions "wheel" defensiveOptions decodeWheelEvent handler | |
defensiveOptions : Options | |
defensiveOptions = | |
{ stopPropagation = True | |
, preventDefault = True | |
} | |
decodeWheelEvent : Json.Decoder Float | |
decodeWheelEvent = | |
oneOf | |
[ at [ "deltaY" ] float | |
, at [ "wheelDelta" ] float |> map (\v -> -v) | |
] | |
`andThen` (\v -> if v /= 0 then succeed v else fail "Wheel of 0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment