微信小程序?qū)崿F(xiàn)自定義動畫彈框/提示框的方法實例
前言
在小程序中,用戶與界面進行交互時,有一些用戶反饋提示,例如:觸發(fā)某個按鈕,從底部彈出框,從頂部彈出等
如今,有一些現(xiàn)成的 UI 庫,雖然已經(jīng)實現(xiàn)了的,但若只是為了實現(xiàn)一個底部彈出框或者自定義提示框,不引用第三方 UI 庫
怎么手動原生方式去實現(xiàn)呢,最主要的是怎么去實現(xiàn)動畫
css3 實現(xiàn)動畫
如下是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);
},
});
在小程序中實現(xiàn)動畫,如上實現(xiàn)的動畫,是通過css3中的@keyframes實現(xiàn)的,如下所示
.pop {
/* ... */
animation-duration: 0.5s;
animation-name: slidein; // 動畫的名稱
}
@keyframes slidein {
// 定義動畫的名稱
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,垂直方向上平移,實現(xiàn)動畫
示例效果如下所示

以上是通過 css3 的動畫animation結(jié)合@keyframes動畫幀實現(xiàn)的,那么在小程序當(dāng)中,也可以通過官方的動畫API實現(xiàn)的
小程序動畫 API-實現(xiàn)動畫
創(chuàng)建一個動畫實例 animation,調(diào)用實例的方法來描述動畫。最后通過動畫實例的 export 方法導(dǎo)出動畫數(shù)據(jù)傳遞給組件的 animation 屬性
示例效果如下所示

如下是實例代碼
<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>
主要是給想要添加動畫的元素添加了一個animation屬性,現(xiàn)在的動畫是通過js去控制,而非css
如下代碼所示
// pages/customalertbox/customalertbox.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
isBottom: false,
isTop: false,
animationData: {}, // 定義動畫對象
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function(options) {},
onBottomBox() {
// 創(chuàng)建動畫
var animation = wx.createAnimation({
duration: 2000,
timingFunction: 'ease',
});
this.animation = animation;
// 先在y軸偏移180,然后用step()完成一個動畫
animation.translateY(180).step();
this.setData({
animationData: animation.export(),
isBottom: true,
});
// 設(shè)置setTimeout來改變y軸偏移量,實現(xiàn)有感覺的滑動,回到初始位置
setTimeout(() => {
animation.translateY(0).step();
this.setData({
animationData: animation.export(),
});
}, 200);
},
// 點擊遮罩層隱藏彈框
onHideBox() {
var animation = wx.createAnimation({
duration: 2000,
timingFunction: 'ease',
});
this.animation = animation;
// 先在y軸偏移180,然后用step()完成一個動畫
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);
},
});
以上就是通過微信小程序中動畫API實現(xiàn)的完成的動畫,代碼要比 css3 要多一些,可以實現(xiàn)更加復(fù)雜的動畫效果
注意
如果是底部彈出框,拖動里面時,若遮罩層底部會跟著滾動,具體解決辦法也可以在外層添加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)中實現(xiàn)動畫可以用css3的animation結(jié)合@keyframes實現(xiàn),同樣也可以通過小程序動畫的api去實現(xiàn)
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)自定義動畫彈框/提示框的文章就介紹到這了,更多相關(guān)微信小程序自定義動畫彈框/提示框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文檔
相關(guān)文章
js寫出遮罩層登陸框和對聯(lián)廣告并自動跟隨滾動條滾動
這篇文章主要介紹了js寫出遮罩層登陸框和對聯(lián)廣告并自動跟隨滾動條滾動,需要的朋友可以參考下2014-04-04
小發(fā)現(xiàn)之淺談location.search與location.hash的問題
下面小編就為大家?guī)硪黄“l(fā)現(xiàn)之淺談location.search與location.hash的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
微信小程序使用map組件實現(xiàn)解析經(jīng)緯度功能示例
這篇文章主要介紹了微信小程序使用map組件實現(xiàn)解析經(jīng)緯度功能,涉及微信小程序map組件結(jié)合高德地圖進行經(jīng)緯度獲取相關(guān)操作技巧,需要的朋友可以參考下2019-01-01

