-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] 为
useDraggableDistance
添加一个完整示例
- Loading branch information
1 parent
5cfdb38
commit c84ac8f
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/use/src/useDraggableDistance/demo/模拟拖拽移动滚动条.preview.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<template> | ||
<div | ||
ref="rootRef" | ||
w-full h-40vmin b="1 solid neutral-3 dark:neutral-5" el="6 op-60" overflow-scroll | ||
> | ||
<div class="size-600%"> | ||
<n-watermark class="size-full" content="在容器内拖拽试试" :width="200" :height="100" :x-offset="60" :y-offset="30" :rotate="15" /> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { useDraggableDistance, watchImmediate, wheneverEffectScopeImmediate, wheneverImmediate } from '@mixte/use'; | ||
import { useScroll } from '@vueuse/core'; | ||
import { reactive, ref } from 'vue'; | ||
const rootRef = ref<HTMLElement>(); | ||
const { x, y, isDragging } = useDraggableDistance(rootRef); | ||
// 防止拖拽时选中文本 ( 按实际情况使用 ) | ||
wheneverImmediate(isDragging, (_, __, onCleanup) => { | ||
document.body.style.userSelect = 'none'; | ||
onCleanup(() => { | ||
document.body.style.userSelect = ''; | ||
}); | ||
}); | ||
// 拖拽后, 将本次拖拽距离加到滚动条上 | ||
wheneverEffectScopeImmediate(isDragging, () => { | ||
const scroll = reactive(useScroll(rootRef)); | ||
scroll.measure(); | ||
const startX = scroll.x; | ||
const startY = scroll.y; | ||
watchImmediate([x, y], () => { | ||
scroll.x = startX - x.value; | ||
scroll.measure(); | ||
scroll.y = startY - y.value; | ||
}); | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters