Skip to main content

As we’ve briefly seen already, you can listen to any DOM event on an element (such as click or pointermove) with an on<name> function:

App
<div onpointermove={onpointermove}>
	The pointer is at {Math.round(m.x)} x {Math.round(m.y)}
</div>

Like with any other property where the name matches the value, we can use the short form:

App
<div {onpointermove}>
	The pointer is at {Math.round(m.x)} x {Math.round(m.y)}
</div>

Edit this page on GitHub

<script>
let m = $state({ x: 0, y: 0 });

function onpointermove(event) {
m.x = event.clientX;
m.y = event.clientY;
}
</script>

<div>
The pointer is at {Math.round(m.x)} x {Math.round(m.y)}
</div>

<style>
div {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
padding: 1rem;
}
</style>