PrefaceIn the development of small programs, we often encounter the need to scroll the list to view, so using anchor positioning can achieve a more friendly interactive experience. Let's take a look at the effect achieved in the project: FunctionalityImplemented using the mini-program view container component: scroll-view It should be noted here that when scrolling vertically, the scroll-view needs to specify a fixed height. Let's look at the WXML code: <scroll-view style="height:{{autoHeight}};" scroll-y scroll-into-view="{{toView}}" class="scroll-warp" scroll-with-animation="{{true}}" bindscroll="handelScroll"> <block wx:for="{{dataList}}" wx:key="{{index}}"> <view class="floorType" id="{{item.floor}}"> </view> </block> </scroll-view> Here, autoHeight is dynamically calculated based on the height of different models. Scroll-view has an important attribute: scroll-into-view, which receives a string value, which is the scrolling location (anchor point). Then we need to set the corresponding anchor point list in the scroll-view child node: such as the id attribute in the class=floorType node in the figure above. In this way, our scrolling will have a one-to-one correspondence. Click the floor in the filter block, set the toView variable to the corresponding data, and the list will scroll to the corresponding position. We can do something with the bindscroll event: this event is triggered when the list is scrolled. Key code: handelScroll(e) { let scrollTop=e.detail.scrollTop; let scrollArr = this.data.anchorArray; if(scrollTop>=scrollArr[scrollArr.length-1]-this.data.autoHeight){ return; }else { for(let i=0;i<scrollArr.length;i++){ if(scrollTop>=0&&scrollTop<scrollArr[0]){ // selectFloorIndex controls the highlighting of the filter block this.setData({ selectFloorIndex: 0 }); }else if(scrollTop>=scrollArr[i-1]&&scrollTop<scrollArr[i]) { this.setData({ selectFloorIndex: i }); } } } } Here, anchorArray is the sum of the arrays of the page height occupied by all anchor blocks. It can be seen more clearly with a picture: Each anchor block has a fixed height. After we get the data and render the page, we can use Key code: let query = wx.createSelectorQuery().in(this); let heightArr = []; let h = 0; query.selectAll('.floorType').boundingClientRect((react)=>{ react.forEach((res) => { h+=res.height; heightArr.push(h) }) this.setData({ anchorArray:heightArr }); }).exec(); At this point, our anchor point positioning scrolling has been completed. I hope it can be helpful to everyone. If you have any questions, please feel free to communicate and point them out! SummarizeThis is the end of this article about how to implement anchor point positioning in WeChat Mini Programs. For more information about WeChat Mini Program anchor point positioning, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Several ways of running in the background of Linux (summary)
>>: MySQL full-text index to achieve a simple version of the search engine example code
Table of contents 1. v-on directive 1. Basic usag...
Effect The pictures in the code can be changed by...
1. Always close HTML tags In the source code of p...
A record of an online MySQL transaction problem L...
Introduction to MySQL logical architecture Overvi...
Let's take an example: The code is very simple...
Many concepts in UI design may seem similar in wo...
I believe that people who have experience with Re...
HTML img tag: defines an image to be introduced in...
In actual development, the primary key of MySQL c...
Vue+js realizes the fade in and fade out of the v...
This article uses an example to describe the MySQ...
IE gave us a headache in the early stages of deve...
Statistics of QPS values in the last N seconds ...
Students who make websites often find that some n...