WeChat applet tab left and right sliding switch function implementation code

WeChat applet tab left and right sliding switch function implementation code

Effect picture:

insert image description here

1. Introduction

Your own applet needs to implement such a function

1. Core idea

Swiper and scroll-view share two variables: currentTab navScrollLeft. When you click nav or slide swiper, set the values ​​of the two variables to the current index.

2. Implementation

The tab navigation bar uses the <scroll-view> tag, and the content uses <swiper>

1.wxml implementation

<view class="container">
 <!-- Tab navigation bar -->
 <!-- The scroll-left attribute can control the scroll bar position-->
 <!-- scroll-with-animation scrolling adds animation transition -->
 <!-- 
  scroll-x="true"
  navScrollLeft: 0 initial value navData: tab text
  Use wx:for-item to specify the variable name of the current element of the array.
  Use wx:for-index to specify the variable name of the current index of the array:
  -->

  <!--tabs -->
 <scroll-view scroll-x="true" class="nav" scroll-left="{{navScrollLeft}}" scroll-with-animation="{{true}}">
  <block wx:for="{{navData}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx">
   <!-- Determine whether it is selected, and set the style if selected-->
   <!-- switchNav -->
   <view class="nav-item {{currentTab == idx ?'active':''}}" data-current="{{idx}}" bindtap="switchNav">
    {{navItem.text}}</view>
  </block>
 </scroll-view>


 <!-- Page Content -->
 <!-- duration="300": sliding animation duration -->
 <!-- switchTab -->
 <swiper class="tab-box" current="{{currentTab}}" duration="300" bindchange="switchTab">
  <swiper-item wx:for="{{[0,1,2,3,4,5,6]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx"
   class="tab-content">
   {{tabItem}}
  </swiper-item>
 </swiper>
</view>

2.js implementation

//index.js
//Get the application instance const app = getApp()

Page({
 data: {
  navData:[
   {
    text: 'News'
   },
   {
    text: 'Confession'
   },
   {
    text: 'Takeaway'
   },
   {
    text: 'Be a tutor'
   },
   {
    text: 'Find a tutor'
   },
   {
    text: 'rent a house'
   },
   {
    text: 'Driving school'
   }
  ],
  currentTab: 0,
  navScrollLeft: 0
 },
 //Event processing function onLoad: function () {
  
 },
 switchNav(event){
  // Get the id of the current tab
  var cur = event.currentTarget.dataset.current; 
  //Each tab option occupies 1/5 of the width
  var singleNavWidth = this.data.windowWidth / 5;

  //tab option centered this.setData({
   navScrollLeft: (cur - 2) * singleNavWidth
  }) 
  // Check if the id is consistent with the clicked tab id if (this.data.currentTab == cur) {
   return false;
  } else {
   this.setData({
    currentTab: cur
   })
  }
 },
 
 switchTab(event){
  var cur = event.detail.current;
  var singleNavWidth = this.data.windowWidth / 5;
  this.setData({
   currentTab: cur,
   navScrollLeft: (cur - 2) * singleNavWidth
  });
 }
})

3.wxss implementation

/**index.wxss**/
page {
 width: 100%;
 height: 100%;
}

.container {
 width: 100%;
 height: 100%;
}

.nav {
 /* Set tab-nav width and height*/
 height: 80rpx;
 width: 100%;

 /* If you need to place two bordered boxes side by side,
 This can be done by setting box-sizing to "border-box". */
 box-sizing: border-box;

 overflow: hidden;

 /* Center */
 line-height: 80rpx;

 background:
 #f7f7f7;

 font-size: 16px;

 /* Specifies that the text in a paragraph does not wrap: */
 white-space: nowrap;

 position: fixed;
 top: 0;
 left: 0;
 z-index: 99;
}

.nav-item {
 width: 20%;
 display: inline-block;
 text-align: center;
}

.nav-item.active {
 color:
 green;
}

.tab-box {
 background:
 rgb(31, 201, 96);
 /* Set this to the height of nav*/
 padding-top: 80rpx;
 height: 100%;
 box-sizing: border-box;
}

.tab-content {
 /* Crop the left/right edges of the content in a div element - if it overflows the element's content area: */
 overflow-y: scroll;
}

3. References and Summary

This article refers to https://www.jb51.net/article/169290.htm

Solution process
1. The width of the tab is fixed at 1/5, which can be improved to change according to the content of the tab

4. Optimization

0. Rendering

insert image description here

1. Each tab length is adaptive 2. When clicking on the previous tab

If you are currently at 1 and click 3, the path is 1-2-3. After real machine testing, it will jump directly to 3 without affecting the experience.

// *******************************Navigation bar selects to get the id and the position of the navigation bar**************************************
 tabSelect(e) {
 console.log("Result:", e);
 //Operate news database var cur = e.currentTarget.dataset.id;
 //tab option centered this.setData({
  // The id of each tab
  TabCur: e.currentTarget.dataset.id,
  
  //Adaptive scrollLeft: (e.currentTarget.dataset.id) * 60,
 })

 // Check if the id is consistent with the clicked tab id if (this.data.currentTab == cur) {
  return false;
 } else {
  this.setData({
  currentTab: cur
  })
 }
 console.log(e.currentTarget.dataset.id);
 console.log(this.data.TabCur);
 console.log("horizontal scroll bar position", this.data.scrollLeft);
 },
 switchTab(e) {
 console.log(e);
 var cur = e.detail.current;
 this.setData({
  TabCur: cur,
  scrollLeft: cur * 60,
 });
 }

This concludes this article about the implementation code of the WeChat mini-program tab sliding switching function. For more relevant mini-program tab sliding switching content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet tab switching can slide to switch the navigation bar to follow the scrolling implementation code
  • WeChat applet custom sliding top TabBar tab to achieve page switching function example
  • 10 lines of code to implement WeChat applet sliding tab switching
  • WeChat applet scroll tab to achieve left and right sliding switching
  • WeChat applet listens to gesture sliding to switch pages
  • WeChat applet swipe left and right to switch pages detailed explanation and example code

<<:  Summary of several key points about mysql init_connect

>>:  11 Linux KDE applications you didn't know about

Recommend

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

Pure CSS custom multi-line ellipsis problem (from principle to implementation)

How to display text overflow? What are your needs...

14 Ways to Create Website Content That Engages Your Visitors

When I surf the Net, I often see web sites filled...

Flash embedded in web pages and IE, FF, Maxthon compatibility issues

After going through a lot of hardships, I searched...

3 ways to create JavaScript objects

Table of contents 1. Object literals 2. The new k...

How to connect idea to docker to achieve one-click deployment

1. Modify the docker configuration file and open ...

Solve the problem of ugly blue border after adding hyperlink to html image img

HTML img produces an ugly blue border after addin...

What you need to understand about MySQL locks

1. Introduction MySQL locks can be divided into g...

Common naming rules for CSS classes and ids

Public name of the page: #wrapper - - The outer e...

How to completely uninstall mysql under CentOS

This article records the complete uninstallation ...

Centos8.3, docker deployment springboot project actual case analysis

introduction Currently, k8s is very popular, and ...

How to configure anti-hotlinking for nginx website service (recommended)

1. Principle of Hotlinking 1.1 Web page preparati...

Install and configure ssh in CentOS7

1. Install openssh-server yum install -y openssl ...

Detailed example of SpringBoot+nginx to achieve resource upload function

Recently, I have been learning to use nginx to pl...

HTML table tag tutorial (24): horizontal alignment attribute of the row ALIGN

In the horizontal direction, you can set the row ...