知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
您當前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
小程序-實現(xiàn)自定義動畫彈框/提示框
發(fā)表時間:2021-1-5
發(fā)布人:葵宇科技
瀏覽次數(shù):46
在小程序中,用戶與界面進行交互時,有一些用戶反饋提示,例如:觸發(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>
復(fù)制代碼
如下是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);
}
}
復(fù)制代碼
如下是邏輯代碼
// 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);
},
});
復(fù)制代碼
在小程序中實現(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);
}
}
復(fù)制代碼
通過 css3
中的@keyframes
以及變換transform
,垂直方向上平移,實現(xiàn)動畫
以上是通過css3
的動畫animation
結(jié)合@keyframes
動畫幀實現(xiàn)的,那么在小程序當中,也可以通過官方的動畫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>
復(fù)制代碼
主要是給想要添加動畫的元素添加了一個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);
},
});
復(fù)制代碼
以上就是通過微信小程序中動畫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>
復(fù)制代碼
結(jié)語
在小程序當中實現(xiàn)動畫可以用css3的animation結(jié)合@keyframes實現(xiàn),同樣也可以通過小程序動畫的api去實現(xiàn)
相關(guān)案例查看更多
相關(guān)閱讀
- 網(wǎng)站建設(shè)公司網(wǎng)站
- 保險網(wǎng)站建設(shè)公司
- 云南etc小程序
- 云南網(wǎng)站建設(shè)服務(wù)公司
- 汽車報廢回收軟件
- 微信小程序
- 網(wǎng)站建設(shè)選
- 網(wǎng)站建設(shè)
- 云南軟件開發(fā)
- 昆明小程序設(shè)計
- 高端網(wǎng)站建設(shè)公司
- 專業(yè)網(wǎng)站建設(shè)公司
- asp網(wǎng)站
- 小程序開發(fā)費用
- 云南省住房建設(shè)廳網(wǎng)站
- 汽車報廢
- 云南微信小程序開發(fā)
- 網(wǎng)站建設(shè)方案 doc
- 小程序開發(fā)公司
- 云南網(wǎng)站建設(shè)快速優(yōu)化
- 云南衛(wèi)視小程序
- 小程序分銷商城
- 小程序商城
- 云南軟件設(shè)計
- 買小程序被騙
- 軟件開發(fā)
- 網(wǎng)站建設(shè)招商
- 網(wǎng)絡(luò)公司排名
- 云南網(wǎng)站建設(shè)公司排名
- 汽車回收管理