


With the popularity of mobile devices and the growth of the mobile application market, developers are increasingly learning to use cross-platform frameworks to develop applications. Uniapp is a popular cross-platform development framework. Uniapp is developed based on Vue.js and provides a series of plug-ins and components to facilitate developers to develop and debug.
However, when developing Uniapp applications, some developers discovered a problem: after the input box gets focus and the keyboard pops up, there is no animation effect when the keyboard shrinks, especially on Android devices. This problem may affect the user experience. Here are several solutions.
1. Use the transition officially provided by vue-router
vue-router is a routing management tool officially provided by Vue.js, which provides transition to achieve routing transition effects. In Uniapp, we can use the transition in vue-router to achieve the animation effect when the keyboard shrinks.
The specific implementation method is as follows:
- Add the following code in App.vue:
<template> <div> <transition> <router-view></router-view> </transition> </div> </template> <script> export default { name: 'App', components: {}, methods: {}, } </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity .5s; } .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ { opacity: 0; } </style>
- Where the animation effect needs to be achieved, For example, in a pop-up layer, add the following code in the style tag of the component:
.slide-up-enter-active, .slide-up-leave-active { transition: all .3s cubic-bezier(.55,0,.1,1); } .slide-up-enter, .slide-up-leave-to { transform: translateY(100%); opacity: 0; }
In the above code,
-
fade-enter-active
: Indicates the animation effect when entering -
fade-leave-active
: Indicates the animation effect when leaving -
fade-enter
: When entering Initial state -
fade-leave-to
: Final state when leaving
The advantage of this method is that it is simple to use and can customize the animation effect. However, it is necessary to write transition code multiple times to make the page animation take effect, which may cause code redundancy.
2. Call the system API
We can use the API provided by uni-app to call the listening event of the system keyboard to achieve the animation effect when the keyboard pops up and shrinks.
The implementation method is as follows:
- On pages that need to achieve animation effects, you can add the following code in the onLoad or onShow life cycle:
onLoad() { uni.onKeyboardHeightChange((res) => { if(res.height > 0){ //键盘弹出时 this.isShowKey = true; this.keyHeight = res.height; } else { //键盘收回时 this.isShowKey = false; } }); },
Above In the code, the uni.onKeyboardHeightChange
method can monitor when the keyboard height changes and trigger the callback function. If the height of the keyboard is greater than 0, it is judged that the keyboard pops up, and a style is added to the corresponding DOM element for animation; if the height of the keyboard is 0, it is judged that the keyboard is retracted, and the animation effect of the DOM element is cancelled.
- Add the following code in the style tag of the corresponding DOM element:
<input style="transform: translate3d(0, {{isShowKey ? -(keyHeight - 68)+'px' : '0'}}, 0);">
In the above code,
-
transform: translate3d()
: Indicates changing the position of the element -
isShowKey
: Indicates whether the keyboard pops up, if true, it means it pops up -
keyHeight-68 'px '
: Indicates the height of the keyboard minus the height of the operation bar at the bottom of the screen, and then translates upward on the original basis -
0
: Indicates the position of the element in its initial state
The advantage of this method is that it is simple to use and does not require the use of third-party plug-ins, but the animation effect when the keyboard is retracted may not be smooth enough.
3. Use third-party plug-ins
We can also use some third-party plug-ins to achieve animation effects, such as the uni-transition component in uview-ui, etc.
The implementation method is as follows:
- Download and reference the uview-ui framework.
- In the page where animation effects need to be achieved, add the following code:
<uni-transition> <div> //... </div> </uni-transition>
In the above code, the uni-transition
component can wrap the page where animation effects need to be added. Component, specify the animation type through the :name
attribute. The v-show
directive means to display the component when needed.
- Add the following code in the style tag:
.fade-enter-active, .fade-leave-active { transition: opacity .5s; } .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ { opacity: 0; }
In the above code:
-
fade-enter-active
: Indicates the animation effect when entering -
fade-leave-active
: Indicates the animation effect when leaving -
fade-enter
: When entering Initial state -
fade-leave-to
: Final state when leaving
The advantage of this method is that it is simple to use and can customize the animation effect. However, third-party plug-ins need to be introduced, which may increase the size of the project.
To sum up, the above are three methods to solve the problem of no animation effect when the Uniapp keyboard shrinks. Developers can choose the appropriate method according to their own project needs. Whether you use vue-router's transition, call system API to achieve animation effects, or use third-party plug-ins, the key is to formulate solutions based on specific situations to improve user experience and improve application quality.
The above is the detailed content of What should I do if there is no animation effect when the uniapp keyboard shrinks?. For more information, please follow other related articles on the PHP Chinese website!

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
