vue拖拽改變寬度的實(shí)現(xiàn)示例
效果圖
1.封裝組件ResizeBox.vue
<template> <div ref="resize" class="resize"> <div ref="resizeHandle" class="handle-resize" /> <slot /> </div> </template> <script> export default { name: 'ResizeBox', props: { resizeConf: { type: Object, default: () => ({ width: 280, // 初始寬度 widthRange: [100, 500] // 寬度范圍 }) } }, mounted() { this.dragControllerDiv(this.$refs.resize, this.$refs.resizeHandle) }, methods: { dragControllerDiv: function(resizeBox, resizeHandle) { resizeBox.style.width = this.resizeConf.width + 'px' // 鼠標(biāo)按下事件 resizeHandle.onmousedown = e => { const resizeWidth = resizeBox.offsetWidth const startX = e.clientX // 水平坐標(biāo) // 鼠標(biāo)拖動(dòng)事件 document.onmousemove = ev => { const moveX = ev.clientX const moveLen = resizeWidth + (moveX - startX) if (this.resizeConf.widthRange[0] <= moveLen && this.resizeConf.widthRange[1] >= moveLen) { resizeBox.style.width = moveLen + 'px' } } // 鼠標(biāo)松開事件 document.onmouseup = function() { document.onmousemove = null document.onmouseup = null } } } } } </script> <style lang="scss" scoped> .resize { background: #fbfbfb; position: relative; word-wrap: break-word; .handle-resize { cursor: col-resize; position: absolute; right: -2px; width: 6px; height: 50px; border-left: 2px solid #c5c5c5; border-right: 2px solid #c5c5c5; top: calc(50% - 25px); } } </style>
2.組件中使用
<template> <div class="container sa-flex"> <ResizeBox :resize-conf="resizeConf"> 我是左邊我是左邊我是左邊我是左邊我是左邊我是左邊我是左邊我是左邊我是左邊我是左邊 </ResizeBox> <div class="right sa-flex-1"> 我是右邊我是右邊我是右邊我是右邊我是右邊我是右邊我是右邊我是右邊我是右邊我是右邊 </div> </div> </template> <script> import ResizeBox from './ResizeBox.vue' export default { components: { ResizeBox }, data() { return { resizeConf: { width: 280, // 初始寬度 widthRange: [100, 500] // 寬度范圍 } } } } </script> <style lang="scss"> .sa-flex { display: flex; flex-wrap: no-wrap } .sa-flex-1 { flex:1 } .container { min-height: 600px; background: #eee; } </style>
到此這篇關(guān)于vue拖拽改變寬度的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue拖拽改變寬度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iview給radio按鈕組件加點(diǎn)擊事件的實(shí)例
下面小編就為大家?guī)硪黄猧view給radio按鈕組件加點(diǎn)擊事件的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09vue elementUI table 自定義表頭和行合并的實(shí)例代碼
這篇文章主要介紹了vue elementUI table 自定義表頭和行合并的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Vue 使用 Mint UI 實(shí)現(xiàn)左滑刪除效果CellSwipe
這篇文章主要介紹了Vue 使用 Mint UI 實(shí)現(xiàn)左滑刪除效果CellSwipe,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04vue 實(shí)現(xiàn)網(wǎng)頁截圖功能詳解
這篇文章主要介紹了通過vue實(shí)現(xiàn)網(wǎng)頁截圖的功能,有興趣的童鞋可以了解一下2021-11-11electron-vue開發(fā)環(huán)境內(nèi)存泄漏問題匯總
這篇文章主要介紹了electron-vue開發(fā)環(huán)境內(nèi)存泄漏問題匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue學(xué)習(xí)筆記之Vue中css動(dòng)畫原理簡(jiǎn)單示例
這篇文章主要介紹了vue學(xué)習(xí)筆記之Vue中css動(dòng)畫原理,結(jié)合簡(jiǎn)單實(shí)例形式分析了Vue中css樣式變換動(dòng)畫效果實(shí)現(xiàn)原理與相關(guān)操作技巧,需要的朋友可以參考下2020-02-02