个人博客地址,更多精彩内容
template部分
<template>
<div class="drag-box" ref="box">
<div class="left">
<slot name="drag-left"></slot>
</div>
<div class="resize" title="收缩侧边栏">
⋮
</div>
<div class="mid">
<slot name="drag-right"></slot>
</div>
</div>
</template>
js部分
<script>
export default {
data() {
return {};
},
components: {},
computed: {},
watch: {},
created() {},
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('drag-box');
for (let i = 0; i < resize.length; i++) {
resize[i].onmousedown = function (e) {
resize[i].style.background = '#002240';
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);
var maxT = box[i].clientWidth - resize[i].offsetWidth;
if (moveLen < 200) moveLen = 200;
if (moveLen > maxT - 500) moveLen = maxT - 500;
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 - 20) + 'px';
}
};
$(window).resize(() => {
var currentWidth=left[0].offsetWidth;
var endX = e.clientX;
var moveLen = resize[i].left + (endX - startX);
var maxT = box[i].clientWidth - resize[i].offsetWidth;
if (moveLen < 200) moveLen = 200;
if (moveLen > maxT - 500) moveLen = maxT - 500;
resize[i].style.left = moveLen;
for (let j = 0; j < left.length; j++) {
left[j].style.width = currentWidth + 'px';
mid[j].style.width = (box[i].clientWidth - currentWidth - 20) + 'px';
}
});
document.onmouseup = function (evt) {
resize[i].style.background = '#002240';
document.onmousemove = null;
document.onmouseup = null;
resize[i].releaseCapture && resize[i].releaseCapture();
};
resize[i].setCapture && resize[i].setCapture();
return false;
};
}
},
}
}
</script>
CSS部分
<style lang="scss" scoped>
.drag-box {
width: 100%;
height: 100%;
overflow: hidden;
box-shadow: -1px 9px 10px 3px rgba(0, 0, 0, 0.11);
}
.left {
width: 200px;
height: 100%;
background: #FFFFFF;
float: left;
}
.resize {
margin-right: -2px;
cursor: col-resize;
float: left;
position: relative;
top: 45%;
background-color: #002240;
border-radius: 20px 0 0 20px;
margin-top: -10px;
line-height: 80px;
width: 20px;
height: 80px;
background-size: cover;
background-position: center;
font-size: 32px;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.resize:hover {
color: #409EFF;
}
.mid {
float: left;
width: calc(100% - 220px);
height: 100%;
background: #fff;
box-shadow: -1px 4px 5px 3px rgba(0, 0, 0, 0.11);
}
</style>
如何使用
<drag>
<div slot="drag-left">
</div>
<div slot="drag-right">
</div>
</drag>
效果展示
个人博客地址,更多精彩内容