【前端开发】可拖拽浮窗组件

1. 组件:DragWindow

javascript 复制代码
<template>
  <div id="dragWindow" ref="dragBox" :style="{ width: width, height: height }">
    <div id="dragHead" @mousedown.stop="mouseDownHandler">
      <slot name="title"></slot>
    </div>
    <div id="dragBody">
      <slot name="content"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "DragWindow",
  components: {},
  props: {
    title: {
      type: String,
      default: "",
    },
    width: {
      type: String,
      default: "300px",
    },
    height: {
      type: String,
      default: "100vh",
    },
  },
  data() {
    return {
      // 是否可以移动
      isMove: false,

      // 移动开始位置
      startPosition: {},
      // 元素当前位置
      currentPosition: {},

      // 拖拽元素
      dragHead: null,
    };
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    mouseDownHandler(e) {
      // 获取元素当前位置
      this.currentPosition = {
        x: this.$refs.dragBox.offsetLeft,
        y: this.$refs.dragBox.offsetTop,
      };

      // 获取移动开始位置
      this.startPosition = {
        x: e.clientX,
        y: e.clientY,
      };

      // 设置为true,允许移动
      this.isMove = true;

      this.dragWindow = document.getElementById("dragWindow");

      // 鼠标移动事件
      this.dragWindow.addEventListener("mousemove", this.mouseMoveHandler);
      // 鼠标抬起事件
      this.dragWindow.addEventListener("mouseup", (e) => {
        this.isMove = false;
        this.dragWindow.removeEventListener(
            "mousemove",
            this.mouseMoveHandler
        );
      });
      // 鼠标移出事件
      this.dragWindow.addEventListener("mouseleave", (e) => {
        this.isMove = false;
        this.dragWindow.removeEventListener(
            "mousemove",
            this.mouseMoveHandler
        );
      });
    },

    // 鼠标移动事件
    mouseMoveHandler(e) {
      if (!this.isMove) {
        return;
      }

      // 获取鼠标移动的距离
      const nowX = e.clientX - this.startPosition.x,
          nowY = e.clientY - this.startPosition.y;

      // 计算并设置移动后的位置
      this.$refs.dragBox.style.left = `${this.currentPosition.x + nowX}px`;
      this.$refs.dragBox.style.top = `${this.currentPosition.y + nowY}px`;
    },

    // 关闭窗口
    close() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
#dragWindow {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 999;
  transform: translate(-50%, -50%);
  background-color: #030e06;
  color: #fff;
  border-radius: 10px;

  #dragHead {
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: rgba(#34a83a, 1);
    padding: 0px 10px 0px 20px;
    box-sizing: border-box;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    position: relative;

    .dragTitle {
      font-size: 18px;
    }

    .el-icon-close {
      font-size: 18px;
      cursor: pointer;

      &:hover {
        color: #ff6347;
      }
    }
  }

  #dragBody {
    width: 100%;
    height: calc(100% - 50px);
  }
}
</style>

2. main.js全局调用

javascript 复制代码
import DragWindow from './components/DragWindow/index.vue';
javascript 复制代码
    <DragWindow
        ref="DragWindow"
        w="600px"
        h="600px"
        title="可拖拽浮窗"
        @close="close()">
    </DragWindow>
相关推荐
H尗1 分钟前
Vue3响应式系统的精妙设计:深入理解 depsLength 与 trackId 的协同艺术
前端·vue.js
昔人'9 分钟前
html`contenteditable`
前端·html
爱宇阳14 分钟前
npm 常用标签与使用技巧新手教程
前端·npm·node.js
@大迁世界19 分钟前
2025 年该选谁?npm vs yarn vs pnpm
前端·npm·node.js
crary,记忆20 分钟前
简介NPM 和 NPX
前端·学习·npm·node.js
JianZhen✓37 分钟前
现在在本地开发了一些代码A,又有了新需求要紧急开发代码B需要只上线代码B的代码,如何更好的处理这种情况
前端
郝学胜-神的一滴1 小时前
Cesium绘制线:从基础到高级技巧
前端·javascript·程序人生·线性代数·算法·矩阵·图形渲染
蒙奇D索大1 小时前
【计算机网络】408计算机网络高分指南:物理层编码与调制技术精讲
java·前端·学习·计算机网络
无盐海2 小时前
CSRF漏洞攻击(跨站请求伪造攻击)
前端·csrf
慧一居士2 小时前
CSS3 全部功能点介绍,使用场景,对应功能点完整使用示例
前端