hover
Motion's hover
function detects hover gestures, firing events when they start and end.
For legacy reasons, browsers emulate hover events from touch devices, which can lead to "stuck" UIs and other unwanted visual artefacts/broken behaviours. hover
filters these fake events out.
hover
is also:
Clean: Automatically manages event listeners
Convenient: Accepts either elements or CSS selectors for attaching multiple gestures at once
Lazy: Attaches only the event listeners needed
hover
callbacks can do anything, but often they're used to start or control animations.
Usage
Import
hover
can be imported into your project via "motion"
:
Hover start
hover
can detect hover gestures on either an Element
/array of elements:
Or via a CSS selector:
When a hover gesture starts, the provided callback is provided both the element that's being hovered, and the triggering PointerEvent
:
Hover end
The hover start function can optionally return a callback. This will be called when the hover gesture ends:
This callback will be provided the triggering PointerEvent
.
Cancelling gesture detection
hover
returns a function that, when fired, will cancel all active event handlers associated with the gesture.
Options
passive
Default: true
If set to false
, it'll be possible to call event.preventDefault()
but the gesture will be less performant. Learn more about passive events.
once
Default: false
If set to true
, each provided element will fire their gesture only once.