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

以下是效果图:实现了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拖动位置内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • vue中实现拖动调整左右两侧div的宽度的示例代码

    写在最前 最近在使用vue的时候,遇到一个需求,实现左右div可通过中间部分拖拽调整宽度,类似于这样 这是我最终的实现效果 还是老话,因为我不是专业的前端工程师,只是兼职写一些简单的前端,所以这个功能的实现得益于以下博客,<vue 拖动调整左右两侧div的宽度>.<vuejs中拖动改变元素宽度实现宽度自适应大小>,而我只是针对于他们提供的代码,加了亿点点自己所需要的细节. 实现原理 如上图所示,我们需要将要实现此功能的页面划分为三个部分,左部.调整区.右部,分别对应css样式为le

  • vue中解决拖拽改变存在iframe的div大小时卡顿问题

    写在最前 针对于在vue中实现拖拽改变两左右个div大小的方式,请查看上一篇文章<vue中实现拖动调整左右两侧div的宽度>.此文章主要针对于实际应用中需要拖拽改变大小的组件中使用iframe框架时存在明显卡顿的问题,比如这样,右侧div中使用了一个iframe组件,导致实际操作中出现两个问题,一个是拖不动,另外一个是无法根据鼠标移动,快速响应,甚至在监听鼠标的按下和松开事件上都有明显的卡顿问题.如果去除右侧iframe框架,则没有问题. 有iframe情况 无iframe情况 问题原因&am

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

    以下是效果图:实现了div盒子在固定区域的拖动,也可改变盒子的高度和宽度,当超出边距后无法继续改变大小 这里说一下大致原理:拖动和改变大小是分开来操作的,接下来分别说一下 盒子拖动 这里用到了js的三个鼠标事件,分别是onmousedown(鼠标按下).onmousemove(鼠标移动)以及onmouseup(鼠标松开),大致流程就是鼠标按下拖动图标进行拖动时,动态获取当前div的left和top再重新赋值给当前div的top.left值,当鼠标松开再清除事件,至于固定在某个区域内拖动,在赋值的

  • c#禁止通过拖动,双击标题栏改变窗体大小的方法

    最近写windows窗体程序,发现一个烦人的问题. 窗体初始化时禁用了最大化按钮,并使之最大化.本希望窗体一直保持最大化.但是拖动,双击标题栏时窗体就会缩小.烦死了+_+. 最后,终于找到了解决方法. //禁止通过拖动,双击标题栏改变窗体大小. public const int WM_NCLBUTTONDBLCLK = 0xA3; const int WM_NCLBUTTONDOWN = 0x00A1; const int HTCAPTION = 2; protected override vo

  • vue实现div拖拽互换位置

    本文实例为大家分享了vue实现div拖拽互换位置的具体代码,供大家参考,具体内容如下 template模板 <transition-group tag="div" class="container"> <div class="item" v-for="(item,index) in items" :key="item.key" :style="{background:item.c

  • vue+echarts实现可拖动节点的折线图(支持拖动方向和上下限的设置)

    本篇文档主要是利用echarts实现可拖动节点的折线图,在echarts中找到了一个demo,传送门:https://echarts.baidu.com/examples/editor.html?c=line-draggable,但是不是用vue写的,并且在改写为vue组件的过程中遇到了很多问题,在百度过程中发现并没有相关的文档,所以决定自己开发,并在demo的基础上开发了一些实用的功能,所以把这个过程记录下来.文档中还有很多不够完善的地方,欢迎讨论哈! 需求:制作一个折线图用于显示当前24小时

  • vue+mousemove实现鼠标拖动功能(拖动过快失效问题解决方法)

    今天用vue+原生js的mousemove事件,写了个拖动,发现只能慢慢拖动才行,鼠标只要移动快了,就失效,不能拖动了: 搞了半天在,总算解决了,但是问题的深层原理还没搞清楚,知道的大侠可以留言分享,下面直接上代码: 只能慢速拖动的代码: <!DOCTYPE html> <html> <head> <title>vue结合原生js实现拖动</title> <script src="https://cdn.bootcss.com/v

  • vue实现移动端拖动排序

    本文实例为大家分享了vue实现移动端拖动排序的具体代码,供大家参考,具体内容如下 效果 代码: <template> <div> <div class="mainDiv" id="columns"> <div id="child" class="childDiv" v-for="(option,index) in options" :key="index&

  • PHP+jQuery实现随意拖动层并即时保存拖动位置

    想拖动页面上的层,完全可以用jQuery ui的Draggable方法来实现,那如何将拖动后层的位置保存下来呢?本文将给出答案.本文讲解了如何采用PHP+MySQL+jQuery,实现随意拖动层并即时保存拖动位置. 之前我有文章:,文中以项目为示例,讲解了实现拖动布局的方法.本文与之不同之处在于可以任意拖动页面位置,原理就是通过拖动将拖动后层的相对位置left,top和z-index三个参数更新到数据表中对应的记录,页面通过CSS解析每个层不同的位置.请看具体实现步骤. 准备MySQL数据表 首

  • PHP+MySQL+jQuery随意拖动层并即时保存拖动位置实例讲解

    想拖动页面上的层,完全可以用jQuery ui的Draggable方法来实现,那如何将拖动后层的位置保存下来呢?本文将给出答案.本文讲解了如何采用PHP+MySQL+jQuery,实现随意拖动层并即时保存拖动位置. 本文原理就是通过拖动将拖动后层的相对位置left,top和z-index三个参数更新到数据表中对应的记录,页面通过CSS解析每个层不同的位置.请看具体实现步骤. 准备MySQL数据表 首先需要准备一张表notes,用来记录层的内容,背景色和坐标等信息. CREATE TABLE IF

  • JavaScript实现DIV层拖动及动态增加新层的方法

    本文实例讲述了JavaScript实现DIV层拖动及动态增加新层的方法.分享给大家供大家参考.具体分析如下: 无刷新添加一个新的DIV层,并可实现的该层的拖动,鼠标拖动层可移动位置,将JS部分另存为一个新文件,用到的时候从外部引入,这个拖动层代码很流行,GG和YAHOO等大网站经常可以看到这种效果. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=

  • VUE实现可随意拖动的弹窗组件

    背景:项目需要,我们引入了前端框架就是目前最流行的框架之一vue,同时引入了一套由饿了吗维护的ui库,由于我们是在pc端使用发现它竟然没有提供可随意拖动的窗口,可能用的更多的时移动端吧吧,于是就随手写了一个,比较简单吧,但是做过的就会知道也是有一些小小的技巧,记录下吧留作备用. 由于不是很难,就不做过多解释了,直接上代码: <template> <el-container v-bind:id="id" v-if="dialogVisible">

随机推荐