Vue3中我們會遇到自由拖拽寬度和高度的頁面需求,查看很多方法都無法滿足當前需求。下面是我們Vue3版本的代碼,非常簡單主要構想說粗發(fā)拖拽方法,把所需要的div的高寬進行拖拽位置進行監(jiān)聽來加減自身div的px值。直接復制粘貼就可以實現(xiàn)效果。根據(jù)自己需求更改即可投入使用,非常方便快捷。(本人文章全都是免費開源,拒絕收費。構建果然免費共享技術環(huán)境)
<template>
<div class="Drag2">
<div class="box" ref="box">
<div class="left">
<!--左側(cè)div內(nèi)容-->
</div>
<div class="resize" title="左右側(cè)邊欄">?</div>
<div class="mid">
<!--右側(cè)div內(nèi)容-->
<div class="topBox">
<!--右上div內(nèi)容-->
</div>
<div title="上下側(cè)邊欄" class="move">?</div>
<div class="downBox">
<!--右下div內(nèi)容-->
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
onMounted(() => {
dragControllerLR();
dragControllerUD();
});
// 左右拖動事件
function dragControllerLR() {
var resize = document.getElementsByClassName("resize");
var left = document.getElementsByClassName("left");
var mid = document.getElementsByClassName("mid");
var box = document.getElementsByClassName("box");
console.log(document.getElementsByClassName("resize"));
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+移動的距離=左邊區(qū)域最后的寬度
var maxT = box[i].clientWidth - resize[i].offsetWidth; // 容器寬度 - 左邊區(qū)域的寬度 = 右邊區(qū)域的寬度
if (moveLen < 50) moveLen = 50; // 左邊區(qū)域的最小寬度為50px
if (moveLen > maxT - 150) moveLen = maxT - 150; //右邊區(qū)域最小寬度為150px
resize[i].style.left = moveLen; // 設置左側(cè)區(qū)域的寬度
for (let j = 0; j < left.length; j++) {
left[j].style.width = moveLen + "px";
mid[j].style.width = box[i].clientWidth - moveLen - 10 + "px";
}
};
// 鼠標松開事件
// eslint-disable-next-line no-unused-vars
document.onmouseup = function (evt) {
//顏色恢復
resize[i].style.background = "#d6d6d6";
document.onmousemove = null;
document.onmouseup = null;
resize[i].releaseCapture && resize[i].releaseCapture(); //當你不在需要繼續(xù)獲得鼠標消息就要應該調(diào)用ReleaseCapture()釋放掉
};
resize[i].setCapture && resize[i].setCapture(); //該函數(shù)在屬于當前線程的指定窗口里設置鼠標捕獲
return false;
};
}
}
// 上下拖動事件
function dragControllerUD() {
var resize = document.getElementsByClassName("move");
var topBox = document.getElementsByClassName("topBox");
var downBox = document.getElementsByClassName("downBox");
var box = document.getElementsByClassName("mid");
console.log(document.getElementsByClassName("move"));
for (let i = 0; i < resize.length; i++) {
// 鼠標按下事件
resize[i].onmousedown = function (e) {
console.log(resize[i].top);
//顏色改變提醒
resize[i].style.background = "#818181";
var startY = e.clientY;
resize[i].top = resize[i].offsetTop;
// 鼠標拖動事件
document.onmousemove = function (e) {
var endY = e.clientY;
var moveLen = resize[i].top + (endY - startY); // (endY - startY)=移動的距離。resize[i].top+移動的距離=上邊區(qū)域最后的高度
var maxT = box[i].clientHeight - resize[i].offsetHeight; // 容器高度 - 上邊區(qū)域的高度 = 下邊區(qū)域的高度
if (moveLen < 50) moveLen = 50; // 上邊區(qū)域的最小高度為50px
if (moveLen > maxT - 150) moveLen = maxT - 150; //下邊區(qū)域最小高度為150px
resize[i].style.top = moveLen; // 設置上邊區(qū)域的高度
for (let j = 0; j < topBox.length; j++) {
topBox[j].style.height = moveLen + "px";
downBox[j].style.height = box[i].clientHeight - moveLen - 10 + "px";
}
};
// 鼠標松開事件
document.onmouseup = function () {
//顏色恢復
resize[i].style.background = "#d6d6d6";
document.onmousemove = null;
document.onmouseup = null;
resize[i].releaseCapture && resize[i].releaseCapture(); //當你不在需要繼續(xù)獲得鼠標消息就要應該調(diào)用ReleaseCapture()釋放掉
};
resize[i].setCapture && resize[i].setCapture(); //該函數(shù)在屬于當前線程的指定窗口里設置鼠標捕獲
return false;
};
}
}
</script>
<style>
/* 拖拽相關樣式 */
/*包圍div樣式*/
.box {
width: 100vh;
height: calc(98vh - 10px);
margin: 1% 0px;
overflow: hidden;
box-shadow: -1px 9px 10px 3px rgba(0, 0, 0, 0.11);
}
/*左側(cè)div樣式*/
.left {
width: calc(32% - 10px); /*左側(cè)初始化寬度*/
height: 100%;
background: #71ad88;
float: left;
}
/* 拖拽區(qū)div樣式 */
.resize {
cursor: w-resize;
float: left;
position: relative;
top: 45%;
background-color: #d6d6d6;
border-radius: 5px;
margin-top: -10px;
width: 10px;
height: 50px;
background-size: cover;
background-position: center;
/*z-index: 99999;*/
font-size: 32px;
color: white;
}
/*拖拽區(qū)鼠標懸停樣式*/
.move:hover {
color: #444444;
}
/*右側(cè)div'樣式*/
.mid {
float: left;
width: 68%; /*右側(cè)初始化寬度*/
height: 100%;
background: #f3f3f3;
box-shadow: -1px 4px 5px 3px rgba(0, 0, 0, 0.11);
/*上方div'樣式*/
.topBox {
height: 60%;
background-color: #3ff53f;
display: flex;
}
/*下方div'樣式*/
.downBox {
height: calc(40% - 10px);
background-color: #f0fd35;
display: flex;
}
/* 拖拽區(qū)div樣式 */
.move {
cursor: s-resize;
width: 50px;
height: 10px;
background-color: #d6d6d6;
margin: 0 auto;
border-radius: 5px;
text-align: center;
line-height: 3px;
font-size: 32px;
color: white;
}
/*拖拽區(qū)鼠標懸停樣式*/
.move:hover {
color: #444444;
}
}
</style>
效果圖如下:
下面希望各位大哥支持一下我開發(fā)的小程序,萘兔小程序用戶始終堅持免費提供用戶使用的一個小程序,提成程序免費便利于群眾,服務群眾的宗旨。同時不斷提高自身的能力和了解用戶需求,開拓自身的程序視野,快速提高自我技術,未編程共享繼續(xù)提供更多的免費代碼交流環(huán)境。點擊一下增加一下我的用戶量。如有建議可以私信我非常感激。
Vue3介紹(不用理會)
vue3.0帶來了什么
1.性能的提升
打包大小減少41%
初次渲染快55%,更新渲染塊133%
內(nèi)存減少54%
........
2.源碼的升級
使用Proxy代替defineProperty實現(xiàn)響應式
重寫虛擬DOM的實現(xiàn)和Tree-Sharking
......
3.擁抱TypeScript
vue3.0更好的支持TypeScript
4.新的特性
Composition API(組合api)
。 setup配置
。ref與reactive
。watch與watchEffect
。 provide和inject
。 .......
新的內(nèi)置組件
。 Fragment
。Teleport
。Suspense
其他改變
。新的生命周期鉤子
。data選項應始終被聲明為一個函數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-486720.html
。移除keyCode支持作為v-on的修飾符文章來源地址http://www.zghlxwxcb.cn/news/detail-486720.html
到了這里,關于Vue3中div自由拖拽寬度和高度。的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!