vue實(shí)現(xiàn)可拖拽div大小的方法
下面看下vue中可拖拽div大小的方法。
可封裝為全局方法在項(xiàng)目中所需要地方直接調(diào)用(mixins)
方法:
參數(shù):
1.allBox:最外層第div class;
2.leftBox:最左層第div class;
3.resizeBox:中間div class;
4.rightBox:最右層第div class;
leftMin /rightMin 距離左右2側(cè)最小距離;
leftBox,resizeBox,rightBox設(shè)置寬度均需float;
*若是在內(nèi)部容器中,須在最外層添加position: relative,否則會引起跳動,計(jì)算距離錯誤
dragDiv(allBox, leftBox, resizeBox, rightBox, leftMin = 200, rightMin = 200) { ? ? ? ? ?var box = document.getElementsByClassName(allBox); ? ? ? ? ?var left = document.getElementsByClassName(leftBox); ? ? ? ? ?var resize = document.getElementsByClassName(resizeBox); ? ? ? ? ?var right = document.getElementsByClassName(rightBox); ? ? ? ? ?for(let i = 0; i < resize.length; i++) { ? ? ? ? ? ? // 鼠標(biāo)按下事件 ? ? ? ? ? ? resize[i].onmousedown = function(e) { ? ? ? ? ? ? ? ?// 顏色改變提醒 ? ? ? ? ? ? ? ?resize[i].style.background = '#818181'; ? ? ? ? ? ? ? ?var startX = e.clientX; ? ? ? ? ? ? ? ?resize[i].left = resize[i].offsetLeft; ? ? ? ? ? ? ? ?// 鼠標(biāo)拖動事件 ? ? ? ? ? ? ? ?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 < leftMin) moveLen = leftMin; ? ? ? ? ? ? ? ? ? if(moveLen > maxT - rightMin) moveLen = maxT - rightMin; ? ? ? ? ? ? ? ? ? resize[i].style.left = moveLen; // 設(shè)置左側(cè)區(qū)域的寬度 ? ? ? ? ? ? ? ? ? for(let j = 0; j < left.length; j++) { ? ? ? ? ? ? ? ? ? ? ?left[j].style.width = moveLen + 'px'; ? ? ? ? ? ? ? ? ? ? ?right[j].style.width = (box[i].clientWidth - moveLen - 10) + 'px'; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ?}; ? ? ? ? ? ? ? ?// 鼠標(biāo)松開事件 ? ? ? ? ? ? ? ?document.onmouseup = function(evt) { ? ? ? ? ? ? ? ? ? // 顏色恢復(fù) ? ? ? ? ? ? ? ? ? resize[i].style.background = '#d6d6d6'; ? ? ? ? ? ? ? ? ? document.onmousemove = null; ? ? ? ? ? ? ? ? ? document.onmouseup = null; ? ? ? ? ? ? ? ? ? resize[i].releaseCapture && resize[i].releaseCapture(); // 當(dāng)你不在需要繼續(xù)獲得鼠標(biāo)消息就要應(yīng)該調(diào)用ReleaseCapture()釋放掉 ? ? ? ? ? ? ? ?resize[i].setCapture && resize[i].setCapture(); // 該函數(shù)在屬于當(dāng)前線程的指定窗口里設(shè)置鼠標(biāo)捕獲 ? ? ? ? ? ? ? ?return false; ? ? ? ? ? ? }; ? ? ? ? ?} ? ? ? },
補(bǔ)充:vue中的可拖拽寬度div
主要思路
- 在需要拖拽寬度的區(qū)域設(shè)置一個(gè)div,高度設(shè)為 100%,寬度盡量窄一些(也不要太窄,3~6px左右)
- 在此div上綁定當(dāng)“鼠標(biāo)按下”時(shí),觸發(fā)document綁定“鼠標(biāo)移動”方法和"鼠標(biāo)抬起"方法
- 通過鼠標(biāo)移動方法不斷獲取當(dāng)前鼠標(biāo)位置,設(shè)置需要變化大小div的寬高。
- 鼠標(biāo)抬起時(shí)取消鼠標(biāo)移動方法和鼠標(biāo)抬起方法的綁定。
<template> <div class="container" id="content_box"> <div class="tab">左側(cè)Tab</div> <div class="menu" ref="menu"> 左側(cè)菜單 <div class="menu-resize" ref="menuResize"></div> </div> <div class="content"> 中心區(qū)域 <div class="opera" ref="opera"> <div class="opera-resize" ref="operaResize"></div> 操作區(qū)域 </div> </div> </template> <script> export default { name: "dropWidth", mounted() { this.$nextTick(() => { this.dropSize(); }) }, methods: { dropSize() { let that = this, menuWidth = 200, operaHeight = 200; this.$refs.menuResize.onmousedown = function () { document.onmousemove = function (e) { let clientX = e.clientX; // 最大寬度 if(clientX>=330){ clientX = 330; } // 最小寬度 if(clientX<=230){ clientX = 230; // TODO 這里減的是最左側(cè)tab的寬度 menuWidth = clientX - 30; that.$refs.menu.style.width = clientX - 30 +"px"; } document.onmouseup = function () { console.log('當(dāng)前寬度', menuWidth); document.onmousemove = null; document.onmouseup = null; that.releaseCapture && that.releaseCapture() } this.$refs.operaResize.onmousedown = function () { let clientY = e.clientY; console.log(clientY) if(clientY<=100){ clientY = 100; if(clientY>=300){ clientY = 300; operaHeight = clientY; // TODO 這里需要取反向 that.$refs.opera.style.height = 400 - clientY +"px"; console.log('當(dāng)前寬度', operaHeight); } } } </script> <style scoped> .container { width: 1000px; height: 400px; border: 2px solid #dddddd; display: flex; justify-content: center; .tab { width: 30px; height: 100%; background-color: #EC8C32; flex-shrink: 0; flex-grow: 0; .menu { width: 200px; background-color: #AAB6E0; position: relative; .content { width: 100%; .opera { height: 200px; position: absolute; bottom: 0; background-color: #F2BE25; .menu-resize { width: 5px; top: 0; right: 0; cursor: col-resize; .opera-resize { height: 5px; left: 0; cursor: row-resize; </style>
實(shí)現(xiàn)效果
到此這篇關(guān)于vue中的可拖拽寬度div的文章就介紹到這了,更多相關(guān)vue可拖拽寬度div內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue結(jié)合vant實(shí)現(xiàn)聯(lián)動效果
這篇文章主要為大家詳細(xì)介紹了vue結(jié)合vant實(shí)現(xiàn)聯(lián)動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Vue OptionsAPI與CompositionAPI的區(qū)別與使用介紹
OptionsAPI和CompositionAPI是Vue.js框架中兩種不同的組件編寫方式,OptionsAPI通過對象字面量定義組件,以屬性分隔不同功能,響應(yīng)式數(shù)據(jù)通過data屬性定義,本文給大家介紹Vue OptionsAPI與CompositionAPI的區(qū)別,感興趣的朋友一起看看吧2024-10-10前端架構(gòu)vue動態(tài)組件使用基礎(chǔ)教程
這篇文章主要為大家介紹了前端架構(gòu)vue動態(tài)組件使用的基礎(chǔ)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02解決vue-quill-editor上傳內(nèi)容由于圖片是base64的導(dǎo)致字符太長的問題
vue-quill-editor默認(rèn)插入圖片是直接將圖片轉(zhuǎn)為base64再放入內(nèi)容中,如果圖片較多,篇幅太長,就會比較煩惱,接下來通過本文給大家介紹vue-quill-editor上傳內(nèi)容由于圖片是base64的導(dǎo)致字符太長的問題及解決方法,需要的朋友可以參考下2018-08-08Vue?Electron實(shí)現(xiàn)輸入法自動刷字?jǐn)?shù)功能詳解
這篇文章主要介紹了Vue?Electron實(shí)現(xiàn)輸入法自動刷字?jǐn)?shù)功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12vue項(xiàng)目配置同一局域網(wǎng)可使用ip訪問的操作
這篇文章主要介紹了vue項(xiàng)目配置同一局域網(wǎng)可使用ip訪問的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue中實(shí)現(xiàn)子組件相互切換且數(shù)據(jù)不丟失的策略詳解
項(xiàng)目為數(shù)據(jù)報(bào)表,但是一個(gè)父頁面中有很多的子頁面,而且子頁面中不是相互關(guān)聯(lián),但是數(shù)據(jù)又有聯(lián)系,所以本文給大家介紹了vue中如何實(shí)現(xiàn)子組件相互切換,而且數(shù)據(jù)不會丟失,并有詳細(xì)的代碼供大家參考,需要的朋友可以參考下2024-03-03