<scriptsetuplang="ts">import{ref}from'vue'import{useDraggable}from'@vueuse/core'constel=ref<HTMLElement |null>(null)// `style` will be a helper computed for `left: ?px; top: ?px;`const{x,y,style}=useDraggable(el,{initialValue:{x:40,y:40},})</script><template><divref="el":style="style"style="position: fixed"> Drag me! I am at {{x}}, {{y}}</div></template>
该函数还通过@vueuse/components提供了一个无渲染的组件版本。
html
<UseDraggable:initialValue="{ x: 10, y: 10 }"v-slot="{ x, y }"> Drag me! I am at {{x}}, {{y}}</UseDraggable><UseDraggablestorage-key="vueuse-draggable"storage-type="session"> Refresh the page and I am still in the same position!</UseDraggable>
<scriptsetuplang="ts">import{useDropZone}from'@vueuse/core'constdropZoneRef=ref<HTMLDivElement>()functiononDrop(files: File[] |null){// called when files are dropped on zone}const{isOverDropZone}=useDropZone(dropZoneRef,onDrop)</script><template><divref="dropZoneRef"> Drop files here</div></template>