


Walk you step by step on how to achieve the 3D naked-eye carousel effect in the mini program
Jan 11, 2022 pm 06:59 PMHow to achieve 3D naked eye effect in mini program carousel? The following article will introduce to you the implementation method to add color to the Spring Festival atmosphere. I hope it will be helpful to everyone!
Among the many functional modules of an APP, the home page carousel plays an important role. It is the entrance to distribute key information. I remembered an article I read a few months ago - " Implementation of naked-eye 3D effect in Ziruke APP". The article mentioned that the following naked-eye 3D banner carousel effect was implemented on the Android app:
Inspired by this article, I decided to "follow the example" and try to simulate a 3D naked-eye effect carousel full of Spring Festival atmosphere on the mini program.
Principle
Carefully observe the dynamic renderings implemented above. It can be seen that the banner picture is not a conventional picture, but uses a picture to divide the content. It is displayed in a layer-by-layer manner (as mentioned in the article mentioned above, the background layer, foreground and middle ground are superimposed and presented. You can go to the above to understand first), and then monitor the direction sensor of the mobile phone and adjust the display according to the direction. The foreground and background move, creating a visual depth of field effect.
What’s interesting is that if you are using an iPhone, I believe you should be able to find that in the homepage state, as the phone rotates in different directions, the background image will move slightly in the opposite direction, which can also give people a sense of A similar depth of field effect. (The effect is as shown below)
Practical combat
After introducing the principle, let’s start the actual combat.
Looking through the mini program documentation, we need to use two APIs: wx.startDeviceMotionListening and wx.onDeviceMotionChange. What we need to focus on here is the content returned by the wx.onDeviceMotionChange API. According to the documentation, the API returns the following three values:
If you are exposed to this for the first time API, I believe you are confused after reading the documentation. Next, I will use the Chrome browser debugging tool to help you fully understand what these three values ??mean.
Use chrome developer tools to understand API return values
Open the browser developer tools and follow the steps below to turn on sensor debugging:
After opening it, look here:
Eh? Isn't this the same thing? Yes, the three values ??shown here exactly correspond to the API return value. It can be seen that in the case of alpha=0, beta=90, gamma=0, it means that the phone is standing vertically on a plane. We can click on the option or directly modify the value in the input box, and we can intuitively see the corresponding value. changes, the flip state of the mobile phone changes, for example, when the mobile phone is placed flat on the table, the three parameter values ????are as follows:
With the above real-time simulation tools, the next picture will be Easy to understand:
- alpha: Indicates the angle of rotation of the device along the Z axis, ranging from 0 to 360;
- beta: Indicates that the device is in The rotation angle on the x-axis, ranging from -180~180. It describes the rotation of the device from front to back;
- gamma: indicates the rotation angle of the device on the y-axis, ranging from -90~90. It describes the situation when the device is rotated from left to right.
Code
wxml:
<view class="swiper-box"> <image wx:for="{{background}}" class="swiper-bg {{animationStart || current === index ? 'fadeIn' : 'fadeOut'}} "></image> <swiper indicator-dots="{{true}}" indicator-active-color="#fff" interval="{{3000}}" autoplay="{{true}}" circular="{{true}}" bindchange="handleChange" bindtransition="handleTransition" bindanimationfinish="handleFinish"> <block wx:for="{{background}}" wx:key="*this"> <swiper-item> <view class="swiper-item-content" > <image class="icon" src="../../images/cloud.png" style="width: 90px; height: 90px;transform: translate3d({{x}}px, {{y}}px, {{z}}px);" wx:if="{{index === 0}}"></image> <image class="icon" src="../../images/firecrackers.png" style="width: 90px; height: 90px;transform: translate3d({{x}}px, {{y}}px, {{z}}px);" wx:else></image> <text class="text" wx:if="{{index === 0}}">新年快樂</text> <text class="text" wx:else>大吉大利</text> </view> </swiper-item> </block> </swiper> </view>
Note here that since swiper can only nest the swiper-item component, you need to The background image is placed at the same level as the swiper and displayed in a positioning manner
js:
// index.js // 獲取應用實例 const app = getApp() Page({ data: { background: ['https://cloud-minapp-39237.cloud.ifanrusercontent.com/1n6jtVIbbJ3rnAv7.jpg', 'https://cloud-minapp-39237.cloud.ifanrusercontent.com/1n6mBOvOutOFQ3E8.png',], x: 0, y: 0, z: 0, animationFinish: true, // 動畫是否執(zhí)行完成 animationStart: false, // 是否開始執(zhí)行動畫 current: 0, }, // 動畫開始執(zhí)行 handleTransition(e) { if (this.data.animationFinish) { this.setData({ animationFinish: false, animationStart: true, }) } }, // 動畫執(zhí)行結束 handleFinish() { this.setData({ animationFinish: true, animationStart: false, }) }, // current值變化 handleChange(e) { this.setData({ current: e.detail.current, }) }, onLoad() { const that = this; // 監(jiān)聽方向變化 wx.startDeviceMotionListening({ success() { wx.onDeviceMotionChange(function (res) { const { alpha, // 0-360 beta, // -180-180 gamma // -90- 90 } = res const disX = gamma / 90 * 20 const disY = beta / 90 * 12 let z = 0 if (disX > 0 || disY > 0) { z = 20 } else { z = -20 } that.setData({ x: disX, y: disY, z }) }) } }) } })
The code to be explained here is
const disY = beta / 90 * 12
Normally when we use the mobile phone, the screen is facing up , so just take half the relative value.
After calculating the offset x, y, the page changes the offset distance of the element through transform: translate3d()
.
Final effect
The effect here seems not to be particularly obvious, for two reasons:
- 素材圖是我網上找到拼湊而成,總體合成效果并不美觀,想達到較逼真的效果需要設計配合出素材圖;
- 在偏移至最大值時,未做緩沖動畫,不合符直覺(這里后面有時間再研究實現(xiàn));
額外的動畫效果
其實借助該方向API,我們還可以作為觸發(fā)動畫的觸發(fā)器。例如在手機翻轉到一定角度值時,我們可以播放煙花效果
安裝lottie-miniprogram包
npm i lottie-miniprogram
安裝完之后記得在微信開發(fā)者工具中點擊構建npm包
wxml:
<canvas id="canvas" type="2d" style="max-width:90%"></canvas>
js:
onLoad() { // 初始化lottie動畫 wx.createSelectorQuery().select('#canvas').node(res => { const canvas = res.node const context = canvas.getContext('2d') lottie.setup(canvas) lottieInstance = lottie.loadAnimation({ path: 'https://assets10.lottiefiles.com/packages/lf20_1qfekvox.json', autoplay: true, loop: false, rendererSettings:{ context } }) }).exec() }
然后在wx.onDeviceMotionChange
中調用
lottieInstance.play()
處理觸發(fā)即可
完整代碼
https://github.com/pengjinlong/cases/tree/main/spring-article
本文轉載自:https://juejin.cn/post/7051490823497580574
作者:碼克吐溫
【相關學習推薦:小程序開發(fā)教程】
The above is the detailed content of Walk you step by step on how to achieve the 3D naked-eye carousel effect in 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)

Hot Topics

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
