vue實(shí)現(xiàn)拖拽窗口功能
本文實(shí)例為大家分享了vue拖拽窗口簡(jiǎn)單實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
效果

實(shí)現(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)距離移動(dòng)窗口左側(cè)偏移量
? ? ? topOffset: 0,//鼠標(biāo)距離移動(dòng)窗口頂部偏移量
? ? ? isMove: false,//是否移動(dòng)標(biāo)識(shí)
? ? };
? },
? computed: {
? ? //top與left加上px
? ? position() {
? ? ? return `top:${this.y}px;left:${this.x}px;`;
? ? },
? },
? methods: {
? ? //控制打開(kāi)關(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)移動(dòng)
? ? 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>
代碼沒(méi)什么難度,主要是使用了定位,在鼠標(biāo)移動(dòng)事件中定義top和left值。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue自定義指令實(shí)現(xiàn)彈窗拖拽四邊拉伸及對(duì)角線拉伸效果
- VUE實(shí)現(xiàn)Studio管理后臺(tái)之鼠標(biāo)拖放改變窗口大小
- VUE實(shí)現(xiàn)可隨意拖動(dòng)的彈窗組件
- vue懸浮可拖拽懸浮按鈕的實(shí)例代碼
- Vue 實(shí)現(xiàn)可視化拖拽頁(yè)面編輯器
- 基于Vue實(shí)現(xiàn)拖拽功能
- 基于Vue實(shí)現(xiàn)拖拽效果
- vue實(shí)現(xiàn)div拖拽互換位置
- vue實(shí)現(xiàn)拖拽交換位置
- Vue draggable實(shí)現(xiàn)從左到右拖拽功能
相關(guān)文章
vue通過(guò)接口直接下載java生成好的Excel表格案例
這篇文章主要介紹了vue通過(guò)接口直接下載java生成好的Excel表格案例2020-10-10
vue實(shí)現(xiàn)模態(tài)框的通用寫(xiě)法推薦
下面小編就為大家分享一篇vue實(shí)現(xiàn)模態(tài)框的通用寫(xiě)法推薦,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue中render函數(shù)和h函數(shù)以及jsx的使用方式
這篇文章主要介紹了vue中render函數(shù)和h函數(shù)以及jsx的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vite+Vue3使用MockJS的實(shí)現(xiàn)示例
寫(xiě)一些純前端的項(xiàng)目時(shí),自己造數(shù)據(jù)有些麻煩,于是我們可以利用mock造一些簡(jiǎn)單的數(shù)據(jù),來(lái)滿足我們的需求,本文主要介紹了Vite+Vue3使用MockJS的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01
Vue3?setup的注意點(diǎn)及watch監(jiān)視屬性的六種情況分析
這篇文章主要介紹了Vue3?setup的注意點(diǎn)及watch監(jiān)視屬性的六種情況,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

