Docs/Functions/animate

animate

animate starts an animation without rendering a motion.* component. Pass an element, selector, or MotionValue, then provide the target keyframes and optional transition. It returns AnimationPlaybackControls, which can be paused, cancelled, completed, or awaited.

import { animate } from 'motion-solidjs'

const controls = animate(
  '.card',
  { y: -20, opacity: 1 },
  {
    type: 'spring',
    stiffness: 320,
    damping: 18,
  },
)

await controls
controls.cancel()
imperative
animate-fn.tsxOpen ↗
import { animate } from 'motion-solidjs'
import { createSignal } from 'solid-js'

export const meta = {
  slug: 'animate-fn',
  title: 'animate()',
  category: 'animations',
  description: 'The imperative animate() function — drive a DOM element from a click handler.',
  tag: 'imperative',
} as const

export default function AnimateFnExample() {
  const [el, setEl] = createSignal<HTMLDivElement | null>(null)
  const [running, setRunning] = createSignal(false)

  const play = async () => {
    const node = el()
    if (!node || running()) return
    setRunning(true)

    const forward = animate(
      node,
      { x: 180, rotate: 90 },
      { duration: 0.6, ease: [0.22, 1, 0.36, 1] },
    )
    await forward.finished

    const back = animate(node, { x: 0, rotate: 0 }, { type: 'spring', stiffness: 220, damping: 14 })
    await back.finished

    setRunning(false)
  }

  return (
    <div class="flex flex-col items-center gap-4">
      <div class="relative h-20 w-64 rounded-2xl border border-border bg-card/40">
        <div
          ref={setEl}
          class="absolute top-2 left-2 h-16 w-16 rounded-2xl bg-grad-rose shadow-glow"
        />
      </div>
      <button
        onClick={play}
        disabled={running()}
        class="rounded-full border border-border bg-card px-4 py-1.5 text-xs text-fg-muted hover:text-fg disabled:opacity-50"
      >
        {running() ? 'Playing…' : 'Play'}
      </button>
    </div>
  )
}

Signatures

animate(element | selector | motionValue, keyframes, transition?)
animate(from, to, options) // tween between two raw values
animate(generator) // run a custom generator

See also