Skip to main content
Similarly to <svelte:window>, this element allows you to add listeners to events on document.body, such as mouseenter and mouseleave, which don’t fire on window. It also lets you use actions on the <body> element.
As with <svelte:window> and <svelte:document>, this element may only appear at the top level of your component and must never be inside a block or element.

Event Listeners

You can add any valid DOM event listener that works on the body element:

Common body events

  • onmouseenter - Mouse enters the body
  • onmouseleave - Mouse leaves the body
  • onmousemove - Mouse moves over body
  • onclick - Click on body
  • ontouchstart - Touch starts on body
  • ontouchend - Touch ends on body

Actions

You can use Svelte actions on the body element:

Usage

Detect mouse leaving window

Track global mouse position

Using multiple actions

Click outside detection

Combine events and actions