vue左右布局容器拖动调整宽度

const boxRef = ref<any>(null)
const width = ref<any>(260)
const height = ref<any>(400)
let startX = 0
let startY = 0
let startWidth = 0
let startHeight = 0
// 初始化拖动操作的函数
function initDrag(side, event) {
  // 记录初始鼠标位置
  startX = event.clientX
  startY = event.clientY
  // 获取目标元素的初始宽度和高度
  startWidth = boxRef.value ? boxRef.value.offsetWidth : 0
  startHeight = boxRef.value ? boxRef.value.offsetHeight : 0
  // 拖动时执行的函数
  const doDrag = (e: any) => {
    // 计算鼠标移动的距离
    const dx = e.clientX - startX
    const dy = e.clientY - startY
    // 根据拖动的方向调整元素的尺寸
    if (side === 'right') {
      // 向右拖动时,增加宽度
      width.value = startWidth + dx
    }
    if (side === 'bottom') {
      // 向下拖动时,增加高度
      height.value = startHeight + dy
    }
  }
  // 结束拖动时执行的函数
  const stopDrag = () => {
    // 移除鼠标移动和鼠标释放时的事件监听
    document.removeEventListener('mousemove', doDrag)
    document.removeEventListener('mouseup', stopDrag)
  }
  // 添加事件监听器,用于响应鼠标移动和鼠标释放事件
  document.addEventListener('mousemove', doDrag)
  document.addEventListener('mouseup', stopDrag)
}
<style scoped>
.scrollbarClass {
  height: calc(100vh - 46px);
}
.content {
  display: flex;
  height: calc(100vh - 46px);
  width: 100%;
}
.rightBox {
  flex: 1;
  background-color: rgb(255, 255, 255);
  height: calc(100vh - 46px);
}

.draggable-box {
  position: relative;
  width: v-bind(width + 'px');
  font-size: 12px;
  background-color: #fff;
}
.resize-handle {
  position: absolute;
  background: #ccc;
}
.resize-handle:hover {
  background-color: #000; /* 当鼠标悬停时改变背景颜色 */
}
.resize-handle.right {
  top: 0;
  bottom: 0;
  width: 2px;
  cursor: ew-resize;
}
.resize-handle.right {
  right: 0;
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

逍遥596607010

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

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

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

打赏作者

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

抵扣说明:

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

余额充值