在小程序中弹出框的应用场景还是不小的,那么我们改如何写一个出来呢?
首先我们需要的是一个组件和使用组件的page
现来看组件
json
{
"component": true,
"usingComponents": {}
}
很显然这就是一个组件初始化的json
然后是组件的js文件
Component({
properties: {
master:{
type:Boolean
}
},
/**
* 组件的初始数据
*/
data: {
},
methods: {
closeImage(){
this.triggerEvent('closeImage',{},{})
}
}
})
master变量用于控制弹出框的显示,closeImage是父级传来的关闭弹出框方法
组件wxml
<view class="wx-popup" hidden="{{master}}">
<view class = "rhythm">
<view class = "close" bind:tap = "closeImage">X</view>
<image src = "http://file.lanxili.vip/LanxiLiMall/20210317/54777e3c642c45729e02efe5d9c91b30.jpg"/>
</view>
</view>
最后是我们的wxss样式
.wx-popup{
position: fixed;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, .5);
}
.wx-popup>.rhythm{
height:80vw;
width:80vw;
margin:auto;
position:absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
}
.wx-popup>.rhythm>image{
height:100%;
width:100%;
}
.wx-popup>.rhythm>.close{
padding:10rpx 20rpx;
font-size:28rpx;
position:absolute;
right: 0;
top: 0;
color:#FFFFFF;
}
最后我们需要在page中引入组件
{
"usingComponents": {
"popup": "../components/modal/modal"
}
}
然后js中传入关闭组件方法和控制组件的变量
Page({
data: {
master:true
},
closeImage:function(){
this.setData({
master:true
})
}
})
wxml
<popup master = "{{master}}" bind:closeImage = "closeImage"></popup>
相信做过pc端vue项目的都会很熟悉这个玩法