欢迎来到代码驿站!

vue

当前位置:首页 > 网页前端 > vue

vue实现div高度可拖拽

时间:2023-03-10 10:46:21|栏目:vue|点击:

本文实例为大家分享了vue实现div高度可拖拽的具体代码,供大家参考,具体内容如下

这里有一个现成的demo,可以实现页面div的拖拽功能,但是和我想要的效果不是很一样,所以说后边有根据我的实际需求又重新修改了一下,先看一下现在的demo效果。

<template>
  <div id="eagleMapContainer" style="border: 1px solid red;overflow-y: auto;" title="">
    <div id="tz" @mousedown="dragEagle" style="border: 1px solid blue;">
      <div title="拖动调整大小" id="move_tz" style="border: 1px solid green;"></div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "eagleMap",
    data() {
      return {}
    },
    methods: {
      dragEagle: function (e) {
        var targetDiv = document.getElementById('eagleMapContainer'); 
        //得到点击时该地图容器的宽高:
        var targetDivHeight = targetDiv.offsetHeight;
        var startX = e.clientX;
        var startY = e.clientY;
        var _this = this;
        document.onmousemove = function (e) {
          e.preventDefault();
          //得到鼠标拖动的宽高距离:取绝对值
          var distX = Math.abs(e.clientX - startX);
          var distY = Math.abs(e.clientY - startY);
          //往上方拖动:
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight + distY + 'px';
          }
          //往下方拖动:
          if (e.clientX < startX && e.clientY > startY) {
            targetDiv.style.height = (targetDivHeight - distY) + 'px';
          }
          if (parseInt(targetDiv.style.height) >= 300) {
            targetDiv.style.height = 300 + 'px';
          }
          if (parseInt(targetDiv.style.height) <= 150) {
            targetDiv.style.height = 150 + 'px';
          }
        }
        document.onmouseup = function () {
          document.onmousemove = null;
        }
      }
    },
  };
</script>

<style scoped>
  #eagleMapContainer {
    position: absolute;
    left: 13%;
    bottom: 10px;
    z-index: 200;
    overflow: hidden;
    visibility: visible;
    width: 200px;
    height: 200px;
  }

  #tz {
    position: absolute;
    right: 1px;
    top: 1px;
    width: 27px;
    height: 20px;
    cursor: ne-resize;
    z-index: 200001;
    background-image: url("");

  }

  #tz:hover {
    background-color: #666;
  }

  #move_tz {
    position: absolute;
    right: 0px;
    top: 0px;
    width: 27px;
    height: 20px;
    cursor: ne-resize;
    z-index: 100;
    background-image: url("");
    background-position: 0px 0px;
  }
</style>

但是这个效果和我想要的不是很一样,所以得稍微改造了一下。

我想要效果是: 我有一个div,里面包含了很多小方块列表,因为超出设置了超出滚动,所以是在有滚动条的div上添加实现高度变化的拖拽。

接下来就是改造一下上边的demo,简单点,直接上代码:

在上边需要拖拽的div下面添加一个div,就是点到这个div开始实现拖拽功能。

<!-- 拖拉拽的小框 -->
    <div id="tz" @mousedown="dragEagle">
      <div title="拖动调整大小" id="move_tz"></div>
    </div>

需要根据拖拽实现高度变化的div设置一个id,假设为 “fuDiv”,然后编写方法。

// 拖拉
      dragEagle(e) {
        var targetDiv = document.getElementById('fuDiv');
        //得到点击时该地图容器的宽高:
        var targetDivHeight = targetDiv.offsetHeight;
        var startX = e.clientX;
        var startY = e.clientY;
        var _this = this;
        document.onmousemove = function (e) {
          e.preventDefault();
          //得到鼠标拖动的宽高距离:取绝对值
          var distY = Math.abs(e.clientY - startY);

          //往上方拖动:
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight - distY + 'px';
          }
          //往下方拖动:
          if (e.clientX < startX && e.clientY > startY) {
            targetDiv.style.height = (targetDivHeight + distY) + 'px';
          }
          if (parseInt(targetDiv.style.height) >= 320) {
            targetDiv.style.height = 320 + 'px';
          }
          if (parseInt(targetDiv.style.height) <= 160) {
            targetDiv.style.height = 160 + 'px';
          }
        }
        document.onmouseup = function () {
          document.onmousemove = null;
        }
      },

然后给他们设置一下css样式,其实这个地方就随意了,根据自己喜好来。

  #tz {
    width: 100%;
    height: 5px;
    cursor: s-resize;
    z-index: 200001;
  }
  
  #move_tz {
    width: 100%;
    height: 5px;
    cursor: s-resize;
    z-index: 100;
    background-image: url("");
    background-position: 0px 0px;
  }

最后效果:

效果不是特别的好,还有很多地方是值得优化以下的,暂时不写了。

上一篇:Vue使用Echart图标插件之柱状图

栏    目:vue

下一篇:没有了

本文标题:vue实现div高度可拖拽

本文地址:http://www.codeinn.net/misctech/227225.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有