首页 > 其他分享 >Vue拖动指令

Vue拖动指令

时间:2023-01-03 10:34:12浏览次数:31  
标签:el style Vue container 拖动 top drag 指令 left

drag.js

import _ from "lodash";

class Drap {
  constructor(el, option = { parent: null }) {
    this.el = el;
    this.x = 0;
    this.y = 0;
    this.el.style.position = "absolute";
    // 父容器
    if (option?.parent) {
      this.container = document.getElementById(option.parent);
    } else {
      this.container = el.parentNode;
    }
    this.init();
  }
  init() {
    this.el.onmousedown = (e) => {
      e.stopPropagation();
      this.el.style.cursor = "move";
      this.el.style.zIndex = 999;
      this.lastLeft = this.el.offsetLeft;
      this.lastTop = this.el.offsetTop;
      this.x = e.clientX;
      this.y = e.clientY;

      this.container.onmousemove = (e) => {
        const offsetX = e.clientX - this.x;
        let left = this.lastLeft + offsetX;
        if (offsetX < 0) {
          // 向左滑动
          if (left < 0) {
            left = 0;
          }
        } else {
          // 向右滑动
          if (left > this.container.offsetWidth - this.el.offsetWidth) {
            left = this.container.offsetWidth - this.el.offsetWidth;
          }
        }

        const offsetY = e.clientY - this.y;
        let top = this.lastTop + offsetY;
        if (offsetY < 0) {
          // 向上滑动
          if (top < 0) {
            top = 0;
          }
        } else {
          // 向下滑动
          if (top > this.container.offsetHeight - this.el.offsetHeight) {
            top = this.container.offsetHeight - this.el.offsetHeight;
          }
        }

        this.el.style.left = left + "px";
        this.el.style.top = top + "px";
      };

      document.onmouseup = (e) => {
        this.container.onmousemove = null;
        document.onmouseup = null;
        this.el.style.cursor = "pointer";
      };
    };
  }
}

export const drag = {
  mounted(el, binding) {
    new Drap(el, binding.value || {});
  },
};

main.js注册

import { createApp } from "vue";
import App from "./App.vue";
import { drag } from "./utils/drag";

const app = createApp(App);
app.directive("drag", drag);

组件使用

<div class="mapBox" id="mapBox">
    <div id="map" class="map"></div>
    <div class="search">
      <div v-drag="{ parent: 'mapBox' }" class="content">
        这是一个窗体
      </div>
    </div>
</div>
<style lang="less" scoped>
.mapBox {
  width: 100%;
  height: 100%;
  position: relative;

  .map {
    width: 100%;
    height: 100%;
  }

  .search {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 100;

    .content {
      width: 430px;
      min-height: 385px;
      background-color: #fff;
    }
  }
}
</style>

 

标签:el,style,Vue,container,拖动,top,drag,指令,left
From: https://www.cnblogs.com/shi2310/p/17021324.html

相关文章

  • vue-slot及自定义分发
    Vue-slot插槽应用在组合组件的场景中<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><divid="app">......
  • 2023.01.03 - vue项目开启https调试
    vue-cli3.x项目:使用vue脚手架3.x搭建的项目,配置开启https方法比较简单,在项目根目录下的vue.config.js文件中增加属性https:true即可。//vue.config.jsmodule.expor......
  • 前端二面vue面试题(边面边更)
    Vuex有哪几种属性?有五种,分别是State、Getter、Mutation、Action、Modulestate=>基本数据(数据源存放地)getters=>从基本数据派生出来的数据mutations=>提交......
  • 滴滴前端一面高频vue面试题及答案
    keep-alive使用场景和原理keep-alive是Vue内置的一个组件,可以实现组件缓存,当组件切换时不会对当前组件进行卸载。一般结合路由和动态组件一起使用,用于缓存组件......
  • vue组件通信6种方式总结(常问知识点)
    前言在Vue组件库开发过程中,Vue组件之间的通信一直是一个重要的话题,虽然官方推出的Vuex状态管理方案可以很好的解决组件之间的通信问题,但是在组件库内部使用Vuex往往会......
  • Vue3:子组件 watch 父组件传递的 props,watch 不触发,该如何通知子组件更新?
    问题阐述问题描述父组件传递给子组件props,由于父组件的业务需求,希望子组件通过watch监听props修改数据。父组件模板:<PaginationPageref="pagination"@fixed......
  • vuex报错 this.$store显示undefined
    报错:vuex报错this.$store显示undefined可能是版本问题或者store压根就没有注入到vue的原型上所以导致undefined。解决方案检查main.js中是否将store注入vue实例中,如果......
  • vue省市区级联
    省市区级联<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><scriptsrc="js/vue.js"type="text/javascript"cha......
  • 关于centos7下所有指令失效
    起因:疑似宝塔更新修复后,本地所有环境变量集体不生效问题环境xshell环境下ssh连接问题描述:-环境变量无法保存-所有的保存方式都是临时生效-关闭ssh会话后,本......
  • Vue-element-template项目学习笔记
    1.vue在css引入背景图片报错:Modulenotfound:Error:Can'tresolve'../../images/icons/loading2.gif'in'/home。报错信息就是找不到路径,我是这样的写法:backgroun......