Svelte Element Bindings
Element bindings give your script a direct line to the page. You can grab a reference to an element, read its size, or control a video player. Svelte keeps these connections current without manual event listeners.
Grabbing An Element With bind:this
The bind this directive stores the real DOM element in a variable. The variable stays undefined until the component mounts, so use it inside onMount or an event handler.
<script>
import { onMount } from "svelte";
let searchBox;
onMount(() => {
searchBox.focus();
});
</script>
<input bind:this={searchBox} placeholder="Type to search" />The input receives focus as soon as the page appears. Visitors can start typing immediately.
Reference Diagram
Component starts
|
searchBox = undefined (element does not exist yet)
|
v
Svelte builds the page
|
v
searchBox = <input> element (bind:this fills it)
|
v
onMount runs and calls searchBox.focus()Reading Element Size
Dimension bindings report the size of an element. They are read-only, so Svelte updates your variable when the element changes, and your code cannot resize the element by changing the variable.
<script>
let boxWidth;
let boxHeight;
</script>
<div bind:clientWidth={boxWidth} bind:clientHeight={boxHeight}>
Resize the browser to change my size.
</div>
<p>Box size: {boxWidth} x {boxHeight}</p>Available dimension bindings include clientWidth, clientHeight, offsetWidth, and offsetHeight. Svelte watches the element with a resize observer, which stays efficient.
Controlling Media Players
Audio and video elements expose many bindings. Some are read-only, such as duration, and some accept changes, such as paused and currentTime.
<script>
let paused = true;
let currentTime = 0;
let duration = 0;
</script>
<video
src="/media/intro.mp4"
bind:paused
bind:currentTime
bind:duration
></video>
<button on:click={() => (paused = !paused)}>
{paused ? "Play" : "Pause"}
</button>
<progress value={currentTime} max={duration || 1}></progress>The button flips the paused variable, and the video obeys. The progress bar follows the playback position.
Binding Summary
Binding Direction Works On
---------------- ------------------- ---------------
this Element to variable Any element
clientWidth Element to variable Any block element
paused Both ways audio, video
currentTime Both ways audio, video
duration Element to variable audio, video
scrollY Both ways svelte:windowDrawing On A Canvas
A canvas needs its element reference before drawing starts. The bind this directive supplies it.
<script>
import { onMount } from "svelte";
let canvas;
onMount(() => {
const pen = canvas.getContext("2d");
pen.fillStyle = "teal";
pen.fillRect(10, 10, 120, 60);
});
</script>
<canvas bind:this={canvas} width="200" height="100"></canvas>A Layman Analogy
Think of a remote control and a television. The bind this directive hands you the remote for one specific television. Dimension bindings work like a window on the television that shows its screen size. Media bindings work like volume and pause buttons that control the picture.
Common Mistakes To Avoid
- Using the bound variable before the component mounts
- Trying to set a read-only dimension by assigning a new number
- Forgetting to give a canvas or video a fallback message for assistive tools
- Binding to elements inside blocks without checking for undefined
Key Points
- bind:this stores a real element after mounting
- Dimension bindings read sizes and stay current
- Media bindings control and report playback
- Always check that the element exists before using it
