vue 2.0封裝model組件的方法
本文介紹了vue 2.0封裝model組件的方法,分享給大家,希望對(duì)大家有所幫助
單文件組件
使用單文件組件封裝model的模板、邏輯和樣式,之后就可以在頁(yè)面中調(diào)用此通用組件。

需求
model有兩個(gè)使用場(chǎng)景:
1、備注/修改密碼(簡(jiǎn)易):
在屏幕中垂直居中
2、添加/修改/展示信息(復(fù)雜):
距離屏幕頂部200px,內(nèi)容過(guò)長(zhǎng)時(shí)可滾動(dòng)。
3、常規(guī)要求(共同點(diǎn)):
標(biāo)題,關(guān)閉icon
點(diǎn)擊確定/關(guān)閉/遮罩,隱藏model組件
分析上述需求點(diǎn),得到如下圖:

- wrapper:負(fù)責(zé)遮蓋屏幕
- inner:負(fù)責(zé)垂直居中/距頂部200px
- title:可變化標(biāo)題
- content:可變化的內(nèi)容區(qū)域
方案
1、Prop傳參
title(標(biāo)題)、show(隱藏/顯示)、width(寬度)、type(居中/頂部)
2、自定義事件
關(guān)閉model
3、slot分發(fā)
內(nèi)容區(qū)域可自定義
4、滾動(dòng)穿透
具體實(shí)現(xiàn)
template
<div class="model-mask" v-show="show">
<div :class="[type === 'top' ? 'model-wrapper-top' : 'model-wrapper']" @click="close">
<div :class="[type === 'top' ? 'model-container-top' : 'model-container']"
:style="{width:width + 'px'}">
<div class="model-header">
<span>{{title}}</span>
<i class="close-empty" @click="close">
<Icon
type="ivu-icon ivu-icon-ios-close-empty"
size="25" />
</i>
</div>
<div class="model-body">
<slot></slot>
</div>
</div>
</div>
</div>
script
export default {
name: 'MyModel',
props:
{
title: String,
show: Boolean,
width: Number,
type: String
},
data () {
return {
scrollTop: ''
}
},
watch: {
show: function (val, oldVal) {
function getScrollTop () {
return document.body.scrollTop || document.documentElement.scrollTop
}
if (val) {
// 在彈出層顯示之前,記錄當(dāng)前的滾動(dòng)位置
this.scrollTop = getScrollTop()
let body = document.querySelector('body')
body.className = 'not-scroll'
// 把脫離文檔流的body拉上去!否則頁(yè)面會(huì)回到頂部!
document.body.style.top = -this.scrollTop + 'px'
}
}
},
methods: {
close: function (e) {
function to (scrollTop) {
document.body.scrollTop = document.documentElement.scrollTop = scrollTop
}
let target = e.srcElement || e.target
if (target.className === 'model-wrapper' ||
target.className.indexOf('ivu-icon-ios-close-empty') > -1 ||
target.className === 'model-wrapper-top') {
this.$emit('close')
let body = document.querySelector('body')
body.className = ''
// 滾回到老地方!
to(this.scrollTop)
}
}
}
}
style
<style scoped lang="scss">
.model-mask {
height: 100%;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: 1000;
background: rgba(0, 0, 0, .5);
}
/**
* 垂直居中
*/
.model-wrapper {
height: 100%;
text-align: center;
}
.model-wrapper:before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
.model-container {
position: relative;
display: inline-block;
vertical-align: middle;
background-color: white;
text-align: left;
box-shadow: 0 5px 14px 0 rgba(0,0,0,0.15);
border-radius: 6px;
overflow: hidden;
z-index: 1050;
}
/**
* 距離頂部100px,可滾動(dòng)
*/
.model-wrapper-top {
position: relative;
height: 100%;
overflow-x: hidden;
overflow-y: scroll;
}
.model-container-top {
margin: 100px auto;
background-color: white;
text-align: left;
box-shadow: 0 5px 14px 0 rgba(0,0,0,0.15);
border-radius: 6px;
overflow: hidden;
}
.close-empty {
position: absolute;
right: 16px;
top: 10px;
overflow: hidden;
cursor: pointer;
z-index: 1100;
}
.model-header {
position: relative;
height: 45px;
line-height: 45px;
padding: 0 20px;
font-size: 14px;
color: #999;
border-bottom: 1px solid #eee;
}
</style>
引用
<button type="button" @click="showModel">戳我呀</button>
import MyModel from '../componets/model.vue'
export default {
name: 'test',
components: {
MyModel
},
data () {
return {
show: false
}
},
methods: {
/**
* 打開model
*/
closeModel: function () {
this.show = false
},
/**
* 關(guān)閉model
*/
showModel: function () {
this.show = true
}
}
}
引用一
<my-model title="標(biāo)題" :width="400" :show="show" v-on:close="closeModel">
<!-- slot -->
<div class="tips">
<p>this is content area。</p>
</div>
</my-model>
引用二
<my-model type="top" title="標(biāo)題" :width="400" :show="show" v-on:close="closeModel">
<!-- slot -->
<div class="tips">
<p v-for="i in 50">this is content area。</p>
</div>
</my-model>
demo
垂直居中

距頂部200px,可滾動(dòng)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue2自定義組件通過(guò)rollup配置發(fā)布到npm的詳細(xì)步驟
這篇文章主要介紹了vue2自定義組件通過(guò)rollup配置發(fā)布到npm,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Vue計(jì)算屬性實(shí)現(xiàn)成績(jī)單
這篇文章主要為大家詳細(xì)介紹了Vue計(jì)算屬性實(shí)現(xiàn)成績(jī)單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
vue項(xiàng)目webpack中Npm傳遞參數(shù)配置不同域名接口
這篇文章主要介紹了vue項(xiàng)目webpack中Npm傳遞參數(shù)配置不同域名接口,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Vue表單驗(yàn)證?trigger:'blur'OR?trigger:'change&ap
利用?elementUI?實(shí)現(xiàn)表單元素校驗(yàn)時(shí),出現(xiàn)下拉框內(nèi)容選中后校驗(yàn)不消失的異常校驗(yàn)情形,這篇文章主要介紹了Vue表單驗(yàn)證?trigger:‘blur‘?OR?trigger:‘change‘?區(qū)別,需要的朋友可以參考下2023-09-09
Vue.js?的計(jì)算屬性和偵聽(tīng)器詳解(提升數(shù)據(jù)處理與交互的關(guān)鍵工具)
在Vue.js開發(fā)中,計(jì)算屬性與偵聽(tīng)器是極為關(guān)鍵的特性,它們極大地提升了數(shù)據(jù)處理與交互邏輯實(shí)現(xiàn)的便捷性和高效性,本文給大家介紹Vue.js?的計(jì)算屬性和偵聽(tīng)器:提升數(shù)據(jù)處理與交互的關(guān)鍵工具,感興趣的朋友一起看看吧2025-04-04

