search
HomeWeb Front-enduni-appHow to make transition animation in uniapp

With the popularity of mobile device applications, colorful dynamic effects have become an essential element for many application development. Among them, transition animation is an important means to improve user experience. In the cross-platform application development framework uniapp, it is also very simple and easy to implement transition animation.

Transition animations in uniapp can be divided into two categories: native transitions and custom transitions. Native transitions are the default transition effects of the system, while custom transitions can be customized according to your own needs.

1. Native transition animation

The implementation of native transition animation in uniapp is very simple. You only need to add "animationType in the pages.json configuration file " attributes are sufficient. The following are several common transition animation effects:

  1. Push

When jumping to page B through uni.navigateTo in page A , you can set the transition animation to Push:

uni.navigateTo({
  url: '/pages/b-page/b-page',
  animationType: 'push',
  animationDuration: 500
});

The effect is as follows:

How to make transition animation in uniapp

  1. Pop

on page B When returning to page A through uni.navigateBack, you can set the transition animation to Pop:

uni.navigateBack({
  animationType: 'pop',
  animationDuration: 500
});

The effect is as follows:

How to make transition animation in uniapp

  1. Fade

You can set the transition animation to the fade effect of fading in and out:

uni.navigateTo({
  url: '/pages/b-page/b-page',
  animationType: 'fade',
  animationDuration: 500
});

The effect is as follows:

How to make transition animation in uniapp

  1. None

You can set the transition animation to None with no effect:

uni.navigateTo({
  url: '/pages/b-page/b-page',
  animationType: 'none',
  animationDuration: 500
});

The effect is as follows:

How to make transition animation in uniapp

2. Custom transition animation

The custom transition animation in uniapp needs to be implemented in combination with the uni-app-plus plug-in and vue-router routing component. . Let’s introduce the implementation process of custom transition animation in detail.

  1. Install the uni-app-plus plug-in

Using the uni-app-plus plug-in allows us to use some native APIs and APIs in uniapp Plug-ins, including UIKit in iOS and android.view in Android. Therefore, when using custom transition animation, we need to use this plug-in.

Execute the following command in the project directory to install:

npm install uni-app-plus --save-dev
  1. Modify vue-router configuration

First, we need to install it in router .jsAdd routing guards to the configuration file so that we can capture the event of jumping from page A to page B to achieve custom transition animation.

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (to.meta.animation === 'custom') {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const prevPage = pages[pages.length - 2];
    currentPage.animation = 'slide-left';
    prevPage.animation = 'slide-right';
  }
  next();
});

export default router;

In this code, we added a routing guardbeforeEach. When the page to be jumped to is configured with a custom transition animation, the current page and the previous page will be The animation effect is set to slide left and right, so that you can implement custom transition animation.

  1. Implementing custom transition animation

In the App.vue file, we can implement custom transitions by listening to page switching events animation. First, we add the following code in the mounted life cycle:

mounted() {
  if (uni.getSystemInfoSync().platform === 'ios') {
    const router = this.$router;
    router.beforeEach(function(to, from, next) {
      if (from.meta.animation === 'custom') {
        UniViewJSBridge.publishHandler('animation', {
          type: 'set',
          pageParam: {
            animationEnabled: true
          }
        }, function() {
          router.app.animation = uni.createFromIconfontCN({
            scriptUrl: "//at.alicdn.com/t/font_2581006_ourmsf7tpoi.js"
          }).css({
            animationDuration: '0.4s',
            animationTimingFunction: 'ease-in'
          }).toStyle();
          next();
        })
      } else {
        router.app.animation = '';
        next();
      }
    });
    UniViewJSBridge.subscribeHandler('animation', function(dat) {
      if (dat.type === 'finish') {
        router.app.animation = '';
      }
    });
  }
},

The above code mainly implements the following functions:

  1. Detect whether the current device is an iOS device ( Because Android devices support custom transition animations by default), if so, perform the following steps. If not, skip this process directly.
  2. Send a message to the native through UniViewJSBridge before the routing changes, telling it that animation needs to be turned on.
  3. Listen to the message sent by UniViewJSBridge. When the native animation execution ends, assign router.app.animation to an empty string, indicating that the animation effect has ended.

Then, add the following code in the <template></template> tag:

<view :class="{ 'animated': animation }">
  <router-view class="page"></router-view>
</view>

Here we use the animation library animate.css To achieve the animation effect, it needs to be introduced in the page:

<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.5.2/animate.min.css">

Finally, add the following code in the <script></script> tag:

data() {
  return {
    animation: ''
  };
},

Before entering page A , set transType to "custom":

uni.navigateTo({
  url: '/pages/b-page/b-page',
  animationType: 'pop',
  animationDuration: 500,
  events: {
    finish: () => {
      console.log('finish');
    }
  },
  complete: () => {
    setTimeout(() => {
      this.animation = '';
    }, 500);
  },
  fail: () => {
    console.log('fail');
  },
  transType: 'custom'
});

In this way, we have completed the entire process of customizing the transition animation. In actual development, you can also define animation types and animation effects according to your own needs.

Summary

In uniapp, it is very simple to implement transition animation. We can use the native transition animation, or we can combine the uni-app-plus plug-in and vue-routerRouting component to implement custom transition animation. During the development process, we need to choose different transition animation effects based on actual needs to improve the user experience.

The above is the detailed content of How to make transition animation in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)