Effect picture: 1. IntroductionYour 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. ImplementationThe 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 SummaryThis article refers to https://www.jb51.net/article/169290.htm Solution process 4. Optimization0. Rendering 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:
|
<<: Summary of several key points about mysql init_connect
>>: 11 Linux KDE applications you didn't know about
What are the benefits of learning HTML? 1: Easily...
How to display text overflow? What are your needs...
When I surf the Net, I often see web sites filled...
After going through a lot of hardships, I searched...
Table of contents 1. Object literals 2. The new k...
1. Modify the docker configuration file and open ...
HTML img produces an ugly blue border after addin...
1. Introduction MySQL locks can be divided into g...
Public name of the page: #wrapper - - The outer e...
This article records the complete uninstallation ...
introduction Currently, k8s is very popular, and ...
1. Principle of Hotlinking 1.1 Web page preparati...
1. Install openssh-server yum install -y openssl ...
Recently, I have been learning to use nginx to pl...
In the horizontal direction, you can set the row ...