
按住分割線調(diào)節(jié)比例
實現(xiàn)并不難,但是網(wǎng)上其他的文章實在是把簡單的事情復雜化了
今天教大家的方法超級簡潔?。。。≈挥?0行代碼??!
當鼠標在分割線位置按下時
triggerDragging變量變?yōu)閠rue
這時split-pane-wrapper的mousemove中判斷triggerDragging
如果為true則改變leftOffset,pane-left的width就會隨之改變
pane-trigger-con的width是固定的
而pane-right采用了彈性布局的flex: 1;
使其填充剩余部分,填充部分適合用于網(wǎng)頁的內(nèi)容展示部分
<template>
<div class="split-pane-wrapper" @mousemove="mouseMoveTrigger">
<div class="pane-left" :style="{ width: leftOffset + 'px' }"></div>
<div class="pane-trigger-con" @mousedown="mouseDownTrigger"></div>
<div class="pane-right"></div>
</div>
</template>
<script>
export default {
name: "Home",
data() {
return {
leftOffset: 300,
triggerDragging: false,
};
},
methods: {
mouseMoveTrigger(event) {
if (!event.which) this.triggerDragging = false;
if (this.triggerDragging) {
this.leftOffset = event.clientX;
}
},
mouseDownTrigger(event) {
this.triggerDragging = true;
},
},
};
</script>
<style scoped>
.split-pane-wrapper {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
}
.pane-left {
background: brown;
}
.pane-right {
flex: 1;
background: chartreuse;
}
.pane-trigger-con {
width: 8px;
background: red;
cursor: ew-resize;
}
</style>
2022-8-24
記得關注??