add vitepress doc
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<template>
|
||||
<div class="demo">
|
||||
<p class="size" v-text="`${width} × ${height}`" />
|
||||
<p class="note" v-text="`回调已触发 ${count} 次(含注册时的立即触发)`" />
|
||||
<div class="bar">
|
||||
<button
|
||||
type="button"
|
||||
class="btn"
|
||||
@click="toggle"
|
||||
v-text="registered ? '移除监听' : '注册监听'"
|
||||
/>
|
||||
<button type="button" class="btn" @click="read" v-text="'手动读取当前尺寸'" />
|
||||
</div>
|
||||
<p class="note" v-text="'缩放浏览器窗口即可看到数字变化。'" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { onMounted, onUnmounted, ref } from "vue";
|
||||
import { Resizer } from "timi-web";
|
||||
|
||||
/** 监听名在内部用于去重,同名会覆盖。 */
|
||||
const LISTENER_NAME = "TimiDocsResizerDemo";
|
||||
|
||||
const width = ref(0);
|
||||
const height = ref(0);
|
||||
const count = ref(0);
|
||||
const registered = ref(false);
|
||||
|
||||
function onChange(nextWidth: number, nextHeight: number) {
|
||||
width.value = nextWidth;
|
||||
height.value = nextHeight;
|
||||
count.value += 1;
|
||||
}
|
||||
|
||||
function register() {
|
||||
// 注册后会立即触发一次,因此不需要再手动取一次尺寸
|
||||
Resizer.addListener(LISTENER_NAME, onChange);
|
||||
registered.value = true;
|
||||
}
|
||||
|
||||
function unregister() {
|
||||
Resizer.removeListener(LISTENER_NAME);
|
||||
registered.value = false;
|
||||
}
|
||||
|
||||
function toggle() {
|
||||
if (registered.value) {
|
||||
unregister();
|
||||
} else {
|
||||
register();
|
||||
}
|
||||
}
|
||||
|
||||
/** getWidth / getHeight 直接读文档可视区,与监听无关。 */
|
||||
function read() {
|
||||
width.value = Resizer.getWidth();
|
||||
height.value = Resizer.getHeight();
|
||||
}
|
||||
|
||||
onMounted(register);
|
||||
|
||||
// 演示组件卸载时务必移除,避免监听残留
|
||||
onUnmounted(unregister);
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.demo {
|
||||
.size {
|
||||
margin: 0 0 .25rem;
|
||||
font-size: 1.5rem;
|
||||
font-weight: 600;
|
||||
font-variant-numeric: tabular-nums;
|
||||
color: var(--vp-c-brand-1);
|
||||
}
|
||||
|
||||
.note {
|
||||
margin: 0 0 .75rem;
|
||||
font-size: .75rem;
|
||||
color: var(--vp-c-text-2);
|
||||
}
|
||||
|
||||
.bar {
|
||||
display: flex;
|
||||
gap: .5rem;
|
||||
margin-bottom: .75rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: .25rem .75rem;
|
||||
font-size: .75rem;
|
||||
cursor: pointer;
|
||||
border: 1px solid var(--vp-c-divider);
|
||||
border-radius: .375rem;
|
||||
color: var(--vp-c-text-1);
|
||||
background: var(--vp-c-bg-soft);
|
||||
|
||||
&:hover {
|
||||
color: var(--vp-c-brand-1);
|
||||
border-color: var(--vp-c-brand-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user