小程序開(kāi)發(fā)之模態(tài)框組件封裝
本文實(shí)例為大家分享了小程序模態(tài)框組件的封裝具體代碼,供大家參考,具體內(nèi)容如下
一、前言
對(duì)于模態(tài)框肯定大家都知道,諸如:Bootstartp、element-ui、layui等等都有自己的彈出層,并可以之定義內(nèi)容,但是小程序的彈出層原生的太簡(jiǎn)單,那么我們?nèi)绻远x呢?
其實(shí)很簡(jiǎn)單,就是一個(gè)遮罩、一個(gè)view內(nèi)容區(qū)就搞定了!接下來(lái)看一下我自己封裝后的模態(tài)框效果:

感覺(jué)還可以哈!
二、模態(tài)框組件的使用
1.先在使用頁(yè)面的json注冊(cè)該組件
{
"navigationBarTitleText": "XXXX",
"usingComponents": {
"print-popups": "/components/popups/popups"
}
}
2.使用組件
<view>
<print-popups ifOpen='{{modelStatus}}' bind:ifClose='closeModel'>
<view slot='popups_main' class='popups_main'>
<view class='popups_item'>添加新設(shè)備</view>
<view class='popups_item print_name'>
<input class='print_input' value='設(shè)備1' auto-focus />
<view class='print_tip'>(點(diǎn)擊設(shè)備可編輯)</view>
</view>
<view class='popups_item print_id'>
<input placeholder="請(qǐng)輸入新添設(shè)備ID" />
<view class='print_id_check'>可用</view>
</view>
<button class='popups_item print_btn' type='primary' size='mini'>添加</button>
</view>
</print-popups>
</view>
3.添加隱藏/顯示方法
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
modelStatus: false
},
/**
* 點(diǎn)擊按鈕打開(kāi)模態(tài)框
*/
openModel () {
const modelStatus = !this.modelStatus
this.setData({ modelStatus})
},
/**
* 子組件觸發(fā)的事件
*/
closeModel (event) {
console.log(event.detail.value)
this.setData({
modelStatus: event.detail.value
})
}
})
三、模態(tài)框組件源碼
1.Json文件
{
"component": true,
"usingComponents": {}
}
2.wxml文件
需要在阿里圖標(biāo)庫(kù)找一張close.png的圖片。
<!--components/popups/popups.wxml-->
<view wx:if="{{ifOpen}}">
<view class='popups_shade' bindtap='popupsClose'></view>
<view class='popups_content'>
<image src='./images/close.png' class='popups_close' bindtap='popupsClose'></image>
<slot name='popups_main'/>
</view>
</view>
3.wxss文件
/* components/popups/popups.wxss */
/* 遮罩 */
.popups_shade {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 1000;
background: #000;
opacity: 0.7;
overflow: hidden;
}
/* 彈出層內(nèi)容 */
.popups_content {
width: 500rpx;
height: 350rpx;
overflow: hidden;
position: fixed;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
margin: auto;
z-index: 1001;
background: #FAFAFA;
border-radius: 5px;
}
.popups_close {
width: 30rpx;
height: 30rpx;
position: absolute;
right: 3px;
top: 3px;
}
4.js文件
// components/popups/popups.js
Component({
options: {
multipleSlots: true
},
/**
* 組件的屬性列表
*/
properties: {
ifOpen: Boolean
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
},
/**
* 父子組件通信
* 組件的方法列表
*/
methods: {
popupsClose () {
console.log('關(guān)閉彈出層' + this.properties.ifOpen)
this.triggerEvent('ifClose', { value: !this.properties.ifOpen})
}
}
})
四、思路
主要是使用slot插槽可以不需要定義內(nèi)容,可以在使用組件的頁(yè)面自定義,這樣就可以很好提高擴(kuò)展性!
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開(kāi)發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js中Object.defineProperty()方法的不詳解
這篇文章主要介紹了js中Object.defineProperty()方法的不詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
js實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為EXCEL(支持大量數(shù)據(jù)導(dǎo)出)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為EXCEL,支持大量數(shù)據(jù)導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
BootstrapTable+KnockoutJS自定義T4模板快速生成增刪改查頁(yè)面
這篇文章主要介紹了BootstrapTable+KnockoutJS自定義T4模板快速生成增刪改查頁(yè)面 的相關(guān)資料,需要的朋友可以參考下2016-08-08
通過(guò)location.replace禁止瀏覽器后退防止重復(fù)提交
如果用戶(hù)重復(fù)提交事件,然后又后退,這樣可能會(huì)對(duì)某些數(shù)據(jù)產(chǎn)生災(zāi)難性的問(wèn)題。所以今天就向大家介紹一種通過(guò)location.replace禁止瀏覽器后退按鈕的方法2014-09-09
JavaScript代碼調(diào)試方法實(shí)例小結(jié)
這篇文章主要介紹了JavaScript代碼調(diào)試方法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript錯(cuò)誤信息的處理與代碼調(diào)試相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
JavaScript反轉(zhuǎn)數(shù)組常用的4種方法
這篇文章主要給大家介紹了關(guān)于JavaScript反轉(zhuǎn)數(shù)組常用的4種方法,反轉(zhuǎn)數(shù)組可以將數(shù)組中的元素順序顛倒過(guò)來(lái),從而達(dá)到一些特定的需求,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

