微信小程序 彈窗輸入組件的實(shí)現(xiàn)解析
寫(xiě)項(xiàng)目的時(shí)候發(fā)現(xiàn)小程序沒(méi)有自帶的彈窗輸入的組件,只能自己寫(xiě)一個(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中的Web worker多線程API研究
這篇文章主要介紹了JavaScript中的Web worker多線程API研究,Web worker是HTML5的API,允許網(wǎng)頁(yè)在安全的情況下執(zhí)行多線程代碼,需要的朋友可以參考下2014-12-12原生JavaScript實(shí)現(xiàn)購(gòu)物車(chē)
這篇文章主要為大家詳細(xì)介紹了原生JavaScript實(shí)現(xiàn)購(gòu)物車(chē),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01js為鼠標(biāo)添加右擊事件防止默認(rèn)的右擊菜單彈出
本文為大家介紹下如何為使用js為鼠標(biāo)添加右擊事件防止默認(rèn)的右擊菜單彈出,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-07-07純js實(shí)現(xiàn)倒計(jì)時(shí)功能
本文主要介紹了通過(guò)js實(shí)現(xiàn)頁(yè)面的倒計(jì)時(shí)功能的思路與方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01javascript使用閉包模擬對(duì)象的私有屬性和方法
本文給大家簡(jiǎn)單介紹了在一個(gè)項(xiàng)目中涉及到的javascript使用閉包模擬對(duì)象的私有屬性和方法,這里記錄下來(lái),分享給大家。2016-10-10前端百度地圖添加點(diǎn)并跳轉(zhuǎn)到百度地圖進(jìn)行導(dǎo)航完整代碼
web開(kāi)發(fā)過(guò)程中經(jīng)常碰到需要調(diào)用百度地圖來(lái)視線定位導(dǎo)航的過(guò)程,許多技術(shù)博客上介紹的都是調(diào)用百度地圖的api,這篇文章主要給大家介紹了關(guān)于前端百度地圖添加點(diǎn)并跳轉(zhuǎn)到百度地圖進(jìn)行導(dǎo)航的相關(guān)資料,需要的朋友可以參考下2024-07-07layui異步加載table表中某一列數(shù)據(jù)的例子
今天小編就為大家分享一篇layui異步加載table表中某一列數(shù)據(jù)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09解析JSON字符串報(bào)錯(cuò)syntaxError:unexpected?end?of?JsoN?input如何解決
這篇文章主要給大家介紹了關(guān)于解析JSON字符串報(bào)錯(cuò)syntaxError:unexpected?end?of?JsoN?input如何解決的相關(guān)資料,文中通過(guò)代碼將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05解決WebStorm?2022.3.x?無(wú)法識(shí)別?Element?UI?2.15.11?新版本中的?el-
這篇文章主要介紹了解決?WebStorm?2022.3.x?無(wú)法識(shí)別?Element?UI?2.15.11?新版本中的?el-xxx?標(biāo)簽問(wèn)題,本文給大家分享兩種解決方案,需要的朋友可以參考下2023-01-01JavaScript代碼實(shí)現(xiàn)圖片循環(huán)滾動(dòng)效果
這篇文章主要介紹了JavaScript代碼實(shí)現(xiàn)圖片循環(huán)滾動(dòng)效果的相關(guān)資料,非常不錯(cuò),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06