GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • DocsDocs
  • ExamplesExamples
  • AI KitAIAI KitAI
  • React
  • JS
  • Vue
  • AI
  • animate
  • Timeline sequences
  • animateLayout
  • animateView
  • scroll
  • Easing
  • drag

This view

  • Original page
  • New address

GeezerVison is showing motion.dev — hover() | Hover-triggered animations | Motion

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(".button", (element) => {
  console.log("hover started on", element)

  return () => console.log("hover end")
})

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.

hover("li", (element) => {
  const animation = animate(element, { rotate: 360 })

  return () => animation.stop()
})
HoverOpen example
Black Friday

Motion+ is 20% off until 30 Nov.

One payment, lifetime updates. Get every premium example, Motion UI and AI Kit for your coding agent.

Get 20% offGet 20% off

Usage

Import

hover can be imported into your project via "motion":

import { hover } from "motion"

Hover start

hover can detect hover gestures on either an Element/array of elements:

hover(
  document.getElementById("my-id"),
  () => {
    console.log("my-id hovered!")
  }
)

Or via a CSS selector:

hover("a", () => console.log("link hovered"))

When a hover gesture starts, the provided callback is provided both the element that's being hovered, and the triggering PointerEvent:

hover("div:nth-child(2)", (element, startEvent) => {
  console.log("Hover started on", element)
  console.log("At", startEvent.clientX, startEvent.clientY)
})

Hover end

The hover start function can optionally return a callback. This will be called when the hover gesture ends:

hover("a", () => {
  console.log("hover start")
  
  return (endEvent) => {
    console.log("hover end")
  }
})

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.

const cancelHover = hover(element, callback)

cancelHover()

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.

Motion+One payment
Lifetime updates
Professional animation toolkit

Animate faster with Motion+.

Unlock 550+ premium examples, Motion UI, AI Kit and private community access.

Examples
550+
Tutorials
110+
AI Kit
Included
Explore Motion+Explore Motion+
  1. 01press

    press() detects press gestures, including double press, long press and press to repeat, with keyboard support and pointer filtering built in.

  2. 02inView

    inView() runs callbacks when elements enter and leave the viewport, built on IntersectionObserver.

  3. 03animate

    animate() runs easing and spring animations, hardware-accelerated where supported.

  • Hover animation exampleHover

Motion is supported by

Motion Cloud members who help fund Motion’s open-source development.

Become a supporter
  • electrinity
  • Sean Bugeja
  • AAbhishek
  • AAriel Rodribuez
  • Meepo
  • BBeto
  • SStanislav Shilin
  • AAriel Conti
  • Joshosullivam
  • DDamián Navarro
  • nimit
  • CCNTNAI
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.