web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

vuejs鼠标拖着改变div宽度大小

566 ℃
     

功能介绍:左右2个div,大小根据用户需求可以进行拖动,具体代码如下:

template代码如下:

<template>
  <div class="drag-box" ref="dragBox">
    <div class="left">
      <!--左侧div内容-->
    </div>
    <div class="resize" title="收缩侧边栏">
      ⋮
    </div>
    <div class="right">
      <!--右侧div内容-->
    </div>
  </div>
</template>

script代码:

export default {
  data() {
    return {
      dragLeftDivWidth: "",
      dragRightDivWidth: ""
    };
  },
  mounted() {
    this.dragDiv();
  },
  methods: {
    dragDiv() {
      let resize = document.getElementsByClassName("resize");
      let left = document.getElementsByClassName("left");
      let right = document.getElementsByClassName("right");
      let dragBox = document.getElementsByClassName("drag-box");
      for (let i = 0; i < resize.length; i++) {
        // 鼠标按下事件
        resize[i].onmousedown = function(e) {
          //颜色改变提醒
          resize[i].style.background = "#818181";
          let startX = e.clientX;
          resize[i].left = resize[i].offsetLeft;
          // 鼠标拖动事件
          document.onmousemove = function(e) {
            let endX = e.clientX;
            // (endx-startx)=移动的距离。resize[i].left+移动的距离=左边区域最后的宽度
            let moveLen = resize[i].left + (endX - startX);

            // 容器宽度 - 左边区域的宽度 = 右边区域的宽度
            let maxT = dragBox[i].clientWidth - resize[i].offsetWidth;

            // 左边区域的最小宽度为32px
            if (moveLen < 32) moveLen = 32;

            //右边区域最小宽度为150px
            if (moveLen > maxT - 150) moveLen = maxT - 150;

            // 设置左侧区域的宽度
            resize[i].style.left = moveLen;

            for (let j = 0; j < left.length; j++) {
              left[j].style.width = moveLen + "px";
              right[j].style.width =
                dragBox[i].clientWidth - moveLen - 10 + "px";
              this.dragLeftDivWidth = moveLen + 10 + "px";
              this.dragRightDivWidth =
                dragBox[i].clientWidth - moveLen - 10 + "px";
            }
            console.log("获取左边宽度:", this.dragLeftDivWidth);
            console.log("获取右边宽度:", this.dragRightDivWidth);
          };
          // 鼠标松开事件
          document.onmouseup = function(evt) {
            //颜色恢复
            resize[i].style.background = "#d6d6d6";
            document.onmousemove = null;
            document.onmouseup = null;

            //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
            resize[i].releaseCapture && resize[i].releaseCapture();
          };

          //该函数在属于当前线程的指定窗口里设置鼠标捕获
          resize[i].setCapture && resize[i].setCapture();
          return false;
        };
      }
    }
  }
};

css代码:

.drag-box {
  width: 100%;
  height: 100%;
  margin: 1% 0px;
  overflow: hidden;
  box-shadow: -1px 9px 10px 3px rgba(0, 0, 0, 0.11);
}
/*左侧div样式*/
.left {
  width: calc(32% - 10px); /*左侧初始化宽度*/
  height: 100%;
  background: #ffffff;
  float: left;
}
/*拖拽区div样式*/
.resize {
  cursor: col-resize;
  float: left;
  position: relative;
  top: 45%;
  background-color: #d6d6d6;
  border-radius: 5px;
  margin-top: -10px;
  width: 10px;
  height: 50px;
  background-size: cover;
  background-position: center;
  /*z-index: 99999;*/
  font-size: 32px;
  color: white;
}
/*拖拽区鼠标悬停样式*/
.resize:hover {
  color: #444444;
}
/*右侧div样式*/
.right {
  float: left;
  width: 68%; /*右侧初始化宽度*/
  height: 100%;
  background: #fff;
  box-shadow: -1px 4px 5px 3px rgba(0, 0, 0, 0.11);
}

如何用Vue实现一个拖拽式可视化编辑器

纯js实现文字无缝滚动(亲测有效)

详解DedeCMSv5.5 删除去掉“匿名”评论的方法

分享一个帝国cms幻灯片插件

帝国cms如何过滤采集内容里的链接和广告

标签: div拖拽, vue拖拽

上面是“vuejs鼠标拖着改变div宽度大小”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_4029.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > vuejs
本文共计2343个字,预计阅读时长16分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款简单高效漂亮的多平台笔记应用——Zoho Notebook
下一篇: 推荐2款充满科技感的艺术字体——小新黑体、小新潮酷体
x 打工人ai神器