原文鏈接:https://www.cnblogs.com/layaling/p/11009570.html
原文是左中右三種情況的拖動。由於項目需要,我刪除掉了右邊的,直接左右區域拖動調整div寬度
1、拖動,調整左右兩側寬度

<template>
<div class="wid100 hig100">
<ul class="box" ref="box">
<li class="left" ref="left">西瓜</li>
<li class="resize" ref="resize"></li>
<li class="mid" ref="mid">備注2</li>
</ul>
<ul class="box" ref="box">
<li class="left" ref="left">西瓜</li>
<li class="resize" ref="resize"></li>
<li class="mid" ref="mid">備注2</li>
</ul>
</div>
</template>
<script>
export default {
name: 'Dashboard',
mounted () {
this.dragControllerDiv();
},
methods: {
dragControllerDiv: function () {
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) {
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 < 150) moveLen = 150; // 左邊區域的最小寬度為150px
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) {
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>
ul,li{
list-style: none;
display: block;
margin:0;
padding:0;
}
.box{
width:100%;
height: 48%;
margin: 1% 0px;
overflow:hidden;
}
.left{
width:calc(30% - 10px);
height:100%;
background:#c9c9c9;
float:left;
}
.resize{
width:5px;
height:100%;
cursor: w-resize;
float:left;
}
.mid{
float:left;
width:70%;
height:100%;
background:#f3f3f3;
}
</style>
