vue 指定区域可拖拽的限定拖拽区域的div(如仅弹窗标题可拖拽的弹窗)

html 复制代码
<template>
  <div class="container" ref="container">
    <div class="drag-box" v-drag>
      <div class="win_head">弹窗标题</div>
      <div class="win_content">弹窗内容</div>
    </div>
  </div>
</template>

<script>
export default {
  //自定义指令
  directives: {
    drag: {
      // 指令的定义
      bind: function (el, binding, vnode) {
        // 获取到vue实例
        let that = vnode.context;
        let drag_dom = el;
        // 获取到拖拽区
        let drag_handle = el.querySelector(".win_head");
        // 鼠标在拖拽区按下时触发拖拽
        drag_handle.onmousedown = (e) => {
          // 按下鼠标时,鼠标相对于被拖拽元素的坐标
          let disX = e.clientX - drag_dom.offsetLeft;
          let disY = e.clientY - drag_dom.offsetTop;

          // 获取容器dom
          let container_dom = that.$refs.container;

          document.onmousemove = (e) => {
            // 用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
            let left = e.clientX - disX;
            let top = e.clientY - disY;

            // 在容器范围内移动时,被拖拽元素的最大left值
            let leftMax =
              container_dom.offsetLeft +
              (container_dom.clientWidth - drag_dom.clientWidth);

            // 在容器范围内移动时,被拖拽元素的最小left值
            let leftMin = container_dom.offsetLeft + 1; //此处+1为容器的边框1px

            if (left > leftMax) {
              drag_dom.style.left = leftMax + "px";
            } else if (left < leftMin) {
              drag_dom.style.left = leftMin + "px";
            } else {
              drag_dom.style.left = left + "px";
            }

            // 在容器范围内移动时,被拖拽元素的最大left值
            let topMax =
              container_dom.offsetTop +
              (container_dom.clientHeight - drag_dom.clientHeight);

            // 在容器范围内移动时,被拖拽元素的最小left值
            let topMin = container_dom.offsetTop + 1; //此处+1为容器的边框1px

            if (top > topMax) {
              drag_dom.style.top = topMax + "px";
            } else if (top < topMin) {
              drag_dom.style.top = leftMin + "px";
            } else {
              drag_dom.style.top = top + "px";
            }
          };

          document.onmouseup = () => {
            document.onmousemove = null;
            document.onmouseup = null;
          };
        };
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.drag-box {
  position: absolute;
  top: 100px;
  left: 100px;
  width: 300px;
  height: 100px;
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);

  // 禁止文字被选中
  user-select: none;
}

.container {
  border: 1px solid red;
  width: 600px;
  height: 300px;
  margin: 30px;
}

.win_head {
  background-color: rgb(45, 141, 250);
  color: white;
  height: 30px;
  line-height: 30px;
  font-weight: bold;
  padding-left: 10px;
  cursor: move;
}
.win_content {
  padding: 10px;
}
</style>
相关推荐
Sheldon一蓑烟雨任平生13 小时前
Vue3 任务管理器(Pinia 练习)
vue.js·vue3·pinia·任务管理器·pinia 练习
前端加油站14 小时前
一份实用的Vue3技术栈代码评审指南
前端·vue.js
计算机学姐17 小时前
基于SpringBoot的高校社团管理系统【协同过滤推荐算法+数据可视化】
java·vue.js·spring boot·后端·mysql·信息可视化·推荐算法
Wang's Blog1 天前
前端FAQ: Vue 3 与 Vue 2 相⽐有哪些重要的改进?
前端·javascript·vue.js
ss2731 天前
Springboot + vue 医院管理系统
vue.js·spring boot·后端
今天也是爱大大的一天吖1 天前
vue2中的.native修饰符和$listeners组件属性
前端·javascript·vue.js
STUPID MAN1 天前
Linux使用tomcat发布vue打包的dist或html
linux·vue.js·tomcat·html
JIngJaneIL1 天前
助农惠农服务平台|助农服务系统|基于SprinBoot+vue的助农服务系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·助农惠农服务平台
云外天ノ☼1 天前
待办事项全栈实现:Vue3 + Node.js (Koa) + MySQL深度整合,构建生产级任务管理系统的技术实践
前端·数据库·vue.js·mysql·vue3·koa·jwt认证
一位搞嵌入式的 genius1 天前
前端实战开发(三):Vue+Pinia中三大核心问题解决方案!!!
前端·javascript·vue.js·前端实战