Skip to main content

Part 1 / Events / DOM events

As we've briefly seen already, you can listen to any event on an element with the on: directive:

<div on:mousemove={handleMousemove}>
	The mouse position is {m.x} x {m.y}
</div>

Next: Inline handlers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script>
	let m = { x: 0, y: 0 };
 
	function handleMousemove(event) {
		m.x = event.clientX;
		m.y = event.clientY;
	}
</script>
 
<div>
	The mouse position is {m.x} x {m.y}
</div>
 
<style>
	div {
		width: 100%;
		height: 100%;
	}
</style>
 
initialising