微信小程序 彈窗輸入組件的實(shí)現(xiàn)解析
寫項(xiàng)目的時(shí)候發(fā)現(xiàn)小程序沒(méi)有自帶的彈窗輸入的組件,只能自己寫一個(gè)。
1.半透明的遮蓋層
遮蓋層的樣式和顯隱事件
wxml代碼:
<view class="body">
<button bindtap='eject'>彈窗</button>
</view>
<view class="model" catchtouchmove='preventTouchMove' wx:if='{{showModal}}'></view>
wxss代碼:
.model{
position: absolute;
width: 100%;
height: 100%;
background: #000;
z-index: 999;
opacity: 0.5;
top: 0;
left:0;
}
js代碼:
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
showModal: false,
},
/**
* 控制遮蓋層的顯示
*/
eject:function(){
this.setData({
showModal:true
})
}
2.彈窗窗口實(shí)現(xiàn)
彈窗窗口的樣式和顯隱事件
wxml代碼:
<view class="modalDlg" catchtouchmove='preventTouchMove' wx:if='{{showModal}}'>
<view class='windowRow'>
<text class='userTitle'>標(biāo)題
</text>
<view class='back' bindtap='back'>
返回
</view>
</view>
<view class='wishName'>
<input bindinput='wish_put' placeholder='請(qǐng)輸入內(nèi)容' class='wish_put'></input>
</view>
<view class='wishbnt'>
<button class='wishbnt_bt' bindtap='ok'>確定</button>
</view>
</view>
wxss代碼:
.modalDlg{
width: 70%;
position: fixed;
top:350rpx;
left: 0;
right: 0;
z-index: 9999;
margin: 0 auto;
background-color: #fff;
border-radius: 10rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.windowRow{
display: flex;
flex-direction: row;
justify-content: space-between;
height: 110rpx;
width: 100%;
}
.back{
text-align: center;
color: #f7a6a2;
font-size: 30rpx;
margin: 30rpx;
}
.userTitle{
font-size: 30rpx;
color: #666;
margin: 30rpx;
}
.wishName{
width: 100%;
justify-content: center;
flex-direction: row;
display: flex;
margin-bottom: 30rpx;
}
.wish_put{
width: 80%;
border: 1px solid;
border-radius: 10rpx;
padding-left: 10rpx;
}
.wishbnt{
width: 100%;
font-size: 30rpx;
margin-bottom: 30rpx;
}
.wishbnt_bt{
width: 50%;
background-color: #f7a6a2;
color: #fbf1e8;
font-size: 30rpx;
border: 0;
}
js代碼:
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
showModal: false,
textV:''
},
/**
* 控制顯示
*/
eject:function(){
this.setData({
showModal:true
})
},
/**
* 點(diǎn)擊返回按鈕隱藏
*/
back:function(){
this.setData({
showModal:false
})
},
/**
* 獲取input輸入值
*/
wish_put:function(e){
this.setData({
textV:e.detail.value
})
},
/**
* 點(diǎn)擊確定按鈕獲取input值并且關(guān)閉彈窗
*/
ok:function(){
console.log(this.data.textV)
this.setData({
showModal:false
})
}
3.完整代碼
最后獻(xiàn)上完整代碼,有點(diǎn)啰嗦了,想盡量詳細(xì)點(diǎn)
wxml代碼:
<view class="body">
<button bindtap='eject'>彈窗</button>
</view>
<view class="model" catchtouchmove='preventTouchMove' wx:if='{{showModal}}'></view>
<view class="modalDlg" catchtouchmove='preventTouchMove' wx:if='{{showModal}}'>
<view class='windowRow'>
<text class='userTitle'>標(biāo)題
</text>
<view class='back' bindtap='back'>
返回
</view>
</view>
<view class='wishName'>
<input bindinput='wish_put' placeholder='請(qǐng)輸入內(nèi)容' class='wish_put'></input>
</view>
<view class='wishbnt'>
<button class='wishbnt_bt' bindtap='ok'>確定</button>
</view>
</view>
wxss代碼:
.body{
width: 100%;
height: 100%;
background-color: #fff;
position: fixed;
display: flex;
}
.body button{
height: 100rpx;
}
.model{
position: absolute;
width: 100%;
height: 100%;
background: #000;
z-index: 999;
opacity: 0.5;
top: 0;
left:0;
}
.modalDlg{
width: 70%;
position: fixed;
top:350rpx;
left: 0;
right: 0;
z-index: 9999;
margin: 0 auto;
background-color: #fff;
border-radius: 10rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.windowRow{
display: flex;
flex-direction: row;
justify-content: space-between;
height: 110rpx;
width: 100%;
}
.back{
text-align: center;
color: #f7a6a2;
font-size: 30rpx;
margin: 30rpx;
}
.userTitle{
font-size: 30rpx;
color: #666;
margin: 30rpx;
}
.wishName{
width: 100%;
justify-content: center;
flex-direction: row;
display: flex;
margin-bottom: 30rpx;
}
.wish_put{
width: 80%;
border: 1px solid;
border-radius: 10rpx;
padding-left: 10rpx;
}
.wishbnt{
width: 100%;
font-size: 30rpx;
margin-bottom: 30rpx;
}
.wishbnt_bt{
width: 50%;
background-color: #f7a6a2;
color: #fbf1e8;
font-size: 30rpx;
border: 0;
}
js代碼:
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
showModal: false,
textV:''
},
/**
* 控制顯示
*/
eject:function(){
this.setData({
showModal:true
})
},
/**
* 點(diǎn)擊返回按鈕隱藏
*/
back:function(){
this.setData({
showModal:false
})
},
/**
* 獲取input輸入值
*/
wish_put:function(e){
this.setData({
textV:e.detail.value
})
},
/**
* 點(diǎn)擊確定按鈕獲取input值并且關(guān)閉彈窗
*/
ok:function(){
console.log(this.data.textV)
this.setData({
showModal:false
})
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Javascript數(shù)組的?splice?方法詳細(xì)介紹
這篇文章主要介紹了Javascript數(shù)組的splice方法詳細(xì)介紹,splice方法通過(guò)刪除或替換現(xiàn)有元素或者原地添加新的元素來(lái)修改數(shù)組,并以數(shù)組形式返回被修改的內(nèi)容。此方法會(huì)改變?cè)瓟?shù)組2022-09-09
JavaScript中常見的高階函數(shù)總結(jié)
JavaScript的函數(shù)其實(shí)都指向某個(gè)變量,既然變量可以指向函數(shù),函數(shù)的參數(shù)能接收變量,那么一個(gè)函數(shù)就可以接收另一個(gè)函數(shù)作為參數(shù),這種函數(shù)就稱之為高階函數(shù),這篇文章主要給大家介紹了關(guān)于JavaScript中常見的高階函數(shù),需要的朋友可以參考下2022-02-02
bootstrap是什么_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了bootstrap是什么,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Bootstrap每天必學(xué)之標(biāo)簽與徽章
Bootstrap每天必學(xué)之標(biāo)簽與徽章,對(duì)Bootstrap標(biāo)簽與徽章小編也了解的很少,希望通過(guò)這篇文章和大家更多的去學(xué)習(xí)Bootstrap標(biāo)簽與徽章,從中得到收獲。2015-11-11
使用 UniApp 實(shí)現(xiàn)小程序的微信登錄功能
這篇文章主要介紹了使用 UniApp 實(shí)現(xiàn)小程序的微信登錄功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

