vue中实现左右,上下拖拽(全,可复制)

左右拖拽效果图:

在这里插入图片描述

直接上代码:

html:

<div id="box">
  <div id="left"></div>
  <div id="resize"></div>
  <div id="right"></div>
</div>

CSS:

body,html{
   
  margin:0;
  padding:0;
  height:100%;
}
#box{
   
  width:600px;
  height:500px;
  overflow:hidden;
}
#left{
   
  width:calc(30% - 5px);
  height:100%;  
  background:skyblue;
  float:left;
}

#resize{
   
  width:5px;
  height:100%;
  cursor: w-resize;
  float:left;
}

#right{
   
  float:right;
  width:70%;
  height:100%;  
  background:tomato;
}

js:

window.onload = function(){
   
  var resize 
  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现上下面板之间可拖动的分割条,可以使用 Vue3 的指令 v-resize,该指令可以监听元素大小的变化,并且可以触发相应的回调函数。具体实现步骤如下: 1. 在上下两个面板之间添加一个分割条元素,例如一个 div。 2. 给分割条元素添加 v-resize 指令,并且绑定一个事件处理函数。 3. 在事件处理函数,获取上下两个面板的元素对象,计算它们的高度,并且根据鼠标移动事件动态改变上下面板的高度。 示例代码如下: ```html <template> <div class="split-panel"> <div class="top-panel" :style="{ height: topHeight + 'px' }"> <!-- 上面板内容 --> </div> <div class="split-bar" v-resize="onResize"></div> <div class="bottom-panel" :style="{ height: bottomHeight + 'px' }"> <!-- 下面板内容 --> </div> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const topHeight = ref(200); // 上面板高度 const bottomHeight = ref(300); // 下面板高度 // 分割条拖动事件处理函数 const onResize = (width, height) => { // 计算上下面板的高度 const totalHeight = height + topHeight.value + bottomHeight.value; const newTopHeight = height / totalHeight * 100; const newBottomHeight = (totalHeight - height) / totalHeight * 100; // 设置上下面板的高度 topHeight.value = newTopHeight; bottomHeight.value = newBottomHeight; }; return { topHeight, bottomHeight, onResize }; } }; </script> <style> .split-panel { position: relative; height: 100%; } .top-panel { position: absolute; top: 0; left: 0; right: 0; overflow: auto; } .split-bar { position: absolute; top: 200px; /* 上面板高度 */ left: 0; right: 0; height: 10px; background-color: #ccc; cursor: row-resize; } .bottom-panel { position: absolute; bottom: 0; left: 0; right: 0; overflow: auto; } </style> ``` 在上面的代码,我们使用 ref 创建了两个响应式变量 topHeight 和 bottomHeight,它们分别表示上下面板的高度。在 onResize 函数,我们根据分割条的高度动态计算出上下面板的高度,并且将它们设置为响应式变量的新值,从而实现面板高度的动态改变。最后,我们将 topHeight 和 bottomHeight 绑定到上下面板的 style 属性,以实现面板高度的动态渲染。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值