Skip to content

Instantly share code, notes, and snippets.

@etaque
Created January 12, 2016 09:02
Show Gist options
  • Save etaque/b49980ba90f93f790859 to your computer and use it in GitHub Desktop.
Save etaque/b49980ba90f93f790859 to your computer and use it in GitHub Desktop.
Mouse wheel with Elm
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