使用:
1 <resize-box :parameter="parameter" style=" 100%;height: 400px;"> 2 <template v-slot:resizeLeft> 3 <h3>this is left content</h3> 4 </template> 5 <template v-slot:resizeRight> 6 <h3>this is right content</h3> 7 </template> 8 </resize-box> 9 10 11
组件: 12 <template> 13 <div class="resizeBox" style="position: relative"> 14 <div 15 ref="resizeLeft" 16 style="position: absolute;left: 0;top: 0;" :style="parameter.leftBoxStyle"> 17 <slot name="resizeLeft"></slot> 18 <div ref="resizeLine" @mousedown="mousedown" 19 style="cursor: col-resize; height:100%; position: absolute;right: 0;top: 0;z-index: 10" 20 :style="parameter.resizeLineStyle"></div> 21 </div> 22 <div ref="resizeRight" 23 style="position: relative;left: 0;top: 0;margin-left: 200px" :style="parameter.rightBoxStyle"> 24 <slot name="resizeRight"></slot> 25 </div> 26 27 </div> 28 </template> 29 30 <script> 31 export default { 32 name: "resizeBox", 33 props: { 34 parameter: { 35 type: Object, 36 default: () => { 37 return { 38 leftBoxStyle: { 39 "200px", 40 height: "100%", 41 background: "red", 42 }, 43 resizeLineStyle: { 44 "4px", 45 background: "RGBA(0,0,0,0.5)" 46 }, 47 rightBoxStyle: { 48 "100%", 49 height: "100%", 50 background: "yellow", 51 } 52 } 53 } 54 } 55 }, 56 methods: { 57 mousedown(ev) { 58 let _this = this; 59 //给可视区域添加鼠标的移动事件 60 document.onmousemove = function (ev) { 61 //获取鼠标移动时的坐标 62 let x1 = ev.clientX; 63 _this.$refs.resizeLeft.style.width = x1 + 'px'; 64 _this.$refs.resizeRight.style.marginLeft = x1 + 'px'; 65 }; 66 //清除 67 document.onmouseup = function (ev) { 68 document.onmousemove = null; 69 } 70 }, 71 } 72 } 73 </script> 74 75 <style scoped> 76 77 </style>