vue實現(xiàn)拖拽窗口功能
更新時間:2022年04月08日 13:33:40 作者:橡皮擦不掉的
這篇文章主要為大家詳細介紹了vue實現(xiàn)拖拽窗口功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue拖拽窗口簡單實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
效果
實現(xiàn)代碼
<template> ? <transition> ? ? <!-- ? ?綁定style中top和left--> ? ? <div class="moveBox" :style="position" v-show="show"> ? ? ? <div ? ? ? ? class="moveHead" ? ? ? ? @mousedown="mousedown" ? ? ? ? @mousemove="mousemove" ? ? ? ? @mouseup="mouseup" ? ? ? ? @mouseleave="mousemove" ? ? ? ></div> ? ? ? <!-- ? ? ?關(guān)閉按鈕--> ? ? ? <div class="close" @click="toggleShow">×</div> ? ? ? <div class="content"> ? ? ? ? <!--插槽--> ? ? ? ? <slot></slot> ? ? ? </div> ? ? </div> ? </transition> </template> <script> export default { ? name: "moveBox", ? data() { ? ? return { ? ? ? show: true,//是否顯示 ? ? ? x: 100,//left:x ? ? ? y: 50,//top:y ? ? ? leftOffset: 0,//鼠標(biāo)距離移動窗口左側(cè)偏移量 ? ? ? topOffset: 0,//鼠標(biāo)距離移動窗口頂部偏移量 ? ? ? isMove: false,//是否移動標(biāo)識 ? ? }; ? }, ? computed: { ? ? //top與left加上px ? ? position() { ? ? ? return `top:${this.y}px;left:${this.x}px;`; ? ? }, ? }, ? methods: { ? ? //控制打開關(guān)閉 ? ? toggleShow() { ? ? ? this.x = 100; ? ? ? this.y = 50; ? ? ? this.show = !this.show; ? ? }, ? ? mousedown(event) { ? ? ? //鼠標(biāo)按下事件 ? ? ? this.leftOffset = event.offsetX; ? ? ? this.topOffset = event.offsetY; ? ? ? this.isMove = true; ? ? }, ? ? //鼠標(biāo)移動 ? ? mousemove(event) { ? ? ? if (!this.isMove) { ? ? ? ? return; ? ? ? } ? ? ? this.x = event.clientX - this.leftOffset; ? ? ? this.y = event.clientY - this.topOffset; ? ? }, ? ? //鼠標(biāo)抬起 ? ? mouseup() { ? ? ? this.isMove = false; ? ? }, ? }, }; </script> <style lang="less" scoped> .moveBox { ? width: 500px; ? height: 300px; ? position: fixed; ? box-shadow: 0 0 5px 3px #95a5a6; ? .moveHead { ? ? height: 30px; ? ? background-color: #bdc3c7; ? ? cursor: move; ? } ? .close { ? ? position: absolute; ? ? right: 0; ? ? top: 0; ? ? line-height: 30px; ? ? font-size: 24px; ? ? cursor: pointer; ? ? display: block; ? ? width: 30px; ? ? height: 30px; ? ? text-align: center; ? } } .v-enter, .v-leave-to { ? opacity: 0; ? transform: scale(0.5); } .content { ? padding: 10px; } .v-enter-active, .v-leave-active { ? transition: all 0.5s ease; } </style>
使用
<template> ? <div class="home"> ? ? <button @click="$refs.moveBox.toggleShow()">toggle moveBox</button> ? ? <move-box ref="moveBox"> ? ? ? Hello World ? ? </move-box> ? </div> </template>
代碼沒什么難度,主要是使用了定位,在鼠標(biāo)移動事件中定義top和left值。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中render函數(shù)和h函數(shù)以及jsx的使用方式
這篇文章主要介紹了vue中render函數(shù)和h函數(shù)以及jsx的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue3?setup的注意點及watch監(jiān)視屬性的六種情況分析
這篇文章主要介紹了Vue3?setup的注意點及watch監(jiān)視屬性的六種情況,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04