微信小程序彈窗組件使用詳解
介紹
最近在開發(fā)小程序應(yīng)用, 發(fā)現(xiàn)小程序當(dāng)中有關(guān)于組件的介紹非常的少, 當(dāng)前自己做的項(xiàng)目當(dāng)中,有出現(xiàn)過這種情況, 所以自己就封裝了一個(gè)小程序的彈窗組件, 現(xiàn)在把自己的心得分享給大家, 大家一起來學(xué)習(xí)吧
效果圖
需求背景
項(xiàng)目需求是需要在頁面上通過點(diǎn)擊按鈕, 然后彈出彈窗蒙層; 因?yàn)樾⌒〕绦虍?dāng)中經(jīng)常會(huì)用到彈窗, 因此這里我直接將彈窗封裝成了一個(gè)組件, 下次使用的時(shí)候,直接調(diào)用就可以了。
實(shí)現(xiàn)步驟
1、在微信小程序當(dāng)中, 在當(dāng)前項(xiàng)目當(dāng)中, 新建一個(gè)component
文件夾, 這個(gè)文件夾專門用來存放我們要使用的組件, 然后在component
文件夾下右擊, 新建文件夾popup
, 這里就是我們要用的彈窗組件的文件夾, 再右擊popup
文件夾, 選擇新建component
, 然后直接輸入popup
即可, 小程序內(nèi)部會(huì)為我們自動(dòng)生成.wxss , wxml , json , js
等模板文件, 如下圖所示,popup
文件夾下的文件為我們的組件,index
文件夾下的文件為首頁上頁面:
2、popup
彈窗組件的代碼部分;
popup.wxml
<view class="wx-popup" style="margin:-{{windowHeight/2}}px 0 0 -{{windowWidth/2}}px" hidden="{{flag}}"> ? <view class='popup-container'> ? ? <view class="wx-popup-title">{{title}}</view> ? ? <!-- <view class="wx-popup-con" >{{content}}</view> --> ? ? <view class="wx-popup-con" > ? ? ? <text>{{content_leftText}}</text> ? ? ? <text class="content_money">{{content_money}}</text> ? ? ? <text>{{content_rightText}}</text> ? ? </view> ? ? <view class="wx-popup-btn"> ? ? ? <view class="closeBtn"> ? ? ? ? <view class="close-popup" bindtap='_close'> ? ? ? ? ? <view>X</view> ? ? ? ? </view> ? ? ? </view> ? ? </view> ? </view> </view>
popup.wxss
.wx-popup { ? position: fixed; ? left: 0; ? bottom: 0; ? top: 0; ? z-index: 2000; ? width: 100%; ? height: 100%; ? background: rgba(0, 0, 0, .6); } .popup-container { ? position: fixed; ? left: 10%; ? top: 20%; ? width: 80%; ? max-width: 600rpx; ? border-radius: 20rpx; ? box-sizing: bordre-box; ? background: #fff; ? z-index: 2000; } .wx-popup-title { ? width: 100%; ? padding: 28rpx; ? text-align: center; ? font-size: 36rpx; ? font-weight: bold; ? border-bottom: 5rpx solid #9EA3BA; ? box-sizing: border-box; } .wx-popup-con { ? margin: 50rpx 10rpx; ? text-align: center; ? padding: 0 86rpx; } .wx-popup-con text { ? padding-bottom: 10rpx; } .content_money { ? color: #FFB258; } .wx-popup-btn { ? display: flex; ? justify-content: space-around; ? margin-bottom: 40rpx; } .wx-popup-btn text { ? display: flex; ? align-items: center; ? justify-content: center; ? width: 30%; ? height: 88rpx; ? border: 2rpx solid #ccc; ? border-radius: 88rpx; } .wx-popup-btn .closeBtn { ? position: fixed; ? left: 45%; ? bottom: 30%; } .wx-popup-btn .close-popup { ? position: relative; ? height: 80rpx; ? width: 80rpx; ? border: 5rpx solid #fff; ? border-radius: 50%; } ?.wx-popup-btn .close-popup view { ? ?position: absolute; ? ?left: 30%; ? ?top: 8%; ? ?font-size: 50rpx; ? ?color: #fff; ?}
popup.js
Component({ ? options: { ? ? multipleSlots: true // 在組件定義時(shí)的選項(xiàng)中啟用多slot支持 ? }, ? /** ? ?* 組件的屬性列表 ? ?*/ ? properties: { ? ? title: { ? ? ? ? ? ?// 屬性名 ? ? ? type: String, ? ? // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型) ? ? ? value: '標(biāo)題' ? ? // 屬性初始值(可選),如果未指定則會(huì)根據(jù)類型選擇一個(gè) ? ? }, ? ? // 彈窗內(nèi)容 ? ? content_leftText: { ? ? ? type: String, ? ? ? value: '內(nèi)容' ? ? }, ? ? content_money: { ? ? ? type: String, ? ? ? value: '內(nèi)容' ? ? }, ? ? content_rightText: { ? ? ? type: String, ? ? ? value: '內(nèi)容' ? ? }, ? }, ? /** ? ?* 組件的初始數(shù)據(jù) ? ?*/ ? data: { ? ? flag: true, ? }, ? /** ? ?* 組件的方法列表 ? ?*/ ? methods: { ? ? //隱藏彈框 ? ? hidePopup: function () { ? ? ? this.setData({ ? ? ? ? flag: !this.data.flag ? ? ? }) ? ? }, ? ? //展示彈框 ? ? showPopup () { ? ? ? this.setData({ ? ? ? ? flag: !this.data.flag ? ? ? }) ? ? }, ? ? /* ? ? * triggerEvent 用于觸發(fā)事件 ? ? */ ? ? _close() { ? ? ? this.triggerEvent("close"); ? ? } ? } })
3、完成模板文件的工作后, 接下來就是在首頁當(dāng)中對(duì)這個(gè)組件進(jìn)行配置, 在index
文件夾當(dāng)中對(duì)index.json
文件進(jìn)行配置, 代碼如下:
4、在首頁當(dāng)中進(jìn)行使用,代碼如下:
<view class="index_popup"> ? ? <view class="btn-area"> ? ? ? ? <button type="primary" bindtap="showPopup">點(diǎn)擊預(yù)測價(jià)錢</button> ? ? </view> ? ? <popup id='popup' ? ? ? ? title='預(yù)測價(jià)格'? ? ? ? ? content_leftText='您好,預(yù)測價(jià)格為' ? ? ? ? content_money='{{content_money}}'? ? ? ? ? content_rightText='元 , 預(yù)測價(jià)格和實(shí)際價(jià)格存在偏差,請(qǐng)耐心等候?qū)I(yè)顧問為您服務(wù)。' ? ? ? ? ? bind:close="_close"> ? ? </popup> </view>
5、index.wxss
的樣式
.index_popup .btn-area button { ? background-image: linear-gradient(to right, rgba(36, 162, 255), rgba(36, 172, 255), rgba(36, 192, 255)); ? font-size: 34rpx; ? font-weight: normal; ? border-radius: 50rpx; ? padding: 18rpx 30rpx; ? margin-top: 100rpx; }
6、index.js
文件里, 配置對(duì)應(yīng)的點(diǎn)擊事件, 還有操作數(shù)據(jù)
// index.js // 獲取應(yīng)用實(shí)例 const app = getApp() Page({ ? data: { ? ? content_money: '1000萬' ? }, ? onReady: function () { ? ? //獲得popup組件 ? ? this.popup = this.selectComponent("#popup"); ? }, ? showPopup() { ? ? this.popup.showPopup(); ? }, ? //取消事件 ? _close() { ? ? console.log('你點(diǎn)擊了關(guān)閉按鈕'); ? ? this.popup.hidePopup(); ? }, ? onLoad() { ? }, })
至此, 就全部結(jié)束了, 當(dāng)點(diǎn)擊首頁index.wxml
上的按鈕時(shí), 彈出彈窗組件, 點(diǎn)擊彈窗頁面下的X按鈕, 可以關(guān)閉彈窗。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
舉例講解JavaScript substring()的使用方法
這篇文章主要通過舉例的方法講解了javaScript substring()的用法,substring() 方法用于提取字符串中介于兩個(gè)指定下標(biāo)之間的字符,感興趣的小伙伴們可以參考一下2015-11-11Javascript 函數(shù)對(duì)象的多重身份
函數(shù)對(duì)象是javascript 中一個(gè)很特殊的對(duì)象,其特殊體現(xiàn)在他的多重身份上。2009-06-06微信小程序?qū)崿F(xiàn)活動(dòng)報(bào)名登記功能(實(shí)例代碼)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)活動(dòng)報(bào)名登記,本篇將介紹使用微信小程序?qū)崿F(xiàn)發(fā)起一個(gè)活動(dòng)報(bào)名的設(shè)計(jì),以此為基礎(chǔ),我們可以掌握微信小程序表單的基本用法,進(jìn)而在諸如疫情信息登記、出入報(bào)備等場景中使用小程序進(jìn)行開發(fā),滿足相關(guān)的需求,需要的朋友可以參考下2022-09-09JavaScript中操作Mysql數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了JavaScript中操作Mysql數(shù)據(jù)庫實(shí)例,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-04-04javascript+HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎(jiǎng)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04Array.prototype.concat不是通用方法反駁[譯]
ECMAScript 5.1規(guī)范中指出,數(shù)組方法concat是通用的(generic).本文反駁了這一結(jié)論,因?yàn)閷?shí)際上并不是這樣的2012-09-09