Skip to main content
Svelte基础
介绍
响应
属性Props
逻辑表达式
事件
绑定
Classes和样式
动作Actions
转场
Svelte高阶
响应式进阶
内容复用
动画
高级绑定
高级转场
上下文API
特殊元素
脚本模块
接下来
SvelteKit基础
介绍
路由
加载数据
Headers和cookies
Shared modules
Forms
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Hooks
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion

前面已经介绍过<svelte:window><svelte:document>了,类似得还有<svelte:body>, 像先前提到的尽量用它绑定mouseentermouseleave事件处理函数:

Similar to <svelte:window> and <svelte:document>, the <svelte:body> element allows you to listen for events that fire on document.body. This is useful with the mouseenter and mouseleave events, which don’t fire on window. Add onmouseenter and onmouseleave handlers to the <svelte:body> tag...

App
<svelte:body
	onmouseenter={() => hereKitty = true}
	onmouseleave={() => hereKitty = false}
/>

小主人可以划拉下鼠标看下效果了。

...and hover over the <body>.

Edit this page on GitHub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<script>
	import kitten from './kitten.png';
 
	let hereKitty = $state(false);
</script>
 
<svelte:body />
 
<!-- creative commons BY-NC http://www.pngall.com/kitten-png/download/7247 -->
<img
	class={{ curious: hereKitty }}
	alt="Kitten wants to know what's going on"
	src={kitten}
/>
 
<style>
	img {
		position: absolute;
		left: 0;
		bottom: -60px;
		transform: translate(-80%, 0) rotate(-15deg);
		transform-origin: 100% 100%;
		transition: transform 0.4s;
	}
 
	.curious {
		transform: translate(-15%, 0) rotate(0deg);
	}
 
	:global(body) {
		overflow: hidden;
	}
</style>