pre { overflow-y: auto; max-height: 500px }

github地址: https://github.com/lxmghct/my-vue-components

组件介绍

  • props:

    • splitCount: 分割数量, default: 2
    • direction: 分割方向, 'vertical' or 'horizontal', default: 'horizontal'
    • defaultRatio: 默认比例, 类型为数组, default: [1/spiltCount, 1/spiltCount, ...]
  • slots:
    • ...
    • ...
    • ...
  • events:
    • @resize: 拖动分割条时触发, 参数为分割线两侧的div
    • @resize-stop: 拖动分割条结束时触发
  • methods:
    • changeItemSize(index, itemSize, dire='next') 改变第item个pane的大小, dire为next或prev, 表示修改当前pane时连带修改前一个pane还是后一个

效果展示

设计思路

整个组件采用flex布局,通过设置整体的flex-direction控制分割方向,通过修改每个pane的style.flex控制每个pane的大小。

<div class="split-main" ref="splitMain"
:class="direction === 'vertical' ? 'split-vertical' : 'split-horizontal'">
<template v-if="direction === 'vertical'">
<div v-for="i in splitCount" :key="i" ref="splitItem"
class="split-vertical-item">
<div class="split-vertical-line" v-if="i < splitCount"
@mousedown="_startDrag(i)"
@touchstart="_startDrag(i)"></div>
<div class="split-vertical-content">
<slot :name="`pane${i}`"></slot>
</div>
</div>
</template>
<template v-else>
<div v-for="i in splitCount" :key="i" ref="splitItem"
class="split-horizontal-item">
<div class="split-horizontal-line" v-if="i < splitCount"
@mousedown="_startDrag(i)"
@touchstart="_startDrag(i)"></div>
<div class="split-horizontal-content">
<slot :name="`pane${i}`"></slot>
</div>
</div>
</template>
</div>

通过v-for循环生成分割数量的pane,每个pane中间插入分割线,分割线通过@mousedown@touchstart事件绑定_startDrag方法,该方法用于监听鼠标或手指的移动事件,从而实现拖动分割线改变pane大小的功能。

_startDrag (index) {
this.dragIndex = index - 1
},
_onMouseMove (e) {
if (this.dragIndex === -1) {
return
}
let items = this.$refs.splitItem
let item1 = items[this.dragIndex]
let item2 = items[this.dragIndex + 1]
let rect1 = item1.getBoundingClientRect()
let rect2 = item2.getBoundingClientRect()
let ratio1, ratio2
let minLen = this.minLen
if (this.direction === 'vertical') {
let height = this.$refs.splitMain.clientHeight
let tempY = e.clientY - rect1.top > minLen ? e.clientY : rect1.top + minLen
tempY = rect2.bottom - tempY > minLen ? tempY : rect2.bottom - minLen
ratio1 = (tempY - rect1.top) / height
ratio2 = (rect2.bottom - tempY) / height
} else {
let width = this.$refs.splitMain.clientWidth
let tempX = e.clientX - rect1.left > minLen ? e.clientX : rect1.left + minLen
tempX = rect2.right - tempX > minLen ? tempX : rect2.right - minLen
ratio1 = (tempX - rect1.left) / width
ratio2 = (rect2.right - tempX) / width
}
item1.style.flex = ratio1
item2.style.flex = ratio2
e.preventDefault()
this.$emit('resize', item1, item2)
},
_onMouseUp () {
if (this.dragIndex === -1) {
return
}
this.dragIndex = -1
this.$emit('resize-stop')
}

完整代码在github上。https://github.com/lxmghct/my-vue-components

vue自定义组件——split-pane的更多相关文章

  1. vue自定义组件(vue.use(),install)+全局组件+局部组件

    相信大家都用过element-ui.mintui.iview等诸如此类的组件库,具体用法请参考:https://www.cnblogs.com/wangtong111/p/11522520.html ...

  2. Vue自定义组件实现v-model指令

    Tips: 本文所描述的Vue均默认是Vue2版本 在我们初次接触Vue的时候,一定会了解到一个语法糖,那就是v-model指令,它带给我们的第一印象就是它可以实现双向绑定 那么,什么是双向绑定?通俗 ...

  3. [转] vue自定义组件(通过Vue.use()来使用)即install的使用

    在vue项目中,我们可以自定义组件,像element-ui一样使用Vue.use()方法来使用,具体实现方法: 1.首先新建一个Cmponent.vue文件 // Cmponent.vue<te ...

  4. vue 自定义组件销毁

    今天在开发电商vue前端项目时,用户每次登出再换其它用户登录时,页面显示的用户名和左则导航都还是上个用户的,刚开始以为是localStorage中没有清除全局数据,然后在用户点击退出系统时手动清除lo ...

  5. Vue自定义组件插入值

    我们自定义组件的时候有时候需要往组件里面插一些内容: //定义一个组件test,插值内容用slog来代替 export default { name: 'test', template:` <d ...

  6. VUE 自定义组件之间的相互通信

    一.自定义组件 1.全局自定义组件 我们在var vm = new Vue({});的上面并列写上Vue.component('自定义组件名',{组件对象});来完成全局自定义组件的声明.示例代码如下 ...

  7. vue自定义组件并使用

    以下是使用自己写的一个简单的文件上传框为例 1.自定义组件结构(一个js文件,一个vue文件),最好单独放一个文件 2.upload.vue 内容 其中,action是父组件传递给子组件的参数,使用p ...

  8. vue自定义组件中的v-model简单解释

    在使用iview框架的时候,经常会看到组件用v-model双向绑定数据,与传统步骤父组件通过props传值子组件,子组件发送$emit来修改值相比,这种方式避免操作子组件的同时再操作父组件,显得子组件 ...

  9. 8、VUE自定义组件

    1.为什么要使用自定义组件? 自定义组件是用来封装复杂的内容,提高可重用性,比如封装复杂的表格组件.日历组件.图片轮播组件等. 2.自定义组件 2.1. 全局组件 全局组件是每个Vue对象都能使用的组 ...

  10. Vue自定义组件以及组件通信的几种方式

    本帖子来源:小贤笔记 功能 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它 ...

随机推荐

  1. PostgreSQL备份与恢复命令

    postgresql备份与恢复相关命令 --备份用户的数据库bct的所有内容pg_dump -U 用户名 -d 库名 -f xxxXXXxxx.sql--删除原有数据库dropdb -U 用户名 -f ...

  2. renren-fast-vue人人开源前端项目搭建保姆级教程

    1.从gitee上clone项目 git clone https://gitee.com/renrenio/renren-fast-vue.git 2.准备好python环境 需要有Python2以上 ...

  3. 【转载】Fisher精确检验的通俗理解

    一直以来都很欣赏把东西讲得通俗简单的文章,今天碰巧翻到一篇, 讲Fisher检验的,内容虽然不深,但是体验很好,能感受到作者想方设法想要读者明白的那种心思~ 原文在这里: https://blog.c ...

  4. (十三).CSS3中的变换(transform),过渡(transition),动画(animation)

    1 变换 transform 1.1 变换相关 CSS 属性 CSS 属性名 含义 值 transform 设置变换方式 transform-origin 设置变换的原点 使用关键字或坐标设置位置 t ...

  5. Python操作数据库读书笔记

    SQLite 简介 什么是 SQLite? SQLite是一个进程内的库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.它是一个零配置的数据库,这意味着与其他数据库一样,您不需 ...

  6. Visual Studio 2019 专业版许可证过期解决办法

    Visual Studio 2019 许可证过期,登录微软账户也不行,一直提示点击更新许可证,"无法下载许可证,请检查你的网络连接或代理设置" 解决方案:找到VS2019安装目录, ...

  7. 用友CELL

    Cell1.login('晋业软件','1101010373','1300-0616-0122-3005'); 下载地址: https://download.csdn.net/download/wil ...

  8. (Linux服务器)git添加SSH公钥后本地验证失败

    环境:腾讯云Ubuntu x86_64 问题说明: 在配置了公钥后,一直提示我git@github.com: Permission denied (publickey). 解决办法: 先查看root/ ...

  9. C# DataTable操作,转载

    DataTable 排序   DataRow[] rows = dataTable1.Select("", "ord asc");   DataTable t ...

  10. 使用JAX构建强化学习agent并借助TensorFlowLite将其部署到Android应用中

    在之前发布文章<一个新 TensorFlow Lite 示例应用:棋盘游戏>中,展示了如何使用 TensorFlow 和 TensorFlow Agents 来训练强化学习 (RL) ag ...