a-layout

文章展示了如何在Vue应用中利用A-Layout组件和CSS来创建一个可调整大小的布局。通过监听鼠标事件,用户可以动态改变侧边栏的宽度,同时主要内容区域的宽度也会相应自动调整,保证了最小宽度限制。这个功能对于构建响应式和自定义布局的Web应用非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<div style="display: flex;">
      <a-layout class="box_gsbz" ref="box_gsbz">
      <a-layout-sider :style="{ maxWidth: '0px', minWidth: '0px' }"></a-layout-sider>
      <div class="leftMove left_gsbz" :style="{ height: siderheigth }">

     </div>
      <div class="resize_gsbz" title="收缩"></div>
<a-layout-content class="mid_gsbz" :style="{height: siderheigth, backgroundColor:'white'}">
               
      </a-layout-content>
      </a-layout>

method

  mounted() {
    this.loadData()
    */
    this.gsbz_mouse()
    },
  gsbz_mouse() {
      const resize = document.getElementsByClassName('resize_gsbz')
      const left = document.getElementsByClassName('left_gsbz')
      const mid = document.getElementsByClassName('mid_gsbz')
      const box = document.getElementsByClassName('box_gsbz')
      for (let i = 0; i < resize.length; i++) {
        //鼠标悬浮事件
        resize[i].onmousemove = function (e) {
          //颜色改变提醒
          resize[i].style.background = '#40a9ff'
        }
        //鼠标移出事件
        resize[i].onmouseout = function (e) {
          //颜色恢复
          resize[i].style.background = '#E9EBF1'
        }
        // 鼠标按下事件
        resize[i].onmousedown = function (e) {
          //颜色改变提醒
          resize[i].style.background = '#40a9ff'
          const startX = e.clientX
          resize[i].left = resize[i].offsetLeft
          // 鼠标拖动事件
          document.onmousemove = function (e) {
            resize[i].style.background = '#40a9ff'
            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 < 200) moveLen = 200 // 左边区域的最小宽度为300px
            if (moveLen > maxT - 300) moveLen = maxT - 300 //右边区域最小宽度为300px

            resize[i].style.left = moveLen // 设置左侧区域的宽度
            console.log(moveLen)
            for (let j = 0; j < left.length; j++) {
              left[j].style.minWidth = moveLen + 'px'
              left[j].style.flex = `0 0 ${moveLen}px`
              left[j].style.maxWidth = moveLen + 'px'
              mid[j].style.width = box[i].clientWidth - moveLen - 12 - 4 + 'px'
            }
          }
          // 鼠标松开事件
          document.onmouseup = function (evt) {
            //颜色恢复
            resize[i].style.background = '#E9EBF1'
            document.onmousemove = null
            document.onmouseup = null
            resize[i].releaseCapture && resize[i].releaseCapture() //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
          }

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

css

  .resize_gsbz {
  cursor: col-resize;
  float: left;
  position: relative;
  transition-delay: 0.05s;
  /*top: 45%;*/
  width: 5px;
  /*height: 104px;*/
  margin-left: 0px;
  /*background-color: #cdcccc;*/
  /*margin-top: 17%;*/
}
  //解决拉伸不显示问题
.box_gsbz {
  display: -webkit-box !important;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值