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

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>
相关推荐
AI大模型顾潇1 小时前
[特殊字符] 本地大模型编程实战(29):用大语言模型LLM查询图数据库NEO4J(2)
前端·数据库·人工智能·语言模型·自然语言处理·prompt·neo4j
九月TTS1 小时前
TTS-Web-Vue系列:Vue3实现内嵌iframe文档显示功能
前端·javascript·vue.js
爱编程的小学究1 小时前
【node】如何把包发布到npm上
前端·npm·node.js
weixin_473894772 小时前
前端服务器部署分类总结
前端·网络·性能优化
LuckyLay2 小时前
React百日学习计划-Grok3
前端·学习·react.js
澄江静如练_2 小时前
小程序 存存上下滑动的页面
前端·javascript·vue.js
互联网搬砖老肖2 小时前
Web 架构之会话保持深度解析
前端·架构
菜鸟una2 小时前
【taro3 + vue3 + webpack4】在微信小程序中的请求封装及使用
前端·vue.js·微信小程序·小程序·typescript·taro
hao_04133 小时前
elpis-core: 基于 Koa 实现 web 服务引擎架构设计解析
前端
狂野小青年3 小时前
npm 报错 gyp verb `which` failed Error: not found: python2 解决方案
前端·npm·node.js