VUE - div拖动边框调整宽度
VUE - DIV拖动边框调整宽度
父页面:
<template> <div class="m-body" > </div> </template>
<style lang="less" scoped> .m-body { position: relative; } </style>
子页面:
<template> <div class="ucard"> <div class="lresize" title="收缩侧边栏" v-show="dragItem.split(',').includes('left')">⋮</div> <div class="left" > </div> <div class="resize" title="收缩侧边栏" v-show="dragItem.split(',').includes('right')">⋮</div> </div> </template>
<script> export default { props: { pheight: { type: String, default: '' }, //内容高度 dragItem: { type: String, default: '' }, //left , right }, data() { return { }; }, computed: {}, mounted() { this.dragControllerDiv(); }, methods: { dragControllerDiv() { var lresize = document.getElementsByClassName('lresize'); var resize = document.getElementsByClassName('resize'); var left = document.getElementsByClassName('left'); var box = document.getElementsByClassName('m-body'); for (let i = 0; i < resize.length; i++) { // 鼠标按下事件 resize[i].onmousedown = function (e) { //颜色改变提醒 resize[i].style.background = '#818181'; 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[0].clientWidth - resize[i].offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度 if (moveLen < 200) moveLen = 200; // 200px if (moveLen > maxT - 150) moveLen = maxT - 150; //右边区域最小宽度为150px resize[i].style.left = moveLen; // 设置左侧区域的宽度 left[i].style.width = moveLen + 'px'; }; // 鼠标松开事件 document.onmouseup = function (evt) { //颜色恢复 resize[i].style.background = '#d6d6d6'; document.onmousemove = null; document.onmouseup = null; resize[i].releaseCapture && resize[i].releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉 }; resize[i].setCapture && resize[i].setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获 return false; }; } for (let i = 0; i < lresize.length; i++) { // 鼠标按下事件 lresize[i].onmousedown = function (e) { let cc = i; //颜色改变提醒 lresize[i].style.background = '#818181'; var startX = e.clientX; var leftWidthSource = left[i].offsetWidth; // lresize[i].left = lresize[i].offsetLeft; // 鼠标拖动事件 document.onmousemove = function (e) { var endX = e.clientX; var moveLen = leftWidthSource + (startX - endX); var maxT = box[0].clientWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度 if (moveLen < 200) moveLen = 200; // 200px if (moveLen > maxT - 150) moveLen = maxT - 150; //右边区域最小宽度为150px // lresize[i].style.left = moveLen; // 设置左侧区域的宽度 left[i].style.width = moveLen + 'px'; }; // 鼠标松开事件 document.onmouseup = function (evt) { //颜色恢复 lresize[i].style.background = '#d6d6d6'; document.onmousemove = null; document.onmouseup = null; lresize[i].releaseCapture && lresize[i].releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉 }; lresize[i].setCapture && lresize[i].setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获 return false; }; } }, }, }; </script>
<style lang="less" scoped> .lresize { cursor: col-resize; background-color: #d6d6d6; border-radius: 5px; width: 10px; height: 50px; background-size: cover; background-position: center; font-size: 32px; color: white; } .resize { cursor: col-resize; background-color: #d6d6d6; border-radius: 5px; width: 10px; height: 50px; background-size: cover; background-position: center; font-size: 32px; color: white; } /*拖拽区鼠标悬停样式*/ .resize:hover { color: #444444; } </style>
参考:https://blog.csdn.net/weixin_48398799/article/details/116535864