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,//鼠標距離移動窗口左側(cè)偏移量
? ? ? topOffset: 0,//鼠標距離移動窗口頂部偏移量
? ? ? isMove: false,//是否移動標識
? ? };
? },
? 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) {
? ? ? //鼠標按下事件
? ? ? this.leftOffset = event.offsetX;
? ? ? this.topOffset = event.offsetY;
? ? ? this.isMove = true;
? ? },
? ? //鼠標移動
? ? mousemove(event) {
? ? ? if (!this.isMove) {
? ? ? ? return;
? ? ? }
? ? ? this.x = event.clientX - this.leftOffset;
? ? ? this.y = event.clientY - this.topOffset;
? ? },
? ? //鼠標抬起
? ? 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>
代碼沒什么難度,主要是使用了定位,在鼠標移動事件中定義top和left值。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
elementUI select組件默認選中效果實現(xiàn)的方法
這篇文章主要介紹了elementUI select組件默認選中效果實現(xiàn)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Spring boot 和Vue開發(fā)中CORS跨域問題解決
這篇文章主要介紹了Spring boot 和Vue開發(fā)中CORS跨域問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
使用element-ui table expand展開行實現(xiàn)手風(fēng)琴效果
這篇文章主要介紹了使用element-ui table expand展開行實現(xiàn)手風(fēng)琴效果,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn)
這篇文章主要介紹了element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue3.5中響應(yīng)式Props解構(gòu)的編譯原理
在Vue3.5版本中,響應(yīng)式Props的解構(gòu)功能正式轉(zhuǎn)正,該功能允許即使在解構(gòu)后也不丟失響應(yīng)性,文通過編譯階段的處理,如何保持解構(gòu)后的props變量仍保持響應(yīng)性,編譯過程中的defineProps宏函數(shù)處理,通過AST和上下文操作實現(xiàn)變量替換,從而讓解構(gòu)后的變量在運行時維持響應(yīng)式狀態(tài)2024-09-09

