手动拖拽改变DIV宽度,分栏

<template>
  <div ref="box" class="box">
    <div class="left">
      <div style="font-size:24px;text-align:left;background:#46A3FF">页面一</div>
    </div>
    <div class="resize"></div>
    <div class="mid">
      <div style="font-size:24px;text-align:left;background:#46A3FF">页面二</div>
    </div>
  </div>
</template>

<script>
  export default {
    mounted() {
      this.dragControllerDiv()
    },
    methods: {
      dragControllerDiv() {
        var resize = document.getElementsByClassName('resize')
        var left = document.getElementsByClassName('left')
        var mid = document.getElementsByClassName('mid')
        var box = document.getElementsByClassName('box')
        for (let i = 0; i < resize.length; i++) {
          // 鼠标按下事件
          resize[i].onmousedown = function(e) {
            //颜色改变提醒
            resize[i].style.background = '#cccccc'
            var startX = e.clientX
            resize[i].left = resize[i].offsetLeft
            // 鼠标拖动事件
            document.onmousemove = function(e) {
              var endX = e.clientX
              var moveLen = resize[i].left + (endX - startX) // (endx-startx)=移动的距离。resize[i].left+移动的距离=左边区域最后的宽度
              var maxT = box[i].clientWidth - resize[i].offsetWidth // 容器宽度 - 左边区域的宽度 = 右边区域的宽度

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

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

              for (let j = 0; j < left.length; j++) {
                left[j].style.width = moveLen + 'px'
                mid[j].style.width = box[i].clientWidth - moveLen - 10 + 'px'
              }
            }
            // 鼠标松开事件
            document.onmouseup = function(evt) {
              //颜色恢复
              resize[i].style.background = '#cccccc'
              document.onmousemove = null
              document.onmouseup = null
              resize[i].releaseCapture && resize[i].releaseCapture() //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
            }
            resize[i].setCapture && resize[i].setCapture() //该函数在属于当前线程的指定窗口里设置鼠标捕获
            return false
          }
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  /* 拖拽相关样式 */
  /*包围div样式*/
  .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: 30%; /*左侧初始化宽度*/
    height: 85vh;
    //background: #ffffff;
    float: left;
    border: 3px solid rgb(192, 192, 192);
  }
  /*拖拽区div样式*/
  .resize {
    cursor: col-resize;
    float: left;
    position: relative;
    background-color: rgb(192, 192, 192);
    // border-radius: 5px;
    // margin-top: -10px;
    width: 5px;
    height: 85vh;
    background-size: cover;
    background-position: center;
    /*z-index: 99999;*/
    font-size: 32px;
    //color: white;
  }
  /*拖拽区鼠标悬停样式*/
  .resize:hover {
    color: #444444;
  }
  /*右侧div'样式*/
  .mid {
    float: left;
    width: 69.5%; /*右侧初始化宽度*/
    height: 85vh;
    background: #fff;
    //box-shadow: -1px 4px 5px 3px rgba(0, 0, 0, 0.11);
    border: 3px solid rgb(192, 192, 192);
  }
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值