Design IntentionsWhen developing a page, you often need to scroll the page to the corresponding position when clicking the navigation menu on the page, and scroll the page to highlight the menu options. In HTML development, we can use the a tag anchor implementation and combine it with jq animation to achieve a similar effect. In the framework, the vant UI framework also achieves this effect for us. How to implement WeChat mini program? ? Effect display
Design ideas1. Realization of ceiling effect
1) Distance const query = wx.createSelectorQuery() query.select('.menu_nav').boundingClientRect(function(res) { let obj = {} if (res && res.top) { obj[item.attr] = parseInt(res.top) } }).exec()
selector syntax
Property Type Description id string ID of the node dataset Object dataset of the node left number left boundary coordinate of the node right number right boundary coordinate of the node top number upper boundary coordinate of the node bottom number lower boundary coordinate of the node width number width of the node height number height of the node
2) Page scroll monitoring
// Listen for page scrolling onPageScroll: function(e) { let hTop = parseInt(e.scrollTop) // Does the menu need to be positioned at the top if (hTop > this.data.menu_top) { this.setData({ tabFixed: true }) } else { this.setData({ tabFixed: false }) } } onPageScroll(Object object)) ParametersObject object:
Note: Please define this method in the page only when necessary, and do not define an empty method. To reduce the impact of unnecessary event dispatch on rendering layer-logic layer communication. Note: Please avoid executing operations such as setData too frequently in onPageScroll that cause communication between the logic layer and the rendering layer. In particular, when a large amount of data is transmitted each time, it will affect the communication time. 2. Switch to the corresponding area
// Navigation bar switch settings setSelectType(event) { let index = event.currentTarget.dataset.type this.setData({ tabIndex: index, }) let arr = ['panel1_top', 'panel2_top', 'panel3_top', 'panel4_top'] let _this = this wx.pageScrollTo({ scrollTop: _this.data[arr[index]], duration: 500 }) }, wx.pageScrollTo(Object object)
3) When you scroll to a certain area, the menu button of the corresponding area is highlighted Get the initial distance between the area and the top let arr = [ { name: '.menu-nav', attr: 'menu_top', addNum: 0 }, { name: '.panel1', attr: 'panel1_top', addNum: 0 }, { name: '.panel2', attr: 'panel2_top', addNum: 0 }, { name: '.panel3', attr: 'panel3_top', addNum: 0 }, { name: '.panel4', attr: 'panel4_top', addNum: 0 }, ] arr.forEach((item, i) => { wx.createSelectorQuery().select(item.name).boundingClientRect(function(res) { let obj = {} if (res && res.top) { obj[item.attr] = parseInt(res.top) if (item.addNum) { obj[item.attr] += item.addNum } that.setData({ ...obj }) } }).exec() }) Check if the scrolling exceeds the area // Listen for page scrolling onPageScroll: function(e) { let hTop = parseInt(e.scrollTop) // Automatically switch menu let tab=0 if (hTop >= (this.data['panel4_top'] - this.data.menu_top)) { tab=3 }else if (hTop >= (this.data['panel3_top'] - this.data.menu_top)){ tab=2 } else if (hTop >= (this.data['panel2_top'] - this.data.menu_top)){ tab=1 } this.setData({ tabIndex: tab, }) }, Complete code index.js // pages/index/index.js Page({ /** * Initial data of the page */ data: { tabIndex: 0, //Current menu menuList: ['Menu 1', 'Menu 2', 'Menu 3', 'Menu 4'], //Navigation menu tabFixed: false, //Whether to position// The distance from the top of the initial page menu_top: 0, panel1_top: 0, panel2_top: 0, panel3_top: 0, panel4_top: 0, }, /** * Life cycle function--listen for page loading*/ onLoad: function (options) { }, onShow:function (options){ this.getTopDistance() }, // Get the height from the top of the page getTopDistance() { let that = this let arr = [{ name: '.menu-nav', attr: 'menu_top', addNum: 0 }, { name: '.panel1', attr: 'panel1_top', addNum: 0 }, { name: '.panel2', attr: 'panel2_top', addNum: 0 }, { name: '.panel3', attr: 'panel3_top', addNum: 0 }, { name: '.panel4', attr: 'panel4_top', addNum: 0 }, ] arr.forEach((item, i) => { wx.createSelectorQuery().select(item.name).boundingClientRect(function (res) { let obj = {} if (res && res.top) { obj[item.attr] = parseInt(res.top) if (item.addNum) { obj[item.attr] += item.addNum } that.setData({ ...obj }) } }).exec() }) }, // Navigation bar switch settings setSelectType(event) { let index = event.currentTarget.dataset.type this.setData({ tabIndex: index, }) let arr = ['panel1_top', 'panel2_top', 'panel3_top', 'panel4_top'] let _this = this wx.pageScrollTo({ scrollTop: _this.data[arr[index]], duration: 500 }) }, // Listen for page scrolling onPageScroll: function (e) { let hTop = parseInt(e.scrollTop) // Does the menu need to be positioned at the top if (hTop > this.data.menu_top) { this.setData({ tabFixed: true }) } else { this.setData({ tabFixed: false }) } // Automatically switch menu if (hTop >= (this.data['panel4_top'] - this.data.menu_top)) { this.setData({ tabIndex: 3, }) }else if (hTop >= (this.data['panel3_top'] - this.data.menu_top)){ this.setData({ tabIndex: 2, }) } else if (hTop >= (this.data['panel2_top'] - this.data.menu_top)){ this.setData({ tabIndex: 1, }) }else{ this.setData({ tabIndex: 0, }) } }, }) index.wxml <view class="Main"> <view class="head"> I am the head area</view> <view class="{{tabFixed?'is-fixed':''}} menu-nav"> <text wx:for="{{menuList}}" class="{{tabIndex==index?'is-select':''}}" bind:tap="setSelectType" data-type='{{index}}'>{{item}}</text> </view> <view class="content"> <view class="panel1 panel">Page 1</view> <view class="panel2 panel">Page 2</view> <view class="panel3 panel">Page 3</view> <view class="panel4 panel">Page 4</view> </view> </view> index.wxss .menu-nav { display: flex; align-items: center; justify-content: space-around; color: black; padding: 10px 0; width: 100%; background-color: white; } .is-select { color: red; } .head { display: flex; align-items: center; justify-content: center; font-size: 40px; height: 120px; background-color: greenyellow; } .is-fixed { position: fixed; top: 0; } .panel { display: flex; align-items: center; justify-content: center; font-size: 20px; } .panel1 { height: 800rpx; background-color: rebeccapurple; } .panel2 { height: 700rpx; background-color: blue; } .panel3 { height: 1000rpx; background-color: orange; } .panel4 { height: 1200rpx; background-color: pink; } This is the end of this article about WeChat Mini Program - Customized Menu Navigation (Realizing Stair Effect). For more relevant WeChat Mini Program customized menu navigation 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:
|
<<: Analysis and solution of the reasons why MySQL scheduled tasks cannot be executed normally
>>: How to convert Chinese into UTF-8 in HTML
This article shares the specific code of JavaScri...
How to modify the style of the el-select componen...
Table of contents Import on demand: Global Import...
Table of contents Preface 1. Arrange the installa...
HTML is a hybrid language used for publishing on ...
This project shares the specific code of Vue+Rout...
Form validation is one of the most commonly used ...
When installing FileZilla Server on the server, t...
1. Previous versions yum remove docker docker-cli...
background In order to support Docker containeriz...
Implementation ideas: First of all, the alarm inf...
First execute the command: [root@mini61 setuptool...
Composition inheritance Combination inheritance i...
Today, when I was on the road, a colleague sent m...
About Event: MySQL 5.1 began to introduce the con...