An element pulled toward the pointer when it comes close.
Installation
Usage
<script setup lang="ts">
import { Magnetic } from '@animate-ui/vue/primitives/effects/magnetic';
</script>
<template>
<Magnetic :strength="0.4" :range="160">
<button>Come closer</button>
</Magnetic>
</template>Animation
- Within
rangepixels of its center, the element moves toward the pointer: the closer, the stronger the pull (strength). The distance is measured from where the element currently is, pull included. It springs back when the pointer leaves the range. - The pull follows the pointer on a light, bouncy spring (stiffness 100, damping 10, mass 0.5), so it's interruptible and never jumps. It moves with
x/y, a hardware-accelerated transform. - The pointer is only tracked while the element is on screen. With
only-on-hover, only while it's hovered. - Off on touch screens (
disable-on-touch), where there is no pointer to follow. <AnimateUIProvider :tempo>paces the default spring.- Under reduced motion, the element stays in place.
API Reference
Magnetic
| Prop | Type | Default |
|---|---|---|
as-child? | boolean | false |
strength? | number | 0.5 |
range? | number | 120 |
spring-options? | SpringOptions | { stiffness: 100, damping: 10, mass: 0.5 } |
only-on-hover? | boolean | false |
disable-on-touch? | boolean | true |
...props? | HTMLMotionProps<'div'> | - |