vue实现div可拖动位置也可改变盒子大小的原理


Posted in Javascript onSeptember 16, 2020

以下是效果图:实现了div盒子在固定区域的拖动,也可改变盒子的高度和宽度,当超出边距后无法继续改变大小

vue实现div可拖动位置也可改变盒子大小的原理

这里说一下大致原理:拖动和改变大小是分开来操作的,接下来分别说一下

盒子拖动

这里用到了js的三个鼠标事件,分别是onmousedown(鼠标按下)、onmousemove(鼠标移动)以及onmouseup(鼠标松开),大致流程就是鼠标按下拖动图标进行拖动时,动态获取当前div的left和top再重新赋值给当前div的top、left值,当鼠标松开再清除事件,至于固定在某个区域内拖动,在赋值的时候判断当前top及left值是否超过限制区域的值,如果超过给最大值最小值

盒子改变大小

这里用到的也是盒子拖动的三个事件,当鼠标移入盒子左边框触发mousemove事件,动态计算盒子宽度重新赋值,鼠标松开注销mousrmove事件,我将宽度和高度改变分别封装了组件,用的时候直接调用就好

博主用的vue写的,这里展示的也是铜鼓vue书写的,其他都是大同小异,知道原理就好

// index.vue
<template>
 <!-- demo -->
 <div class="demo" id="maxBoxId">
  <div
   :id="moveInfo.dragId"
   :style="
    'width:' +
    moveInfo.width +
    'px; left:' +
    moveInfo.coordinate.x +
    'px; top:' +
    moveInfo.coordinate.y +
    'px; height:' +
    moveInfo.height +
    'px'
   "
   class="drag-class"
  >
   <div class="drag-content">
    <div class="content-text">
     <!-- 拖拽图标 -->
     <div class="drag-icon">
      <i
       class="iconfont icon-tuodong1 down-dragger"
       @mousedown.stop="dragDiv($event)"
       @mouseup.stop="dragUp($event)"
      ></i>
     </div>
     {{ moveInfo.text }}
    </div>
    <!-- 宽度改变组件 -->
    <ChangeWidth :moveId="moveInfo.moveId" index="0" @widthChange="changeWidth" @clearEvent="clearEvent" />
    <!-- 高度改变组件 -->
    <ChangeHeight :moveId="moveInfo.moveId" index="1" @heightChange="heightChange" @clearEvent="clearEvent" />
   </div>
  </div>
 </div>
</template>
 
<script>
import ChangeWidth from '../component/ChangeWidth'
import ChangeHeight from '../component/ChangeHeight'
export default {
 components: { ChangeWidth, ChangeHeight },
 name: 'demo',
 data() {
  return {
   moveInfo: {
    dragId: 'smallDragBoxId',
    moveId: 'smallMoveBoxId',
    text: '我是拖动的小盒子',
    width: 400,
    height: 100,
    // 上边距和左边距
    coordinate: {
     x: 180,
     y: 10
    }
   }
  }
 },
 methods: {
  // 区块拖动
  dragDiv(el, index) {
   // dragId: 可拖动区域唯一标识
   // moveId: 改变宽度组件唯一标识
   const { dragId, coordinate } = this.moveInfo
   let obig = document.getElementById('maxBoxId')
   let osmall = document.getElementById(dragId)
   // 用于保存小的div拖拽前的坐标
   osmall.startX = el.clientX - osmall.offsetLeft
   osmall.startY = el.clientY - osmall.offsetTop
   document.onmousemove = e => {
    let left, top
    left = e.clientX - osmall.startX
    top = e.clientY - osmall.startY
    osmall.style.left = left + 'px'
    osmall.style.top = top + 'px'
    coordinate.x = left
    coordinate.y = top
    if (left <= 0) {
     osmall.style.left = 0 + 'px'
     coordinate.x = 0
    }
    if (top <= 0) {
     osmall.style.top = 0 + 'px'
     coordinate.y = 0
    }
    if (left >= obig.offsetWidth - osmall.offsetWidth) {
     osmall.style.left = obig.offsetWidth - osmall.offsetWidth + 'px'
     coordinate.x = obig.offsetWidth - osmall.offsetWidth
    }
    if (top >= obig.offsetHeight - osmall.offsetHeight) {
     osmall.style.top = obig.offsetHeight - osmall.offsetHeight + 'px'
     coordinate.y = obig.offsetHeight - osmall.offsetHeight
    }
   }
  },
  dragUp(el) {
   document.onmousemove = null
   document.onmouseup = null
   // 调用接口保存数据
  },
  // 改变drag宽度尺寸
  changeWidth(params) {
   const { index, width } = params
   let left
   const { dragId } = this.moveInfo
   // let obig = document.getElementById('maxBoxId')
 
   let osmall = document.getElementById(dragId)
   let boxWidth = document.getElementById('maxBoxId').offsetWidth
   left = osmall.style.left
   const newWidth = this.moveInfo.width + width
   // outWidth拖动宽度时超出box的宽度
   const outWidth = Number(left.slice(0, left.length - 2)) + Number(newWidth) - Number(boxWidth)
   // 如果超出box将截取留下的
   if (outWidth >= 0) {
    this.moveInfo.width = Number(boxWidth) - Number(left.slice(0, left.length - 2))
   } else {
    this.moveInfo.width = newWidth
   }
   // 设置div的最小宽度和最大宽度
   if (this.moveInfo.width < 200) {
    this.moveInfo.width = 200
   }
   if (this.moveInfo.width > 900) {
    this.moveInfo.width = 900
   }
  },
  // 改变drag高度
  heightChange(params) {
   const { index, height } = params
   let top
   let osmall = document.getElementById(this.moveInfo.dragId)
   let boxHeight = document.getElementById('maxBoxId').offsetHeight
   top = osmall.style.top
   const newHeight = this.moveInfo.height + height
   // outHeight拖动宽度时超出box的高度
   const outHeight = Number(top.slice(0, top.length - 2)) + Number(newHeight) - Number(boxHeight)
   // 如果超出box将截取留下的
   if (outHeight >= 0) {
    this.moveInfo.height = Number(boxHeight) - Number(top.slice(0, top.length - 2))
   } else {
    this.moveInfo.height = newHeight
   }
   // 设置div的最小宽度和最大宽度
   if (this.moveInfo.height < 100) {
    this.moveInfo.height = 100
   }
   if (this.moveInfo.height > 200) {
    this.moveInfo.height = 200
   }
  },
  // 清除鼠标事件
  clearEvent() {
   document.onmousemove = null
   document.onmouseup = null
  }
 }
}
</script>
<style lang="scss" scoped>
.demo {
 position: relative;
 width: 100%;
 z-index: 10;
 width: 1200px;
 background: red;
 height: 300px;
 margin-bottom: 1000px;
 margin-left: 100px;
 .drag-class {
  background: rgba(255, 255, 255, 0);
  position: absolute;
  .drag-content {
   position: relative;
   height: 100%;
   .content-text {
    border: 1px dashed #ffffff;
    font-size: 34px;
    color: #ffffff;
    margin-top: 5px;
    position: relative;
    height: 100%;
    .drag-icon {
     position: absolute;
     right: 10px;
     top: 5px;
     float: left;
     // margin-right: 10px;
     .down-dragger {
      cursor: move;
      font-size: 30px;
      color: #dbdce0;
      color: #ffffff;
     }
    }
   }
  }
 }
}
</style>

以下是改变大小的组件

<template>
 <!-- 拖动右边距改变div宽度 -->
 <div :id="`width${moveId}`" class="x-handle" @mousedown="mouseDown"></div>
</template>
 
<script>
export default {
 name: 'ChangeWidth',
 props: ['index', 'moveId'],
 data() {
  return {
   lastX: ''
  }
 },
 
 created() {
  document.addEventListener('mouseup', this.mouseUp)
 },
 
 destroyed() {
  document.removeEventListener('mouseup', this.mouseUp)
 },
 
 methods: {
  mouseDown(event) {
   document.addEventListener('mousemove', this.mouseMove)
   this.lastX = event.screenX
  },
  mouseMove(e) {
   this.$emit('widthChange', { width: e.screenX - this.lastX, index: this.index })
   this.lastX = e.screenX
  },
  mouseUp() {
   this.lastX = ''
   document.removeEventListener('mousemove', this.mouseMove)
   this.$emit('clearEvent')
  }
 }
}
</script>
<style lang="less" scoped>
.x-handle {
 width: 5px;
 cursor: e-resize;
 background: #2866f0;
 height: 30px;
 position: absolute;
 right: 0;
 top: 40%;
}
</style>

改变高度的组件原理和宽度一样,避免代码重复就不上传了

上面就是大致流程和源码。

总结

到此这篇关于vue实现div可拖动位置也可改变盒子大小的文章就介绍到这了,更多相关vue 实现div拖动位置内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
二级域名转向类
Nov 09 Javascript
让innerText在firefox火狐和IE浏览器都能用的写法
May 14 Javascript
jquery.artwl.thickbox.js  一个非常简单好用的jQuery弹出层插件
Mar 01 Javascript
jquery插件EasyUI中form表单提交实例分享
Jan 11 Javascript
JavaScript隐式类型转换
Mar 15 Javascript
jQuery的实例及必知重要的jQuery选择器详解
May 20 Javascript
【经典源码收藏】基于jQuery的项目常见函数封装集合
Jun 07 Javascript
简单实现Vue的observer和watcher
Dec 21 Javascript
Bootstrap modal只加载一次数据的解决办法(推荐)
Nov 24 Javascript
微信小程序自定义音乐进度条的实例代码
Aug 28 Javascript
Nautil 中使用双向数据绑定的实现
Oct 02 Javascript
jquery绑定事件 bind和on的用法与区别分析
May 22 jQuery
Vue项目打包编译优化方案
Sep 16 #Javascript
Vue封装Axios请求和拦截器的步骤
Sep 16 #Javascript
如何在JS文件中获取Vue组件
Sep 16 #Javascript
javascript自定义加载loading效果
Sep 15 #Javascript
图解JS原型和原型链实现原理
Sep 15 #Javascript
vue实现简单全选和反选功能
Sep 15 #Javascript
vscode 调试 node.js的方法步骤
Sep 15 #Javascript
You might like
PHP按行读取文件时删除换行符的3种方法
2014/05/04 PHP
Zend Framework框架路由机制代码分析
2016/03/22 PHP
php 无限分类 树形数据格式化代码
2016/10/11 PHP
PHP执行系统命令函数实例讲解
2021/03/03 PHP
Javascript学习笔记 delete运算符
2011/09/13 Javascript
js实现省市联动效果的简单实例
2014/02/10 Javascript
php中给js数组赋值方法
2014/03/10 Javascript
jQuery插件Validate实现自定义表单验证
2016/01/18 Javascript
探寻JavaScript中this指针指向
2016/04/23 Javascript
AngularJS入门教程之数据绑定原理详解
2016/11/02 Javascript
Nodejs进阶:核心模块net入门学习与实例讲解
2016/11/21 NodeJs
angularjs+bootstrap菜单的使用示例代码
2017/03/07 Javascript
Chrome调试折腾记之JS断点调试技巧
2017/09/11 Javascript
javascript计算渐变颜色的实例
2017/09/22 Javascript
打字效果动画的4种实现方法(超简单)
2017/10/18 Javascript
js+canvas实现转盘效果(两个版本)
2020/09/13 Javascript
手动实现vue2.0的双向数据绑定原理详解
2021/02/06 Vue.js
[02:27]2014DOTA2国际邀请赛 VG赛后采访:更大的挑战在等着我们
2014/07/13 DOTA
[01:00:14]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs TNC 第三场
2018/04/10 DOTA
详解常用查找数据结构及算法(Python实现)
2016/12/09 Python
django实现前后台交互实例
2017/08/07 Python
对python中return和print的一些理解
2017/08/18 Python
python使用rpc框架gRPC的方法
2018/08/24 Python
python 读取数据库并绘图的实例
2019/12/03 Python
美国第一大药店连锁机构:Walgreens(沃尔格林)
2019/10/10 全球购物
Optimalprint加拿大:在线打印服务
2020/04/03 全球购物
十八届三中全会感言
2014/03/10 职场文书
总账会计岗位职责
2014/03/13 职场文书
雷锋式好少年事迹材料
2014/08/17 职场文书
科长个人四风问题整改措施思想汇报
2014/10/13 职场文书
稽核岗位职责范本
2015/04/13 职场文书
信用卡工作证明范本
2015/06/19 职场文书
幼儿园小班教育随笔
2015/08/14 职场文书
读完《骆驼祥子》的观后感!
2019/07/05 职场文书
解决python存数据库速度太慢的问题
2021/04/23 Python
Nginx虚拟主机的搭建的实现步骤
2022/01/18 Servers