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
Click here to return to the 123WORDPRESS.COM HTML ...
Table of contents 1. Add monitoring Linux host 2....
Table of contents Preface Confusing undefined and...
The reason why Docker is so popular nowadays is m...
01 Winter Flakes (Individual only) 02 Snowtop Cap...
Nginx reverse proxy multiple servers, which means...
The installation process of MySQL 8.0 Windows zip...
Since I installed the official version of IE8.0, ...
Demand scenario: The existing PXC environment has...
Preface Recently, I have been helping clients con...
1. Concept They are all attributes of Element, in...
I have a server with multiple docker containers d...
In Linux systems, especially server systems, it i...
CocosCreator version: 2.3.4 Most games have layer...
The implementation principle of Vue2.0/3.0 two-wa...