Home > Web Front-end > uni-app > How to implement tabbar sliding switching in UniAPP

How to implement tabbar sliding switching in UniAPP

PHPz
Release: 2023-04-27 10:19:46
Original
3666 people have browsed it

With the continuous advancement of the development of mobile Internet, the development of APP applications has attracted more and more attention. In APP development, tabbar, as a common page design pattern, is widely used in various APPs. In this design pattern, different page switches are usually performed by clicking the tabbar. But for users who want to quickly browse different pages, sliding to switch may be a better choice.

As a cross-platform development tool, UniAPP provides us with a simple way to make tabbar sliding switching. This article will introduce how UniAPP implements tabbar sliding switching, with detailed sample code.

1. Implementation ideas

The effect we want to achieve is that in the tabbar page, when the user slides left or right, he can automatically switch to the corresponding page. This process can be achieved through the swiper component in UniAPP, and the code is very simple. We just need to do some configuration to make the sliding switch take effect.

2. Implementation steps

  1. First, find the tabbar page in the project. You can find and open the corresponding vue file in the pages directory.
  2. In the template section, add the swiper component. You can use the uni-swiper tag to add it. The sample code is as follows:
<template>
  <view>
    <uni-swiper :current="current" :duration="300" :circular="false" :autoplay="false" @change="swiperChange">
      <uni-swiper-item v-for="(item, index) of tabBarList" :key="item.pagePath">
        <component :is="item.pagePath" ref="pageRef"></component>
      </uni-swiper-item>
    </uni-swiper>
  </view>
</template>
Copy after login

In this code, we use uni- swiper is used to implement the sliding function, and the current attribute is used to set the current page. Circular is set to false to indicate that the carousel will not cycle, and autoplay is set to false to indicate that it will not play automatically. In addition, we also added a component component to each swiper-item to represent the content of each page in the tabbar.

  1. In the script part, we need to configure the tabBar and add some necessary functions and variables. We need the following code:
<script>
export default {
  data() {
    return {
      current: 0,
      tabBarList: [
        {
          text: '首页',
          iconPath: '/static/tabbar/home.png',
          selectedIconPath: '/static/tabbar/home-active.png',
          pagePath: '/pages/index/index',
        },
        {
          text: '分类',
          iconPath: '/static/tabbar/category.png',
          selectedIconPath: '/static/tabbar/category-active.png',
          pagePath: '/pages/category/category',
        },
        {
          text: '购物车',
          iconPath: '/static/tabbar/cart.png',
          selectedIconPath: '/static/tabbar/cart-active.png',
          pagePath: '/pages/cart/cart',
        },
        {
          text: '我的',
          iconPath: '/static/tabbar/user.png',
          selectedIconPath: '/static/tabbar/user-active.png',
          pagePath: '/pages/mine/mine',
        },
      ],
    };
  },
  methods: {
    swiperChange(e) {
      this.current = e.detail.current;
      uni.switchTab({
        url: this.tabBarList[this.current].pagePath,
      });
    },
  },
};
</script>
Copy after login

In this code, we Four tabBar pages are configured and a current variable is defined to record the current page. At the same time, we defined a function called swiperChange to listen for page change events. In the swiperChange function, we use the uni.switchTab function to switch the current page to the corresponding page.

In this way, we have completed the production of tabbar sliding switching and can preview and debug.

3. Summary

This article introduces UniAPP’s method of implementing tabbar sliding switching and gives detailed sample code. By learning and practicing these codes, we can quickly create our own APP applications and provide users with a better user experience. At the same time, UniAPP, as a cross-platform development tool, can also support running on multiple platforms, which greatly reduces our development difficulty and workload.

The above is the detailed content of How to implement tabbar sliding switching in UniAPP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template