WeChat applet implementation anchor positioning function example

WeChat applet implementation anchor positioning function example

Preface

In 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:

Functionality

Implemented 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.
When we scroll the list, the highlighted filter items at the top also need to be switched accordingly. What should we do at this time?

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
The boundingClientRect method gets the height of each node with the class name floorType and puts these heights into the anchorArray array. Then, when we scroll beyond the height of a certain anchor point, the top filter item will also switch to the next one.

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!

Summarize

This 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:
  • WeChat applet implements anchor point positioning floor jump example
  • WeChat applet scroll-view implementation anchor sliding example
  • WeChat applet implements anchor function
  • WeChat applet scroll-view realizes the function of scrolling to the anchor point on the left navigation bar to order food (click on the type and scroll to the anchor point)
  • WeChat applet realizes anchor point jump
  • WeChat applet scroll-view implements anchor point jump function
  • WeChat applet scroll-view anchor link scroll jump function

<<:  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

Recommend

Summary of methods for inserting videos into HTML pages

Now if you want to use the video tag in a page, y...

Element dynamic routing breadcrumbs implementation example

To master: localStorage, component encapsulation ...

Two ways to implement square div using CSS

Goal: Create a square whose side length is equal ...

Using zabbix to monitor the ogg process (Linux platform)

The ogg process of a database produced some time ...

In-depth understanding of Vue transition and animation

1. When inserting, updating, or removing DOM elem...

A guide to writing flexible, stable, high-quality HTML and CSS code standards

The Golden Rule Always follow the same set of cod...

How to fix the width of table in ie8 and chrome

When the above settings are used in IE8 and Chrome...

Solution to the problem of insufficient storage resource pool of Docker server

Table of contents 1. Problem Description 2. Probl...

Python MySQL database table modification and query

Python connects to MySQL to modify and query data...

CSS Tricks to Create Wave Effects

It has always been very difficult to achieve wave...