PrefaceThere are more than 4,000 popular stock lists, which need to be rendered on the page and updated in real time during trading. If interfaces and paging are used, the page will become more and more stuck when scrolling down dozens of pages and must be updated in real time. The final approach is to pass the data from ws at the beginning. We only need to pass the starting and ending subscripts, and we only generate a few tags on the page. Greatly reduces rendering pressure. What is a virtual list?It only refers to rendering the labels in the visible area, constantly switching the start and end subscripts to update the view when scrolling, and calculating the offset at the same time. Demo effectPreparation
Screen height & box heightIn the applet we can use wx.createSelectorQuery to get the screen height and the box height. getEleInfo(ele) { return new Promise( ( resolve, reject ) => { const query = wx.createSelectorQuery().in(this); query.select(ele).boundingClientRect(); query.selectViewport().scrollOffset(); query.exec( res => { resolve(res); }) }) }, this.getEleInfo('.stock').then( res => { if (!res[0]) return; // Screen height this.screenHeight = res[1].scrollHeight; //Box height this.boxhigh = res[0].height; }) Start & End & Offset onPageScroll(e) { let { scrollTop } = e; this.start = Math.floor(scrollTop / this.boxhigh); this.end = this.start + this.visibleCount; this.offsetY = this.start * this.boxhigh; } scrollTop can be understood as how many boxes have been scrolled from the top / the height of the box = the starting subscript Start + How many boxes can be displayed in the visible area of the page = End Start * Box Height = Offset computed: { visibleData() { return this.data.slice(this.start, Math.min(this.end, this.data.length)) }, } When start and end change, we will use slice(this.start, this.end) to change the data, so that the content of the label can be replaced in time. optimizationWhen scrolling quickly, a blank area will appear at the bottom because the data has not been loaded yet. We can render three screens to ensure that the data is loaded in a timely manner when sliding. prevCount() { return Math.min(this.start, this.visibleCount); }, nextCount() { return Math.min(this.visibleCount, this.data.length - this.end); }, visibleData() { let start = this.start - this.prevCount, end = this.end + this.nextCount; return this.data.slice(start, Math.min(end, this.data.length)) }, If you have calculated the front screen reserved offset, you need to modify it as follows: this.offsetY = this.start * this.boxhigh - this.boxhigh * this.prevCount When sliding, start, end, and offsetY are constantly changing. Frequent calls to setData may cause the applet to exceed memory and crash. Here we throttle when sliding to dilute the execution frequency of setData. mounted() { this.deliquate = this.throttle(this.changeDeliquate, 130) }, methods: { throttle(fn, time) { var previous = 0; return function(scrollTop) { let now = Date.now(); if ( now - previous > time ) { fn(scrollTop) previous = now; } } }, changeDeliquate(scrollTop) { this.start = Math.floor(scrollTop / this.boxhigh); this.end = this.start + this.visibleCount; this.offsetY = this.start * this.boxhigh; console.log('Execute setData') } }, onPageScroll(e) { let { scrollTop } = e; console.log('Scroll =================>>>>>>>') this.deliquate(scrollTop); } As can be seen from the above figure, each scroll reduces the writing of setData by at least two times. The demo written in the article is here, you can refer to it if you need it. SummarizeThis is the end of this article about the WeChat mini-program virtual list application. For more relevant mini-program virtual list 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:
|
<<: Introduction to building a DNS server under centos7
>>: Tutorial on how to modify element.style inline styles
There are too much knowledge to learn recently, a...
usemap is an attribute of the <img> tag, use...
When playing music, the lyrics will gradually fil...
Description of the phenomenon: The project uses s...
About semantics Semantics is the study of the rel...
Students who make websites often find that some n...
This article introduces a tutorial about how to u...
Table of contents Ideas Request interception Resp...
introduction You must have encountered this in an...
Table of contents 3 ways to deploy projects with ...
MVCC MVCC (Multi-Version Concurrency Control) is ...
Nginx (engine x) is a high-performance HTTP and r...
Introduction: In many cases, many people think th...
This article is based on the Windows 10 system en...
If you want to transfer files between Windows and...