微信小程序帶動(dòng)畫(huà)彈窗組件使用方法詳解
本文實(shí)例為大家分享了微信小程序帶動(dòng)畫(huà)彈窗的具體代碼,供大家參考,具體內(nèi)容如下
基本效果如下:
具體實(shí)現(xiàn)如下:
第一步:
新建一個(gè) components 文件夾,用于存放我們以后開(kāi)發(fā)中的所用組件,在 components 組件中新建一個(gè)popup文件夾來(lái)存放我們的彈窗組件,在popup下右擊新建 Component 并命名為 popup 后,會(huì)生成對(duì)應(yīng)的 json wxml wxss js 4個(gè)文件,也就是一個(gè)自定義組件的組成部分,此時(shí)項(xiàng)目結(jié)構(gòu)應(yīng)該如下圖所示:
第二步上代碼:
popup.wxml
<view hidden="{{!flag}}" class='container' style=''> <view bindtap='_error' class='wrap {{wrapAnimate}}' style='background:rgba(0,0,0,{{bgOpacity}});'></view> <view class='popup-container {{popupAnimate}}'> <view class="wx-popup-title">{{title}}</view> <view class="wx-popup-con">{{content}}</view> <view class="wx-popup-btn"> <text class="btn-no" bindtap='_error'>{{btn_no}}</text> <text class="btn-ok" bindtap='_success'>{{btn_ok}}</text> </view> <image bindtap='_error' src='../../image/close.png' mode='widthFix' class='btn-colse'></image> </view> </view>
popup.wxss
.container{font-size:15px;color:#666;font-weight: bold;z-index:2;position:fixed;width:100vw;height:100vh;} .wrap{position:fixed;top:0;left:0;bottom:0;right:0;} .popup-container {position: fixed;left: 50%;top: 100%;width: 80%;max-width: 600rpx;border: 2rpx solid #ccc;border-radius: 10rpx;box-sizing: bordre-box;transform: translate(-50%, -50%);background: #fff;opacity: 0;} .wx-popup-title {width: 100%;padding: 20rpx 0;text-align: center;font-size: 40rpx;border-bottom: 2rpx solid #89cfea;} .wx-popup-con {margin: 60rpx 10rpx;text-align: center;} .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;} .btn-colse{width:35px;height:35px;position:absolute;bottom:-60px;left:50%;margin-left:-17.5px;} .wrapAnimate{animation: wrapAnimate 1s linear forwards} @keyframes wrapAnimate{ 0%{} 100%{background:rgba(0,0,0,0.7);} } .wrapAnimateOut{animation: wrapAnimateOut 1s 0.2s linear forwards} @keyframes wrapAnimateOut{ 0%{background:rgba(0,0,0,0.7);} 100%{background:rgba(0,0,0,0);} } .popupAnimate{animation: popupAnimate 1.2s linear forwards} @keyframes popupAnimate{ 0%{} 60%{top:47%;opacity: 1;} 80%{top:53%;opacity: 1;} 100%{top:50%;opacity: 1;} } .popupAnimateOut{animation: popupAnimateOut 1.2s linear forwards} @keyframes popupAnimateOut{ 0%{top:50%;opacity: 1;} 20%{top:47%;opacity: 1;} 100%{} }
popup.js
Component({ options: { multipleSlots: true // 在組件定義時(shí)的選項(xiàng)中啟用多slot支持 }, /*組件的屬性列表*/ properties: { title: { type: String, value: '標(biāo)題' }, // 彈窗內(nèi)容 content: { type: String, value: '內(nèi)容' }, // 彈窗取消按鈕文字 btn_no: { type: String, value: '取消' }, // 彈窗確認(rèn)按鈕文字 btn_ok: { type: String, value: '確定' } }, /* 組件的初始數(shù)據(jù) */ data: { flag: true, bgOpacity:0, wrapAnimate:'wrapAnimate', popupAnimate:'popupAnimate' }, /* 組件的方法列表 */ methods: { //隱藏彈框 hidePopup: function () { const that = this; this.setData({ bgOpacity: 0.7, wrapAnimate: "wrapAnimateOut", popupAnimate:"popupAnimateOut"}) setTimeout(function(){ that.setData({flag: false}) },1200) }, /* 內(nèi)部私有方法建議以下劃線開(kāi)頭 triggerEvent 用于觸發(fā)事件 */ _error() {//觸發(fā)取消回調(diào) this.triggerEvent("error") }, _success() {//觸發(fā)成功回調(diào) this.triggerEvent("success"); } } })
popup.json
{ "component": true, "usingComponents": {} }
第三步引用組件:
index.json
{ "usingComponents": { "popup":"/components/popup/popup" } }
index.wxml
<popup id='popup' title='彈窗組件' content='學(xué)會(huì)了嗎' btn_no='沒(méi)有' btn_ok='學(xué)會(huì)了' binderror="_error" bindsuccess="_success" > </popup>
index.js
Page({ showPopup() { this.popup.showPopup(); }, //取消事件 _error() { console.log('你點(diǎn)擊了取消'); this.selectComponent("#popup").hidePopup(); }, //確認(rèn)事件 _success() { console.log('你點(diǎn)擊了確定'); this.selectComponent("#popup").hidePopup(); } })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳釋JavaScript執(zhí)行環(huán)境與執(zhí)行棧
一句話就可以概括:代碼 ( 包括函數(shù) ) 執(zhí)行時(shí)所需要的所有信息就是執(zhí)行環(huán)境。由于 ES 歷經(jīng)多個(gè)版本,所以執(zhí)行環(huán)境的標(biāo)準(zhǔn)也一直在變。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04一個(gè)多瀏覽器支持的背景變暗的div并可拖動(dòng)提示窗口功能的代碼
兼容IE、Firefox、Opera前幾天在網(wǎng)上找了許多資料,看了不少兄弟的源碼,一直找不到合適的,要不就是拖動(dòng)有問(wèn)題,要不就是不兼容Firefox,所以自已寫(xiě)了一個(gè),下面是代碼:2008-04-04Javascript removeChild()刪除節(jié)點(diǎn)及刪除子節(jié)點(diǎn)的方法
這篇文章主要介紹了Javascript removeChild()刪除節(jié)點(diǎn)及刪除子節(jié)點(diǎn)的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12在線所見(jiàn)即所得HTML編輯器的實(shí)現(xiàn)原理淺析
這篇文章主要介紹了在線所見(jiàn)即所得HTML編輯器的實(shí)現(xiàn)原理淺析,本文用初始化、打開(kāi)編輯功能、獲取編輯器的內(nèi)容、增加樣式設(shè)置、再進(jìn)一步等步驟闡述在線編輯器的基本實(shí)現(xiàn)原理,需要的朋友可以參考下2015-04-04微信小程序 (地址選擇1)--選取搜索地點(diǎn)并顯示效果
這篇文章主要介紹了微信小程序 (地址選擇1)--選取搜索地點(diǎn)并顯示效果,實(shí)現(xiàn)思路是通過(guò)拖動(dòng)地圖,搜索地址,選擇地址并將地址值傳給文本框,具體實(shí)例代碼跟隨小編一起看看吧2019-12-12JavaScript事件循環(huán)剖析宏任務(wù)與微任務(wù)
這篇文章主要為大家介紹了JavaScript事件循環(huán)剖析宏任務(wù)與微任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10JavaScript實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)菜單實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)菜單實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06js完美實(shí)現(xiàn)@提到好友特效(兼容各大瀏覽器)
本文給大家分享的是一則使用javascript完美實(shí)現(xiàn)兼容各大瀏覽器的@好友自動(dòng)提示的特效,是根據(jù)百度貼吧的效果模仿來(lái)的,推薦給小伙伴們,希望大家能夠喜歡。2015-03-03JS module的導(dǎo)出和導(dǎo)入的實(shí)現(xiàn)代碼
這篇文章主要介紹了JS module的導(dǎo)出和導(dǎo)入的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02深入學(xué)習(xí)JS?XML和Fetch請(qǐng)求
這篇文章主要介紹了深入學(xué)習(xí)JS?XML和Fetch請(qǐng)求,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09