vue拖拽改變寬度的實現(xiàn)示例
更新時間:2023年07月31日 11:51:58 作者:九億宅男的夢
本文主要介紹了vue拖拽改變寬度的實現(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'
// 鼠標按下事件
resizeHandle.onmousedown = e => {
const resizeWidth = resizeBox.offsetWidth
const startX = e.clientX // 水平坐標
// 鼠標拖動事件
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'
}
}
// 鼠標松開事件
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拖拽改變寬度的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue拖拽改變寬度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue elementUI table 自定義表頭和行合并的實例代碼
這篇文章主要介紹了vue elementUI table 自定義表頭和行合并的實例代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
Vue 使用 Mint UI 實現(xiàn)左滑刪除效果CellSwipe
這篇文章主要介紹了Vue 使用 Mint UI 實現(xiàn)左滑刪除效果CellSwipe,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04
electron-vue開發(fā)環(huán)境內(nèi)存泄漏問題匯總
這篇文章主要介紹了electron-vue開發(fā)環(huán)境內(nèi)存泄漏問題匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10

