[TOC]
>[success] # 正式分裝章節
~~~
1.上個章節講了一下思路,這個章節會封裝一個splitpane的布局組件
~~~
>[danger] ##### 基礎面板
~~~
1.根據上章節封裝一個基礎面板
~~~
~~~
<template>
<div class="split-pane-wrapper">
<div class="pane pane-left" :style="{width:leftOffsetPercent}">
<button @click="handleClick">點擊減少左側寬度</button>
</div>
<div class="pane-trigger-con" :style="{left:triggerLeft, width: `${triggerWidht}px`}"></div>
<div class="pane pane-right" :style="{left:leftOffsetPercent}"></div>
</div>
</template>
<script>
export default {
name: "split-pane",
props:{
triggerWidht:{
type:Number,
default:8,
},
},
data(){
return{
// 在這定義一個值。這樣用戶可以直接指定占比的值
// 在頁面css 布局使用的值 使用計算屬性拼接即可
leftOffset:0.3,
}
},
methods:{
handleClick(){
this.leftOffset -= 0.02
}
},
computed:{
// 動態屬性去拼接生成css 實際需要的代%形式的數據
leftOffsetPercent(){
return `${this.leftOffset * 100}%`
},
triggerLeft(){
return `calc(${this.leftOffset * 100}% - ${this.triggerWidht/2}px)`
},
},
}
</script>
<style lang="less">
.split-pane-wrapper{
width: 100%;
height: 100%;
position: relative;
.pane{
position: absolute;
height: 100%;
top:0;
&-left{
/*width: 30%;*/
background: brown;
}
&-right{
right: 0;
bottom: 0;
/*left: 30%;*/
background: chartreuse;
}
&-trigger-con{
z-index: 100;
height: 100%;
background: red;
position: absolute;
top: 0;
}
}
}
</style>
~~~
>[info] ## 增加事件
~~~
1.接下來需要做通過給紅色條添加事件,可以通過數據去操作布局
2.了解幾個方法'event.pageX' -- 鼠標x軸的坐標距離(準確或是)
3.'this.$refs.outer.getBoundingClientRect()' -- 獲取綁定dom元素的一些
距離瀏覽器的基本信息:
DOMRect {x: 8, y: 8, width: 400, height: 200, top: 8, …}
bottom:208
height:200
left:8
right:408
top:8
width:400
x:8
y:8
3.常見想得到某個元素具體的位置,一般采用鼠標距離減去dom元素距
離瀏覽器距離
4.'event'事件中隱藏的兩個快捷方法'event.srcElement' -- 獲取當前dom
5.'event'事件中隱藏的兩個快捷方法'event.srcElement.getBoundingClientRect()' -- 獲取當前dom位置信息
6.去除css 拖住樣式'user-select: none;'
~~~
>[danger] ##### 綁定鼠標點擊紅條時候觸發事件
~~~
1.想在鼠標點擊的時候觸發一個事件,可以得到紅色條距離dom的位置
,后期根據這個位置去觸發讓左側和右側距離重新計算。
2.點擊事件應該綁定在哪里,如果將點擊事件綁定在紅條區域,會出現
當鼠標移出紅條的位置的時候,整個動態布局停止
'event.target.addEventListener('mousemove',this.handleMousemove)'
3.因此不能寫成上面的寫法,而是應該綁定在整個document上
'document.addEventListener('mousemove',this.handleMousemove)'
4.在點擊事件上,增加的鼠標移動后重新計算的事件分開寫方便維護
5.增加移入事件后,要對應的加入移除事件的后續操作,因此在data
中聲明一個變量'canMove'用來記錄當前點擊還是未點擊來觸
發'handleMousemove' 事件
6.由于'mousemove' 是綁定在全局的,因此'mouseup' 也要綁定全局
7.按照第六條的思路需要在頁面上綁定兩個事件,但實際可以整合在
'@mousedown="handleMousedown" ' 中,這里面已經綁定了一個鼠標
移入事件,因此直接在綁定一個鼠標抬起事件
'handleMousup'
8.當我們計算'leftOffset'也就是左面區域的百分比的時候,鼠標到左側邊框
距離除以整體距離,就會出現一個效果,一瞬間區域發生變化并且鼠標
在整個紅色區域,為了避免這個問題定位計算的時候直接從紅條中心
位置計算,對應代碼地方的g公式如下:
'const offset = event.pageX - this.initOffset + this.triggerWidth / 2 - outerRect.left;'
~~~
~~~
<template>
<div class="split-pane-wrapper" ref="outer">
<div class="pane pane-left" :style="{width:leftOffsetPercent}">
<button @click="handleClick">點擊減少左側寬度</button>
</div>
<div class="pane-trigger-con" @mousedown="handleMousedown" :style="{left:triggerLeft, width: `${triggerWidth}px`}"></div>
<div class="pane pane-right" :style="{left:leftOffsetPercent}"></div>
</div>
</template>
<script>
export default {
name: "split-pane",
props:{
triggerWidth:{
type:Number,
default:8,
},
},
data(){
return{
// 在這定義一個值。這樣用戶可以直接指定占比的值
// 在頁面css 布局使用的值 使用計算屬性拼接即可
leftOffset:0.3,
// 增一個判斷 鼠標移入移除
canMove :false,
// 去除誤差值
initOffset: 0
}
},
methods:{
handleClick(){
this.leftOffset -= 0.02
},
handleMousemove(event){
// pageX 是鼠標距離瀏覽器x軸的距離
// 想獲取 紅條在布局頁面的準確位置需要用鼠標距離 - 容器對瀏覽器的距離
// 因此給 這個外部的div 綁上ref 屬性用來獲取,獲取可以使用getBoundingClientRect()
//console.log(event.pageX -this.$refs.outer.getBoundingClientRect().left)
if(!this.canMove) return
const outerRect = this.$refs.outer.getBoundingClientRect();
// 整個leftOffset 的像素距離其實是其實就是pageX鼠標距離減去最外層整理距離瀏覽器的距離
//根據上面可以得到一個公式就是 event.pageX-outerRect.left
// 但要注意實際上中間區域是蓋住了部分左右區域,也就是說只要我們進行了改變
// 就會自動到整個中間區域的中間位置,因此需要在點擊的時候需要得到整中間區域最右面的位置
// 根據上面得到一個公式減去 this.initOffset也就是扣除無用區域獲得中間區域最右面
// 為了去除因為中間區域覆蓋的影響加上中間區域一半的寬度
const offset = event.pageX - this.initOffset + this.triggerWidth / 2 - outerRect.left;
this.leftOffset = (offset/outerRect.width)
},
handleMousup(){
// 抬起后 重新對canMove 賦值,讓紅條不能移動
this.canMove = false
},
handleMousedown(event){
//event.target.addEventListener('mousemove',this.handleMousemove)
document.addEventListener('mousemove',this.handleMousemove)
document.addEventListener('mouseup',this.handleMousup)
this.initOffset = event.pageX - event.srcElement.getBoundingClientRect().left
this.canMove = true;
},
},
computed:{
// 動態屬性去拼接生成css 實際需要的代%形式的數據
leftOffsetPercent(){
return `${this.leftOffset * 100}%`
},
triggerLeft(){
return `calc(${this.leftOffset * 100}% - ${this.triggerWidth/2}px)`
},
},
}
</script>
<style lang="less">
.split-pane-wrapper{
width: 100%;
height: 100%;
position: relative;
.pane{
position: absolute;
height: 100%;
top:0;
&-left{
/*width: 30%;*/
background: brown;
}
&-right{
right: 0;
bottom: 0;
/*left: 30%;*/
background: chartreuse;
}
&-trigger-con{
z-index: 100;
height: 100%;
background: red;
position: absolute;
top: 0;
}
}
}
</style>
~~~
>[danger] ##### 最后的封裝
~~~
1.上面已經可以實現整體使用,但是需要注意拖拽是有限度的,因此也需要設置拖拽的最大值最小值
2.上面的代碼是我們指定'leftOffset'也是百分比占比,如果讓組件去控制,就要有一個問題,
當我么拖拽的時候會重新改變這個值因此就是涉及到,父傳子和子傳父同時出發,想同時
出發兩種方法在使用組件的時候使用v-model 和 使用語法糖sync
~~~
* 在使用組件的時候
~~~
<template>
<div class="split-pane-con">
<split-pane :value.sync="offset">
<div slot="left">left</div>
<div slot="right">right</div>
</split-pane>
</div>
</template>
<script>
import SplitPane from '_c/split-pane'
export default {
components: {
SplitPane
},
data () {
return {
offset: 0.8
}
},
methods: {
// handleInput (value) {
// this.offset = value
// }
}
}
</script>
<style lang="less">
.split-pane-con{
width: 400px;
height: 200px;
background: papayawhip;
}
</style>
~~~
* 組件封裝代碼
~~~
<template>
<div class="split-pane-wrapper" ref="outer">
<div class="pane pane-left" :style="{ width: leftOffsetPercent, paddingRight: `${this.triggerWidth / 2}px` }">
<slot name="left"></slot>
</div>
<div class="pane-trigger-con" @mousedown="handleMousedown" :style="{ left: triggerLeft, width: `${triggerWidth}px` }"></div>
<div class="pane pane-right" :style="{ left: leftOffsetPercent, paddingLeft: `${this.triggerWidth / 2}px` }">
<slot name="right"></slot>
</div>
</div>
</template>
<script>
export default {
name: 'SplitPane',
props: {
value: {
type: Number,
default: 0.5
},
triggerWidth: {
type: Number,
default: 8
},
min: {
type: Number,
default: 0.1
},
max: {
type: Number,
default: 0.9
}
},
data () {
return {
// leftOffset: 0.3,
canMove: false,
initOffset: 0
}
},
computed: {
leftOffsetPercent () {
return `${this.value * 100}%`
},
triggerLeft () {
return `calc(${this.value * 100}% - ${this.triggerWidth / 2}px)`
}
},
methods: {
handleClick () {
this.leftOffset -= 0.02
},
handleMousedown (event) {
document.addEventListener('mousemove', this.handleMousemove)
document.addEventListener('mouseup', this.handleMouseup)
this.initOffset = event.pageX - event.srcElement.getBoundingClientRect().left
this.canMove = true
},
handleMousemove (event) {
if (!this.canMove) return
const outerRect = this.$refs.outer.getBoundingClientRect()
let offsetPercent = (event.pageX - this.initOffset + this.triggerWidth / 2 - outerRect.left) / outerRect.width
if (offsetPercent < this.min) offsetPercent = this.min
if (offsetPercent > this.max) offsetPercent = this.max
// this.$emit('input', offsetPercent)
this.$emit('update:value', offsetPercent)
},
handleMouseup () {
this.canMove = false
}
}
}
</script>
<style lang="less">
.split-pane-wrapper{
height: 100%;
width: 100%;
position: relative;
.pane{
position: absolute;
top: 0;
height: 100%;
&-left{
// width: 30%;
background: palevioletred;
}
&-right{
right: 0;
bottom: 0;
background: paleturquoise;
}
&-trigger-con{
height: 100%;
background: red;
position: absolute;
top: 0;
z-index: 10;
user-select: none;
cursor: col-resize;
}
}
}
</style>
~~~
- Vue--基礎篇章
- Vue -- 介紹
- Vue -- MVVM
- Vue -- 創建Vue實例
- Vue -- 模板語法
- Vue -- 指令用法
- v-cloak -- 遮蓋
- v-bind -- 標簽屬性動態綁定
- v-on -- 綁定事件
- v-model -- 雙向數據綁定
- v-for -- 只是循環沒那么簡單
- 小知識點 -- 計劃內屬性
- key -- 屬性為什么要加
- 案例說明
- v-if/v-show -- 顯示隱藏
- v-for 和 v-if 同時使用
- v-pre -- 不渲染大大胡語法
- v-once -- 只渲染一次
- Vue -- class和style綁定
- Vue -- filter 過濾器
- Vue--watch/computed/fun
- watch -- 巧妙利用watch思想
- Vue -- 自定義指令
- Vue -- $方法
- Vue--生命周期
- Vue -- 專屬ajax
- Vue -- transition過渡動畫
- 前面章節的案例
- 案例 -- 跑馬燈效果
- 案例 -- 選項卡內容切換
- 案例-- 篩選商品
- 案例 -- 搜索/刪除/更改
- 案例 -- 用computed做多選
- 案例 -- checked 多選
- Vue--組件篇章
- component -- 介紹
- component -- 使用全局組件
- component -- 使用局部組件
- component -- 組件深入
- component -- 組件傳值父傳子
- component -- 組件傳值子傳父
- component -- 子傳父語法糖拆解
- component -- 父組件操作子組件
- component -- is 動態切換組件
- component -- 用v-if/v-show控制子組件
- component -- 組件切換的動畫效果
- component -- slot 插槽
- component -- 插槽2.6
- component -- 組件的生命周期
- component -- 基礎組件全局注冊
- VueRouter--獲取路由參數
- VueRouter -- 介紹路由
- VueRouter -- 安裝
- VueRouter -- 使用
- VueRouter--router-link簡單參數
- VueRouter--router-link樣式問題
- VueRouter--router-view動畫效果
- VueRouter -- 匹配優先級
- vueRouter -- 動態路由
- VueRouter -- 命名路由
- VueRouter -- 命名視圖
- VueRouter--$router 獲取函數
- VueRouter--$route獲取參數
- VueRouter--路由嵌套
- VueRouter -- 導航守衛
- VueRouter -- 寫在最后
- Vue--模塊化方式結構
- webpack--自定義配置
- webpack -- 自定義Vue操作
- VueCli -- 3.0可視化配置
- VueCli -- 3.0 項目目錄
- Vue -- 組件升級篇
- Vue -- 組件種類與組件組成
- Vue -- 組件prop、event、slot 技巧
- Vue -- 組件通信(一)
- Vue -- 組件通信(二)
- Vue -- 組件通信(三)
- Vue -- 組件通信(四)
- Vue -- 組件通信(五)
- Vue -- 組件通信(六)
- Vue -- bus非父子組件通信
- Vue -- 封裝js插件成vue組件
- vue組件分裝 -- 進階篇
- Vue -- 組件封裝splitpane(分割面板)
- UI -- 正式封裝
- Vue -- iview 可編輯表格案例
- Ui -- iview 可以同時編輯多行
- Vue -- 了解遞歸組件
- UI -- 正式使用遞歸菜單
- Vue -- iview Tree組件
- Vue -- 利用通信仿寫一個form驗證
- Vue -- 使用自己的Form
- Vue -- Checkbox 組件
- Vue -- CheckboxGroup.vue
- Vue -- Alert 組件
- Vue -- 手動掛載組件
- Vue -- Alert開始封裝
- Vue -- 動態表單組件
- Vue -- Vuex組件的狀態管理
- Vuex -- 參數使用理解
- Vuex -- state擴展
- Vuex -- getters擴展
- Vuex--mutations擴展
- Vuex -- Action 異步
- Vuex -- plugins插件
- Vuex -- v-model寫法
- Vuex -- 更多
- VueCli -- 技巧總結篇
- CLI -- 路由基礎
- CLI -- 路由升級篇
- CLI --異步axios
- axios -- 封裝axios
- CLI -- 登錄寫法
- CLI -- 權限
- CLI -- 簡單權限
- CLI -- 動態路由加載
- CLI -- 數據性能優化
- ES6 -- 類的概念
- ES6類 -- 基礎
- ES6 -- 繼承
- ES6 -- 工作實戰用類數據管理
- JS -- 適配器模式
- ES7 -- 裝飾器(Decorator)
- 裝飾器 -- 裝飾器修飾類
- 裝飾器--修飾類方法(知識擴展)
- 裝飾器 -- 裝飾器修飾類中的方法
- 裝飾器 -- 執行順序
- Reflect -- es6 自帶版本
- Reflect -- reflect-metadata 版本
- 實戰 -- 驗證篇章(基礎)
- 驗證篇章 -- 搭建和目錄
- 驗證篇章 -- 創建基本模板
- 驗證篇章 -- 使用
- 實戰 -- 更新模型(為了迎合ui升級)
- 實戰 -- 模型與接口對接
- TypeSprict -- 基礎篇章
- TS-- 搭建(一)webpack版本
- TS -- 搭建(二)直接使用
- TS -- 基礎類型
- TS -- 枚舉類型
- TS -- Symbol
- TS -- interface 接口
- TS -- 函數
- TS -- 泛型
- TS -- 類
- TS -- 類型推論和兼容
- TS -- 高級類型(一)
- TS -- 高級類型(二)
- TS -- 關于模塊解析
- TS -- 聲明合并
- TS -- 混入
- Vue -- TS項目模擬
- TS -- vue和以前代碼對比
- TS -- vue簡單案例上手
- Vue -- 簡單弄懂VueRouter過程
- VueRouter -- 實現簡單Router
- Vue-- 原理2.x源碼簡單理解
- 了解 -- 簡單的響應式工作原理
- 準備工作 -- 了解發布訂閱和觀察者模式
- 了解 -- 響應式工作原理(一)
- 了解 -- 響應式工作原理(二)
- 手寫 -- 簡單的vue數據響應(一)
- 手寫 -- 簡單的vue數據響應(二)
- 模板引擎可以做的
- 了解 -- 虛擬DOM
- 虛擬dom -- 使用Snabbdom
- 閱讀 -- Snabbdom
- 分析snabbdom源碼 -- h函數
- 分析snabbdom -- init 方法
- init 方法 -- patch方法分析(一)
- init 方法 -- patch方法分析(二)
- init方法 -- patch方法分析(三)
- 手寫 -- 簡單的虛擬dom渲染
- 函數表達解析 - h 和 create-element
- dom操作 -- patch.js
- Vue -- 完成一個minVue
- minVue -- 打包入口
- Vue -- new實例做了什么
- Vue -- $mount 模板編譯階段
- 模板編譯 -- 分析入口
- 模板編譯 -- 分析模板轉譯
- Vue -- mountComponent 掛載階段
- 掛載階段 -- vm._render()
- 掛載階段 -- vnode
- 備份章節
- Vue -- Nuxt.js
- Vue3 -- 學習
- Vue3.x --基本功能快速預覽
- Vue3.x -- createApp
- Vue3.x -- 生命周期
- Vue3.x -- 組件
- vue3.x -- 異步組件???
- vue3.x -- Teleport???
- vue3.x -- 動畫章節 ??
- vue3.x -- 自定義指令 ???
- 深入響應性原理 ???
- vue3.x -- Option API VS Composition API
- Vue3.x -- 使用set up
- Vue3.x -- 響應性API
- 其他 Api 使用
- 計算屬性和監聽屬性
- 生命周期
- 小的案例(一)
- 小的案例(二)-- 泛型
- Vue2.x => Vue3.x 導讀
- v-for 中的 Ref 數組 -- 非兼容
- 異步組件
- attribute 強制行為 -- 非兼容
- $attrs 包括 class & style -- 非兼容
- $children -- 移除
- 自定義指令 -- 非兼容
- 自定義元素交互 -- 非兼容
- Data選項 -- 非兼容
- emits Option -- 新增
- 事件 API -- 非兼容
- 過濾器 -- 移除
- 片段 -- 新增
- 函數式組件 -- 非兼容
- 全局 API -- 非兼容
- 全局 API Treeshaking -- 非兼容
- 內聯模板 Attribute -- 非兼容
- key attribute -- 非兼容
- 按鍵修飾符 -- 非兼容
- 移除 $listeners 和 v-on.native -- 非兼容
- 在 prop 的默認函數中訪問 this -- ??
- 組件使用 v-model -- 非兼容
- 渲染函數 API -- ??
- Slot 統一 ??
- 過渡的 class 名更改 ???
- Transition Group 根元素 -- ??
- v-if 與 v-for 的優先級對比 -- 非兼容
- v-bind 合并行為 非兼容
- 監聽數組 -- 非兼容