How to implement tabs in WeChat mini programs
The application of tabs in WeChat mini programs can be seen everywhere. This article will introduce to you the implementation of tabs in WeChat mini programs.
Ideas
I have written a swiper-based tab before, and there is a swiper component in the applet. There is no doubt that the swiper component is used here
The swiper component in the applet has a problem that it cannot adapt the height according to the content, so you need to get the device height and set the swiper height through wx.getSystemInfoSync
The swiper-item content in the swiper component in the applet cannot be scrolled after it exceeds the visible area, so another component scroll-view is used here.
#The functions of the swiper component in the mini program are still relatively limited and need to be optimized.
Plan
1. First set the data in js
data: { tabs: ['菜单一', '菜单二'],// 导航菜单栏 curIdx:0,// 当前导航索引 scrollHeight:0, //滚动高度 = 设备可视区高度 - 导航栏高度 list:[],// 内容区列表 },
Fill the data in the onLoad function
/** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let list=[]; for (let i=1;i<=30;i++){ list.push(i) } this.setData({ list: list }); },
2. Loop in WXML Render navigation
<!-- 导航栏开始 --> <view class="swiper-tab"> <view wx:for="{{tabs}}" wx:key class="swiper-tab-item {{curIdx==index?'swiper-active':''}}" data-current="{{index}}" catchtap="clickTab"> <text>{{item}}</text> </view> </view>
3. Set the current active navigation style
/*初始化样式*/ view, text, picker, input, button, image{ display: flex; box-sizing: border-box; } /* 导航样式*/ .swiper-tab { position: relative; width: 100%; height: 100rpx; justify-content: center; align-items: center; } .swiper-tab-item { background-color: #f3f3f3; width: 50%; height: 80rpx; justify-content: center; align-items: center; } .swiper-active{ background-color: rgb(129, 190, 247); color: #fff; }
4. Content display area
The content display area uses the swiper component, and the number of swiper-items must be equal to the number of tabs The array lengths are consistent
<!-- 内容开始 --> <swiper class="swiper_content" current="{{curIdx}}" bindchange="swiperTab" style='height:{{scrollHeight}}px'> <swiper-item> <scroll-view class="scroll-y" scroll-y style='height:{{scrollHeight}}px' bindscrolltolower="onReachBottom"> <view wx:for="{{list}}" wx:key> <text> 内容一{{item}}</text> </view> </scroll-view> </swiper-item> <swiper-item> 内容二 </swiper-item> </swiper>
The swiper component in the applet has a problem that it cannot adapt the height according to the content, so you need to get the device height and set the swiper height through [wx.getSystemInfoSync][4]
The swiper-item content in the swiper component in the applet cannot be scrolled after it exceeds the visual area, so another component [scroll-view][5] is used here.
We get the width and height of the device through getSystemInfoSync in the onShow function to set the swiper component height and scroll-view height
onShow: function () { // 100为导航栏swiper-tab 的高度 this.setData({ scrollHeight: wx.getSystemInfoSync().windowHeight - (wx.getSystemInfoSync().windowWidth / 750 * 100), }) },
5. Click the navigation bar to switch content
//点击切换 clickTab: function (e) { this.setData({ curIdx: e.currentTarget.dataset.current }) },
6 .Swipe the content to switch the navigation bar
//滑动切换 swiperTab: function (e) { this.setData({ curIdx: e.detail.current }); },
7.Scroll the scrollable area to the bottom to refresh the data
/** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { // 更新列表 let list = this.data.list; console.log(list) let lens = list.length for (let i = lens; i < lens+30; i++) { list.push(i) } this.setData({ list: list }); },
A beautiful tab is completed. Complete Case
The above is the entire content of this article. For more exciting information, please pay attention to the php Chinese website.
The above is the detailed content of How to implement tabs in WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!

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

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

Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the <swiper> tag to achieve the switching effect of the carousel. In this component, you can pass b

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
