微信小程序?qū)崿F(xiàn)自定義動(dòng)畫彈框/提示框的方法實(shí)例
前言
在小程序中,用戶與界面進(jìn)行交互時(shí),有一些用戶反饋提示,例如:觸發(fā)某個(gè)按鈕,從底部彈出框,從頂部彈出等
如今,有一些現(xiàn)成的 UI 庫,雖然已經(jīng)實(shí)現(xiàn)了的,但若只是為了實(shí)現(xiàn)一個(gè)底部彈出框或者自定義提示框,不引用第三方 UI 庫
怎么手動(dòng)原生方式去實(shí)現(xiàn)呢,最主要的是怎么去實(shí)現(xiàn)動(dòng)畫
css3 實(shí)現(xiàn)動(dòng)畫
如下是wxml代碼
<view> <view class="click-btn" catchtap="onBottomBox">彈出底部彈出框</view> <view class="click-btn" bindtap="onTopBox">彈出頂部提示框</view> <view wx:if="{{isBottom}}" class="bottom-box"> <div class="mask" bindtap="onHideBox"></div> <div class="pop">底部彈出內(nèi)容</div> </view> <div wx:if="{{isTop}}" class="top-box">通知內(nèi)容</div> </view>
如下是wxss代碼
/* pages/customalertbox/customalertbox.wxss */ .click-btn { width: 120px; height: 40px; line-height: 40px; text-align: center; margin: 20px auto; border: 1px solid #ccc; border-radius: 5px; } .top-box { width: 100%; height: 30px; background: #f56c6c; border-radius: 0 0 8px 8px; color: #fff; text-align: center; line-height: 30px; font-size: 28rpx; position: absolute; top: 0px; left: 0; animation-duration: 0.5s; animation-name: slidetop; } .mask { width: 100%; height: 100%; position: fixed; top: 0; left: 0; background: rgba(0, 0, 0, 0.5); } .pop { position: absolute; width: 100%; height: 180px; background: #42b983; border-radius: 8px 8px 0 0; position: absolute; bottom: 0px; animation-duration: 0.5s; animation-name: slidein; } @keyframes slidein { from { transform: translateY(70%); } to { transform: translateY(0); } } @keyframes slidetop { from { transform: translateY(-30px); } to { transform: translateY(0px); } }
如下是邏輯代碼
// pages/customalertbox/customalertbox.js Page({ /** * 頁面的初始數(shù)據(jù) */ data: { isBottom: false, isTop: false, }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function(options) {}, onBottomBox() { this.setData({ isBottom: true, }); }, onHideBox() { this.setData({ isBottom: false, }); }, onTopBox() { this.setData({ isTop: true, }); setTimeout(() => { this.setData({ isTop: false, }); }, 2000); }, });
在小程序中實(shí)現(xiàn)動(dòng)畫,如上實(shí)現(xiàn)的動(dòng)畫,是通過css3中的@keyframes實(shí)現(xiàn)的,如下所示
.pop { /* ... */ animation-duration: 0.5s; animation-name: slidein; // 動(dòng)畫的名稱 } @keyframes slidein { // 定義動(dòng)畫的名稱 from { transform: translateY(70%); // 平移,垂直方向上 } to { transform: translateY(0); } } .top-box { /* ... */ animation-duration: 0.5s; animation-name: slidetop; } @keyframes slidetop { from { transform: translateY(-30px); } to { transform: translateY(0px); } }
通過 css3 中的@keyframes以及變換transform,垂直方向上平移,實(shí)現(xiàn)動(dòng)畫
示例效果如下所示
以上是通過 css3 的動(dòng)畫animation結(jié)合@keyframes動(dòng)畫幀實(shí)現(xiàn)的,那么在小程序當(dāng)中,也可以通過官方的動(dòng)畫API實(shí)現(xiàn)的
小程序動(dòng)畫 API-實(shí)現(xiàn)動(dòng)畫
創(chuàng)建一個(gè)動(dòng)畫實(shí)例 animation,調(diào)用實(shí)例的方法來描述動(dòng)畫。最后通過動(dòng)畫實(shí)例的 export 方法導(dǎo)出動(dòng)畫數(shù)據(jù)傳遞給組件的 animation 屬性
示例效果如下所示
如下是實(shí)例代碼
<view> <view class="click-btn" bindtap="onBottomBox">彈出底部彈出框</view> <view class="click-btn" bindtap="onTopBox">彈出頂部提示框</view> <view wx:if="{{isBottom}}" style="position: absolute;width: 100%;height: 100%;bottom: 0px;" > <div class="mask" bindtap="onHideBox"></div> <div class="pop" animation="{{animationData}}">底部彈出內(nèi)容</div> </view> <div wx:if="{{isTop}}" class="top-box">通知內(nèi)容</div> </view>
主要是給想要添加動(dòng)畫的元素添加了一個(gè)animation屬性,現(xiàn)在的動(dòng)畫是通過js去控制,而非css
如下代碼所示
// pages/customalertbox/customalertbox.js Page({ /** * 頁面的初始數(shù)據(jù) */ data: { isBottom: false, isTop: false, animationData: {}, // 定義動(dòng)畫對(duì)象 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function(options) {}, onBottomBox() { // 創(chuàng)建動(dòng)畫 var animation = wx.createAnimation({ duration: 2000, timingFunction: 'ease', }); this.animation = animation; // 先在y軸偏移180,然后用step()完成一個(gè)動(dòng)畫 animation.translateY(180).step(); this.setData({ animationData: animation.export(), isBottom: true, }); // 設(shè)置setTimeout來改變y軸偏移量,實(shí)現(xiàn)有感覺的滑動(dòng),回到初始位置 setTimeout(() => { animation.translateY(0).step(); this.setData({ animationData: animation.export(), }); }, 200); }, // 點(diǎn)擊遮罩層隱藏彈框 onHideBox() { var animation = wx.createAnimation({ duration: 2000, timingFunction: 'ease', }); this.animation = animation; // 先在y軸偏移180,然后用step()完成一個(gè)動(dòng)畫 animation.translateY(180).step(); this.setData({ animationData: animation.export(), }); setTimeout(() => { animation.translateY(0).step(); this.setData({ animationData: animation.export(), isBottom: false, }); }, 200); }, onTopBox() { this.setData({ isTop: true, }); setTimeout(() => { this.setData({ isTop: false, }); }, 2000); }, });
以上就是通過微信小程序中動(dòng)畫API實(shí)現(xiàn)的完成的動(dòng)畫,代碼要比 css3 要多一些,可以實(shí)現(xiàn)更加復(fù)雜的動(dòng)畫效果
注意
如果是底部彈出框,拖動(dòng)里面時(shí),若遮罩層底部會(huì)跟著滾動(dòng),具體解決辦法也可以在外層添加catchtouchmove="true"即可解決
<view> <view class="click-btn" bindtap="onBottomBox">彈出底部彈出框</view> <view catchtouchmove="true" wx:if="{{isBottom}}" style="position: absolute;width: 100%;height: 100%;bottom: 0px;" > <div class="mask" bindtap="onHideBox"></div> <div class="pop" animation="{{animationData}}">底部彈出內(nèi)容</div> </view> <div wx:if="{{isTop}}" class="top-box">通知內(nèi)容</div> </view>
結(jié)語
在小程序當(dāng)中實(shí)現(xiàn)動(dòng)畫可以用css3的animation結(jié)合@keyframes實(shí)現(xiàn),同樣也可以通過小程序動(dòng)畫的api去實(shí)現(xiàn)
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)自定義動(dòng)畫彈框/提示框的文章就介紹到這了,更多相關(guān)微信小程序自定義動(dòng)畫彈框/提示框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文檔
相關(guān)文章
js函數(shù)定時(shí)器實(shí)現(xiàn)定時(shí)讀取系統(tǒng)實(shí)時(shí)連接數(shù)
這篇文章主要介紹了使用js函數(shù)定時(shí)器實(shí)現(xiàn)定時(shí)讀取系統(tǒng)實(shí)時(shí)連接數(shù),需要的朋友可以參考下2014-04-04bootstrap基本配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了bootstrap基本配置,詳細(xì)講解如何下載并安裝 Bootstrap,討論 Bootstrap 文件結(jié)構(gòu),并通過一個(gè)實(shí)例演示它的用法。2017-07-07使用腳本控制網(wǎng)頁Table的顯示隱藏(全代碼)_AX
使用腳本控制網(wǎng)頁Table的顯示隱藏(全代碼)_AX...2006-12-12使用JavaScript判斷手機(jī)瀏覽器是橫屏還是豎屏問題
這篇文章主要介紹了使用JavaScript判斷手機(jī)瀏覽器是橫屏還是豎屏問題的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08uniapp中使用render.js進(jìn)行openlayers、arcgis等地圖操作實(shí)戰(zhàn)指南
renderjs是一個(gè)運(yùn)行在視圖層的js,它比WXS更加強(qiáng)大,它只支持app-vue和h5,下面這篇文章主要給大家介紹了關(guān)于uniapp中使用render.js進(jìn)行openlayers、arcgis等地圖操作的相關(guān)資料,需要的朋友可以參考下2024-01-01