微信小程式開發#欄位介紹如何自訂實作小程式動畫彈框/提示框。
前言
在小程式中,使用者與介面互動時,有一些使用者回饋提示,例如:觸發某個按鈕,從底部彈出框,從頂部彈出等
如今,有一些現成的UI 庫,雖然已經實現了的,但若只是為了實現一個底部彈出框或自訂提示框,不引用第三方UI 庫
怎麼手動原生方式去實現呢,最主要的是怎麼去實現動畫
css3 實作動畫
如下是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">底部弹出内容</div> </view> <div wx:if="{{isTop}}" class="top-box">通知内容</div> </view>
/* 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({ /** * 页面的初始数据 */ data: { isBottom: false, isTop: false, }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) {}, onBottomBox() { this.setData({ isBottom: true, }); }, onHideBox() { this.setData({ isBottom: false, }); }, onTopBox() { this.setData({ isTop: true, }); setTimeout(() => { this.setData({ isTop: false, }); }, 2000); }, });
.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
,垂直方向上平移,實現動畫
範例效果如下所示
掘金不支持gif-實例效果可戳鏈接
以上是透過css3
的動畫animation
結合@keyframes
動畫幀實現的,那麼在小程序當中,也可以透過官方的動畫API實現的
小程序动画 API-实现动画
创建一个动画实例 animation
,调用实例的方法来描述动画。最后通过动画实例的 export
方法导出动画数据传递给组件的 animation
属性
示例效果如下所示
掘金不支持gif-实例效果可戳链接
如下是实例代码
<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}}">底部弹出内容</div> </view> <div wx:if="{{isTop}}" class="top-box">通知内容</div> </view>
主要是给想要添加动画的元素添加了一个animation
属性,现在的动画是通过js去控制,而非css
如下代码所示
// pages/customalertbox/customalertbox.js Page({ /** * 页面的初始数据 */ data: { isBottom: false, isTop: false, animationData: {}, // 定义动画对象 }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) {}, onBottomBox() { // 创建动画 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, }); // 设置setTimeout来改变y轴偏移量,实现有感觉的滑动,回到初始位置 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
实现的完成的动画,代码要比 css3
要多一些,可以实现更加复杂的动画效果
注意
如果是底部弹出框,拖动里面时,若遮罩层底部会跟着滚动,具体解决办法也可以在外层添加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}}">底部弹出内容</div> </view> <div wx:if="{{isTop}}" class="top-box">通知内容</div> </view>
结语
在小程序当中实现动画可以用css3的animation结合@keyframes实现,同样也可以通过小程序动画的api去实现
相关免费学习推荐:微信小程序开发
以上是自訂實作小程式的動畫彈框/提示框的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)