vue實(shí)現(xiàn)多欄布局拖拽
本文實(shí)例為大家分享了vue實(shí)現(xiàn)多欄布局拖拽的具體代碼,供大家參考,具體內(nèi)容如下
一、目標(biāo)
vue 實(shí)現(xiàn)多個(gè)盒子(用戶根據(jù)實(shí)際場(chǎng)景決定盒子數(shù)量)自由拖拽,改變寬度。
二、應(yīng)用場(chǎng)景
可自由拖動(dòng)寬度的多欄布局。
最典型的案例:編輯器(eg:vscode,idea等)

三、組件設(shè)計(jì)
由于該組件盒子數(shù)量不確定,所以我們?cè)O(shè)計(jì)組件時(shí)參考了Vuetify中的Form和FormItem的設(shè)計(jì)。即:外層大盒子處理分發(fā)的拖拽事件,里層的盒子負(fù)責(zé)展示各個(gè)Item的內(nèi)容。
組件設(shè)計(jì)實(shí)現(xiàn)目標(biāo):
<drag-box style="width: 100%; height: 100%;"> <drag-item>item1</drag-item> <drag-item>item2</drag-item> <drag-item>item3</drag-item> <drag-item>item4</drag-item> </drag-box>
四、實(shí)現(xiàn)
4.1 dragBox 靜態(tài)頁(yè)面
(通過(guò)插槽實(shí)現(xiàn)子元素的嵌套)
<template>
<div ref='dragBox' style='display: flex; width: 100%; height: 100%;'>
<slot></slot>
</div>
</template>
4.2 dragItem 頁(yè)面
(通過(guò)插槽實(shí)現(xiàn)drag-item內(nèi)部元素的嵌套)
<template>
<div ref="container" class="d-flex" style="min-width: 200px; position: relative;">
<div style="width: 100%; height: 100%;">
<slot>默認(rèn)信息</slot>
</div>
<!-- 拖拽條 -->
<div v-if="resizeShow" class="resize" />
</div>
</template>
<script>
export default {
props: {
// 控制拖拽條的是否顯示,默認(rèn)顯示
resizeShow: {
type: Boolean,
default: true
}
}
}
</script>
<style>
.resize {
position: absolute;
top: 0;
right: 0;
width: 4px;
height: 100%;
cursor: col-resize;
background-color: #d6d6d6;
}
</style>
4.3 拖拽邏輯
拖拽的邏輯應(yīng)當(dāng)交給dragBox處理,而非dragItem。
4.3.1 在實(shí)現(xiàn)拖拽之前,應(yīng)當(dāng)給子元素(即:dragItem)進(jìn)行合理布局。
當(dāng)用戶未給 dragItem 分配初始寬度時(shí),則默認(rèn)flex:1(平均分配剩余空間)。具體邏輯如下:
// 如果dragItem 沒(méi)有定義寬度,則flex=1
setDragItemFlex () {
const dragBox = this.$refs.dragBox
const childsLen = dragBox.children.length
for (let i = 0; i < childsLen; i++) {
const node = dragBox.children[i]
if (!node.style.width) {
// 如果沒(méi)有定義寬度,則flex=1
node.style.flex = 1
}
}
},
4.3.2 拖拽實(shí)現(xiàn)邏輯
應(yīng)當(dāng)為每個(gè)dragItem的拖動(dòng)條添加拖動(dòng)事件。完整的拖動(dòng)事件包括:鼠標(biāo)按下,鼠標(biāo)移動(dòng),鼠標(biāo)抬起(拖動(dòng)結(jié)束)。
循環(huán)為每個(gè)拖動(dòng)條添加事件:
dragControllerDiv () {
const resize = document.getElementsByClassName('resize') // 拖拽條
// 循環(huán)為每個(gè)拖拽條添加事件
for (let i = 0; i < resize.length; i++) {
// 鼠標(biāo)按下事件
resize[i].addEventListener('mousedown', this.onMouseDown)
}
},
鼠標(biāo)按下邏輯:獲取鼠標(biāo)按下的初始位置,改變拖拽條顏色,添加move和up的監(jiān)聽(tīng)事件。
onMouseDown (e) {
this.resizeBox = e.target
this.currentBox = this.resizeBox.parentNode// 當(dāng)前盒子
this.rightBox = this.getNextElement(this.currentBox)// 當(dāng)前盒子的下個(gè)兄弟節(jié)點(diǎn)
if (!this.rightBox) return
this.curLen = this.currentBox.clientWidth
this.otherBoxWidth = this.$refs.dragBox.clientWidth - this.currentBox.clientWidth - this.rightBox.clientWidth // 其他盒子的寬度
// 顏色改變提醒
this.resizeBox.style.background = '#818181'
this.startX = e.clientX
document.addEventListener('mousemove', this.onMousemove)
document.addEventListener('mouseup', this.onMouseup)
},
// 獲取下一個(gè)兄弟元素的兼容函數(shù)
getNextElement (element) {
if (element.nextElementSibling) {
return element.nextElementSibling
} else {
var next = element.nextSibling// 下一個(gè)兄弟節(jié)點(diǎn)
while (next && next.nodeType !== 1) { // 有 并且 不是我想要的
next = next.nextSibling
}
return next
}
}
鼠標(biāo)移動(dòng)事件:計(jì)算并設(shè)置當(dāng)前盒子和右側(cè)盒子的寬度。
onMousemove (e) {
const endX = e.clientX
const moveLen = endX - this.startX // (endx-startx)= 移動(dòng)的距離
const CurBoxLen = this.curLen + moveLen // resize[i].left+移動(dòng)的距離=左邊區(qū)域最后的寬度
const rightBoxLen = this.$refs.dragBox.clientWidth - CurBoxLen - this.otherBoxWidth // 右側(cè)寬度=總寬度-左側(cè)寬度-其它盒子寬度
// 當(dāng)最小寬度時(shí),無(wú)法繼續(xù)拖動(dòng)
if (CurBoxLen <= 200 || rightBoxLen <= 200) return
this.currentBox.style.width = CurBoxLen + 'px'// 當(dāng)前盒子的寬度
this.resizeBox.style.left = CurBoxLen // 設(shè)置左側(cè)區(qū)域的寬度
this.rightBox.style.width = rightBoxLen + 'px'
},
鼠標(biāo)抬起事件:銷毀mousedown和mousemove事件;恢復(fù)拖拽條的顏色。
onMouseup () {
// 顏色恢復(fù)
this.resizeBox.style.background = '#d6d6d6'
document.removeEventListener('mousedown', this.onMouseDown)
document.removeEventListener('mousemove', this.onMousemove)
},
在mounted鉤子函數(shù)里添加對(duì)應(yīng)的事件。
mounted () {
this.setDragItemFlex()
this.dragControllerDiv()
},
引入并注冊(cè)使用該組件:
<template>
<div id="app" style="width: 100%; height: 100vh; border:1px solid #ccc;">
<drag-box style="width: 100%; height: 100%;">
<drag-item style="width: 20%;">item1</drag-item>
<drag-item>item2</drag-item>
<drag-item style="width: 20%;" :resizeShow='false'>item3</drag-item>
</drag-box>
</div>
</template>
<script>
import {DragBox, DragItem} from './components/dragLayouter'
export default {
name: 'App',
components: {
DragBox,
DragItem
}
}
</script>
五、運(yùn)行結(jié)果

具體樣式可后期修改。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue el-upload單圖片上傳功能實(shí)現(xiàn)
這篇文章主要介紹了Vue el-upload單圖片上傳功能實(shí)現(xiàn),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
vue父列表數(shù)據(jù)獲取子列表數(shù)據(jù)的實(shí)現(xiàn)步驟
本文檔將介紹如何通過(guò)點(diǎn)擊父列表(表格)中的數(shù)據(jù)行來(lái)獲取到子列表(表格)的數(shù)據(jù),代碼示例是基于Vue框架實(shí)現(xiàn)的一個(gè)組件,包含了父列表和子列表,通過(guò)點(diǎn)擊父列表的數(shù)據(jù)行來(lái)動(dòng)態(tài)獲取子列表的數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2024-06-06
vue?webpack打包原理解析(全網(wǎng)最新最全)
webpack是讓我們可以進(jìn)行模塊化開(kāi)發(fā),并且會(huì)幫助我們處理模塊間的依賴關(guān)系,這篇文章主要介紹了vue?webpack打包原理,本篇介紹的有點(diǎn)長(zhǎng),希望大家耐心閱讀2023-02-02
前端vue3項(xiàng)目中百度地圖的使用api以及操作實(shí)例
最近項(xiàng)目要用到百度地圖api,好久沒(méi)用到地圖,就百度了一番,但是找了好幾篇文章,發(fā)現(xiàn)都沒(méi)辦法成功實(shí)現(xiàn),現(xiàn)將方法記錄如下,下面這篇文章主要給大家介紹了關(guān)于前端vue3項(xiàng)目中百度地圖的使用api以及操作實(shí)例,需要的朋友可以參考下2023-05-05
vue-cli與webpack處理靜態(tài)資源的方法及webpack打包的坑
這篇文章主要介紹了vue-cli與webpack處理靜態(tài)資源的方法,需要的朋友可以參考下2018-05-05
ant design的table組件實(shí)現(xiàn)全選功能以及自定義分頁(yè)
這篇文章主要介紹了ant design的table組件實(shí)現(xiàn)全選功能以及自定義分頁(yè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue返回上一頁(yè)(后退)的幾種方法與區(qū)別說(shuō)明
這篇文章主要介紹了vue返回上一頁(yè)(后退)的幾種方法與區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue3?計(jì)算屬性computed的實(shí)現(xiàn)原理
這篇文章主要介紹了Vue3?計(jì)算屬性computed的實(shí)現(xiàn)原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08

