微信小程序彈窗組件使用詳解
介紹
最近在開發(fā)小程序應(yīng)用, 發(fā)現(xiàn)小程序當中有關(guān)于組件的介紹非常的少, 當前自己做的項目當中,有出現(xiàn)過這種情況, 所以自己就封裝了一個小程序的彈窗組件, 現(xiàn)在把自己的心得分享給大家, 大家一起來學(xué)習(xí)吧
效果圖
需求背景
項目需求是需要在頁面上通過點擊按鈕, 然后彈出彈窗蒙層; 因為小小程序當中經(jīng)常會用到彈窗, 因此這里我直接將彈窗封裝成了一個組件, 下次使用的時候,直接調(diào)用就可以了。
實現(xiàn)步驟
1、在微信小程序當中, 在當前項目當中, 新建一個component
文件夾, 這個文件夾專門用來存放我們要使用的組件, 然后在component
文件夾下右擊, 新建文件夾popup
, 這里就是我們要用的彈窗組件的文件夾, 再右擊popup
文件夾, 選擇新建component
, 然后直接輸入popup
即可, 小程序內(nèi)部會為我們自動生成.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 // 在組件定義時的選項中啟用多slot支持 ? }, ? /** ? ?* 組件的屬性列表 ? ?*/ ? properties: { ? ? title: { ? ? ? ? ? ?// 屬性名 ? ? ? type: String, ? ? // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型) ? ? ? value: '標題' ? ? // 屬性初始值(可選),如果未指定則會根據(jù)類型選擇一個 ? ? }, ? ? // 彈窗內(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、完成模板文件的工作后, 接下來就是在首頁當中對這個組件進行配置, 在index
文件夾當中對index.json
文件進行配置, 代碼如下:
4、在首頁當中進行使用,代碼如下:
<view class="index_popup"> ? ? <view class="btn-area"> ? ? ? ? <button type="primary" bindtap="showPopup">點擊預(yù)測價錢</button> ? ? </view> ? ? <popup id='popup' ? ? ? ? title='預(yù)測價格'? ? ? ? ? content_leftText='您好,預(yù)測價格為' ? ? ? ? content_money='{{content_money}}'? ? ? ? ? content_rightText='元 , 預(yù)測價格和實際價格存在偏差,請耐心等候?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
文件里, 配置對應(yīng)的點擊事件, 還有操作數(shù)據(jù)
// index.js // 獲取應(yīng)用實例 const app = getApp() Page({ ? data: { ? ? content_money: '1000萬' ? }, ? onReady: function () { ? ? //獲得popup組件 ? ? this.popup = this.selectComponent("#popup"); ? }, ? showPopup() { ? ? this.popup.showPopup(); ? }, ? //取消事件 ? _close() { ? ? console.log('你點擊了關(guān)閉按鈕'); ? ? this.popup.hidePopup(); ? }, ? onLoad() { ? }, })
至此, 就全部結(jié)束了, 當點擊首頁index.wxml
上的按鈕時, 彈出彈窗組件, 點擊彈窗頁面下的X按鈕, 可以關(guān)閉彈窗。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
舉例講解JavaScript substring()的使用方法
這篇文章主要通過舉例的方法講解了javaScript substring()的用法,substring() 方法用于提取字符串中介于兩個指定下標之間的字符,感興趣的小伙伴們可以參考一下2015-11-11微信小程序?qū)崿F(xiàn)活動報名登記功能(實例代碼)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)活動報名登記,本篇將介紹使用微信小程序?qū)崿F(xiàn)發(fā)起一個活動報名的設(shè)計,以此為基礎(chǔ),我們可以掌握微信小程序表單的基本用法,進而在諸如疫情信息登記、出入報備等場景中使用小程序進行開發(fā),滿足相關(guān)的需求,需要的朋友可以參考下2022-09-09JavaScript中操作Mysql數(shù)據(jù)庫實例
這篇文章主要介紹了JavaScript中操作Mysql數(shù)據(jù)庫實例,本文直接給出實現(xiàn)代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-04-04javascript+HTML5 Canvas繪制轉(zhuǎn)盤抽獎
這篇文章主要為大家詳細介紹了javascrip+HTML5 Canvas繪制轉(zhuǎn)盤抽獎的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-04-04Array.prototype.concat不是通用方法反駁[譯]
ECMAScript 5.1規(guī)范中指出,數(shù)組方法concat是通用的(generic).本文反駁了這一結(jié)論,因為實際上并不是這樣的2012-09-09