


Customize the animated pop-up box/prompt box to implement the mini program
Oct 27, 2020 pm 05:25 PMWeChat Mini Program DevelopmentThe column introduces how to customize the animated pop-up box/prompt box of the mini program.
##Foreword
In the mini program , When the user interacts with the interface, there are some user feedback prompts, such as: triggering a button, popping up the box from the bottom, popping up from the top, etc.Nowadays, there are some ready-made UI libraries, although they have been implemented , but if it is just to implement a bottom pop-up box or a custom prompt box without referencing the third-party UI libraryHow to implement it manually and natively? The most important thing is how to implement animationcss3 to achieve animation
The following is thewxmlcode
<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>
/* 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); }, });
.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); } }through
css3#@keyframes
and transformation transform
, translate in the vertical direction to achieve animationThe sample effect is as follows
Nuggets do not support gif-Instance effect can be clicked on the link
The above is achieved through the animation of
css3animation
combined with the @keyframes
animation frame, then in the mini program Among them, it can also be achieved through the official animation API<h2 id="span-class-prefix-style-display-none-span-span-class-content-小程序動畫-API-實現(xiàn)動畫-span-span-class-suffix-span"><span class="prefix" style="display: none;"></span><span class="content">小程序動畫 API-實現(xiàn)動畫</span><span class="suffix"></span></h2><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black; text-align: justify;">創(chuàng)建一個動畫實例 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #009688;">animation
,調(diào)用實例的方法來描述動畫。最后通過動畫實例的 export
方法導出動畫數(shù)據(jù)傳遞給組件的 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}}">底部彈出內(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, }); // 設置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)更加復雜的動畫效果
注意
如果是底部彈出框,拖動里面時,若遮罩層底部會跟著滾動,具體解決辦法也可以在外層添加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>
結語
在小程序當中實現(xiàn)動畫可以用css3的animation結合@keyframes實現(xiàn),同樣也可以通過小程序動畫的api去實現(xiàn)
相關免費學習推薦:微信小程序開發(fā)
The above is the detailed content of Customize the animated pop-up box/prompt box to implement the mini program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

With the popularity of mobile Internet technology and smartphones, WeChat has become an indispensable application in people's lives. WeChat mini programs allow people to directly use mini programs to solve some simple needs without downloading and installing applications. This article will introduce how to use Python to develop WeChat applet. 1. Preparation Before using Python to develop WeChat applet, you need to install the relevant Python library. It is recommended to use the two libraries wxpy and itchat here. wxpy is a WeChat machine

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

Mini programs can use react. How to use it: 1. Implement a renderer based on "react-reconciler" and generate a DSL; 2. Create a mini program component to parse and render DSL; 3. Install npm and execute the developer Build npm in the tool; 4. Introduce the package into your own page, and then use the API to complete the development.

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

This article brings you some related issues about WeChat mini programs. It mainly introduces how to use official account template messages in mini programs. Let’s take a look at them together. I hope it will be helpful to everyone.

Implementation idea: Establishing the server side of thread, so as to process the various functions of the chat room. The establishment of the x02 client is much simpler than the server. The function of the client is only to send and receive messages, and to enter specific characters according to specific rules. To achieve the use of different functions, therefore, on the client side, you only need to use two threads, one is dedicated to receiving messages, and the other is dedicated to sending messages. As for why not use one, that is because, only

Geolocation positioning and map display of PHP and mini programs Geolocation positioning and map display have become one of the necessary functions in modern technology. With the popularity of mobile devices, people's demand for positioning and map display is also increasing. During the development process, PHP and applets are two common technology choices. This article will introduce you to the implementation method of geographical location positioning and map display in PHP and mini programs, and attach corresponding code examples. 1. Geolocation in PHP In PHP, we can use third-party geolocation
