{"id":33610,"date":"2024-11-25T13:38:44","date_gmt":"2024-11-25T05:38:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33610\/"},"modified":"2024-11-25T13:38:44","modified_gmt":"2024-11-25T05:38:44","slug":"%e5%9c%a8%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b8%ad%e5%ae%9e%e7%8e%b0virtual-list%e7%9a%84%e6%96%b9%e6%b3%95%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33610\/","title":{"rendered":"\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/5f55e5714e09e422.jpg\" class=\"aligncenter\" title=\"\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3\u63d2\u56fe\" alt=\"\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p><strong>\u3010\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a<\/strong><strong>\u3011<\/strong><\/p>\n<h2>\u80cc\u666f<\/h2>\n<p>\u5c0f\u7a0b\u5e8f\u5728\u5f88\u591a\u573a\u666f\u4e0b\u9762\u4f1a\u9047\u5230\u957f\u5217\u8868\u7684\u4ea4\u4e92\uff0c\u5f53\u4e00\u4e2a\u9875\u9762\u6e32\u67d3\u8fc7\u591a\u7684wxml\u8282\u70b9\u7684\u65f6\u5019\uff0c\u4f1a\u9020\u6210\u5c0f\u7a0b\u5e8f\u9875\u9762\u7684\u5361\u987f\u548c\u767d\u5c4f\u3002\u539f\u56e0\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<p>1.\u5217\u8868\u6570\u636e\u91cf\u5927\uff0c\u521d\u59cb\u5316setData\u548c\u521d\u59cb\u5316\u6e32\u67d3\u5217\u8868wxml\u8017\u65f6\u90fd\u6bd4\u8f83\u957f\uff1b<\/p>\n<p>2.\u6e32\u67d3\u7684wxml\u8282\u70b9\u6bd4\u8f83\u591a\uff0c\u6bcf\u6b21setData\u66f4\u65b0\u89c6\u56fe\u90fd\u9700\u8981\u521b\u5efa\u65b0\u7684\u865a\u62df\u6811\uff0c\u548c\u65e7\u6811\u7684diff\u64cd\u4f5c\u8017\u65f6\u6bd4\u8f83\u9ad8\uff1b<\/p>\n<p>3.\u6e32\u67d3\u7684wxml\u8282\u70b9\u6bd4\u8f83\u591a\uff0cpage\u80fd\u591f\u5bb9\u7eb3\u7684wxml\u662f\u6709\u9650\u7684\uff0c\u5360\u7528\u7684\u5185\u5b58\u9ad8\u3002<\/p>\n<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u672c\u8eab\u7684scroll-view\u6ca1\u6709\u9488\u5bf9\u957f\u5217\u8868\u505a\u4f18\u5316\uff0c\u5b98\u65b9\u7ec4\u4ef6recycle-view\u5c31\u662f\u4e00\u4e2a\u7c7b\u4f3cvirtual-list\u7684\u957f\u5217\u8868\u7ec4\u4ef6\u3002\u73b0\u5728\u6211\u4eec\u8981\u5256\u6790\u865a\u62df\u5217\u8868\u7684\u539f\u7406\uff0c\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\u7684virtual-list\u3002<\/p>\n<h2>\u5b9e\u73b0\u539f\u7406<\/h2>\n<p>\u9996\u5148\u6211\u4eec\u8981\u4e86\u89e3\u4ec0\u4e48\u662fvirtual-list\uff0c\u8fd9\u662f\u4e00\u79cd\u521d\u59cb\u5316\u53ea\u52a0\u8f7d\u300c\u53ef\u89c6\u533a\u57df\u300d\u53ca\u5176\u9644\u8fd1dom\u5143\u7d20\uff0c\u5e76\u4e14\u5728\u6eda\u52a8\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u590d\u7528dom\u5143\u7d20\u53ea\u6e32\u67d3\u300c\u53ef\u89c6\u533a\u57df\u300d\u53ca\u5176\u9644\u8fd1dom\u5143\u7d20\u7684\u6eda\u52a8\u5217\u8868\u524d\u7aef\u4f18\u5316\u6280\u672f\u3002\u76f8\u6bd4\u4f20\u7edf\u7684\u5217\u8868\u65b9\u5f0f\u53ef\u4ee5\u5230\u8fbe\u6781\u9ad8\u7684\u521d\u6b21\u6e32\u67d3\u6027\u80fd\uff0c\u5e76\u4e14\u5728\u6eda\u52a8\u8fc7\u7a0b\u4e2d\u53ea\u7ef4\u6301\u8d85\u8f7b\u91cf\u7684dom\u7ed3\u6784\u3002<\/p>\n<p>\u865a\u62df\u5217\u8868\u6700\u91cd\u8981\u7684\u51e0\u4e2a\u6982\u5ff5\uff1a<\/p>\n<ul>\n<li>\n<p>\u53ef\u6eda\u52a8\u533a\u57df\uff1a\u6bd4\u5982\u5217\u8868\u5bb9\u5668\u7684\u9ad8\u5ea6\u662f600\uff0c\u5185\u90e8\u5143\u7d20\u7684\u9ad8\u5ea6\u4e4b\u548c\u8d85\u8fc7\u4e86\u5bb9\u5668\u9ad8\u5ea6\uff0c\u8fd9\u4e00\u5757\u533a\u57df\u5c31\u53ef\u4ee5\u6eda\u52a8\uff0c\u5c31\u662f\u300c\u53ef\u6eda\u52a8\u533a\u57df\u300d\uff1b<\/p>\n<\/li>\n<li>\n<p>\u53ef\u89c6\u533a\u57df\uff1a\u6bd4\u5982\u5217\u8868\u5bb9\u5668\u7684\u9ad8\u5ea6\u662f600\uff0c\u53f3\u4fa7\u6709\u7eb5\u5411\u6eda\u52a8\u6761\u53ef\u4ee5\u6eda\u52a8\uff0c\u89c6\u89c9\u53ef\u89c1\u7684\u5185\u90e8\u533a\u57df\u5c31\u662f\u300c\u53ef\u89c6\u533a\u57df\u300d\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5b9e\u73b0\u865a\u62df\u5217\u8868\u7684\u6838\u5fc3\u5c31\u662f\u76d1\u542cscroll\u4e8b\u4ef6\uff0c\u901a\u8fc7\u6eda\u52a8\u8ddd\u79bboffset\u548c\u6eda\u52a8\u7684\u5143\u7d20\u7684\u5c3a\u5bf8\u4e4b\u548ctotalSize\u52a8\u6001\u8c03\u6574\u300c\u53ef\u89c6\u533a\u57df\u300d\u6570\u636e\u6e32\u67d3\u7684\u9876\u90e8\u8ddd\u79bb\u548c\u524d\u540e\u622a\u53d6\u7d22\u5f15\u503c\uff0c\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>1.\u76d1\u542cscroll\u4e8b\u4ef6\u7684scrollTop\/scrollLeft\uff0c\u8ba1\u7b97\u300c\u53ef\u89c6\u533a\u57df\u300d\u8d77\u59cb\u9879\u7684\u7d22\u5f15\u503cstartIndex\u548c\u7ed3\u675f\u9879\u7d22\u5f15\u503cendIndex\uff1b<\/p>\n<p>2.\u901a\u8fc7startIndex\u548cendIndex\u622a\u53d6\u957f\u5217\u8868\u7684\u300c\u53ef\u89c6\u533a\u57df\u300d\u7684\u6570\u636e\u9879\uff0c\u66f4\u65b0\u5230\u5217\u8868\u4e2d\uff1b<\/p>\n<p>3.\u8ba1\u7b97\u53ef\u6eda\u52a8\u533a\u57df\u7684\u9ad8\u5ea6\u548citem\u7684\u504f\u79fb\u91cf\uff0c\u5e76\u5e94\u7528\u5728\u53ef\u6eda\u52a8\u533a\u57df\u548citem\u4e0a\u3002<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/b4e89800c9f08f9a8ef3336dc0c1ea05-0.png\" style=\"max-width:90%\" title=\"\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3\u63d2\u56fe1\" alt=\"\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3\u63d2\u56fe1\" \/> <\/p>\n<h3>1.\u5217\u8868\u9879\u7684\u5bbd\/\u9ad8\u548c\u6eda\u52a8\u504f\u79fb\u91cf<\/h3>\n<p>\u5728\u865a\u62df\u5217\u8868\u4e2d\uff0c\u4f9d\u8d56\u6bcf\u4e00\u4e2a\u5217\u8868\u9879\u7684\u5bbd\/\u9ad8\u6765\u8ba1\u7b97\u300c\u53ef\u6eda\u52a8\u533a\u57df\u300d\uff0c\u800c\u4e14\u53ef\u80fd\u662f\u9700\u8981\u81ea\u5b9a\u4e49\u7684\uff0c\u5b9a\u4e49itemSizeGetter\u51fd\u6570\u6765\u8ba1\u7b97\u5217\u8868\u9879\u5bbd\/\u9ad8\u3002<\/p>\n<pre>itemSizeGetter(itemSize)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(index:&nbsp;number)&nbsp;=&amp;gt;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isFunction(itemSize))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;itemSize(index);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;isArray(itemSize)&nbsp;?&nbsp;itemSize[index]&nbsp;:&nbsp;itemSize;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};\n&nbsp;&nbsp;&nbsp;&nbsp;}\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6eda\u52a8\u8fc7\u7a0b\u4e2d\uff0c\u4e0d\u4f1a\u8ba1\u7b97\u6ca1\u6709\u51fa\u73b0\u8fc7\u7684\u5217\u8868\u9879\u7684itemSize\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f1a\u4f7f\u7528\u4e00\u4e2a\u9884\u4f30\u7684\u5217\u8868\u9879estimatedItemSize\uff0c\u76ee\u7684\u5c31\u662f\u5728\u8ba1\u7b97\u300c\u53ef\u6eda\u52a8\u533a\u57df\u300d\u9ad8\u5ea6\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u6d4b\u91cf\u8fc7\u7684itemSize\u7528estimatedItemSize\u4ee3\u66ff\u3002<\/p>\n<pre>getSizeAndPositionOfLastMeasuredItem()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.lastMeasuredIndex&nbsp;&amp;gt;=&nbsp;0\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;this.itemSizeAndPositionData[this.lastMeasuredIndex]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;{&nbsp;offset:&nbsp;0,&nbsp;size:&nbsp;0&nbsp;};\n&nbsp;&nbsp;}\n\ngetTotalSize():&nbsp;number&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredSizeAndPosition&nbsp;=&nbsp;this.getSizeAndPositionOfLastMeasuredItem();&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastMeasuredSizeAndPosition.offset&nbsp;+\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastMeasuredSizeAndPosition.size&nbsp;+\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(this.itemCount&nbsp;-&nbsp;this.lastMeasuredIndex&nbsp;-&nbsp;1)&nbsp;*&nbsp;this.estimatedItemSize\n&nbsp;&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;}\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u770b\u5230\u4e86\u662f\u76f4\u63a5\u901a\u8fc7\u7f13\u5b58\u547d\u4e2d\u6700\u8fd1\u4e00\u4e2a\u8ba1\u7b97\u8fc7\u7684\u5217\u8868\u9879\u7684itemSize\u548coffset\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u83b7\u53d6\u6bcf\u4e00\u4e2a\u5217\u8868\u9879\u7684\u4e24\u4e2a\u53c2\u6570\u65f6\u5019\uff0c\u90fd\u5bf9\u5176\u505a\u4e86\u7f13\u5b58\u3002<\/p>\n<pre>&nbsp;getSizeAndPositionForIndex(index:&nbsp;number)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(index&nbsp;&amp;gt;&nbsp;this.lastMeasuredIndex)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredSizeAndPosition&nbsp;=&nbsp;this.getSizeAndPositionOfLastMeasuredItem();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;offset&nbsp;=\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastMeasuredSizeAndPosition.offset&nbsp;+&nbsp;lastMeasuredSizeAndPosition.size;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(let&nbsp;i&nbsp;=&nbsp;this.lastMeasuredIndex&nbsp;+&nbsp;1;&nbsp;i&nbsp;&lt;h3 data-id=\"heading-3\"&gt;2.\u6839\u636e\u504f\u79fb\u91cf\u641c\u7d22\u7d22\u5f15\u503c&lt;\/h3&gt;&lt;p&gt;\u5728\u6eda\u52a8\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u901a\u8fc7\u6eda\u52a8\u504f\u79fb\u91cfoffset\u8ba1\u7b97\u51fa\u5c55\u793a\u5728\u300c\u53ef\u89c6\u533a\u57df\u300d\u9996\u9879\u6570\u636e\u7684\u7d22\u5f15\u503c\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4ece0\u5f00\u59cb\u8ba1\u7b97\u6bcf\u4e00\u5217\u8868\u9879\u7684itemSize\uff0c\u7d2f\u52a0\u5230\u4e00\u65e6\u8d85\u8fc7offset\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e2a\u7d22\u5f15\u503c\u3002\u4f46\u662f\u5728\u6570\u636e\u91cf\u592a\u5927\u548c\u9891\u7e41\u89e6\u53d1\u7684\u6eda\u52a8\u4e8b\u4ef6\u4e2d\uff0c\u4f1a\u6709\u8f83\u5927\u7684\u6027\u80fd\u635f\u8017\u3002\u597d\u5728\u5217\u8868\u9879\u7684\u6eda\u52a8\u8ddd\u79bb\u662f\u5b8c\u5168\u5347\u5e8f\u6392\u5217\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u5bf9\u5df2\u7ecf\u7f13\u5b58\u7684\u6570\u636e\u505a\u4e8c\u5206\u67e5\u627e\uff0c\u628a\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u5230 O(lgN) \u3002&lt;\/p&gt;&lt;p&gt;js\u4ee3\u7801\u5982\u4e0b\uff1a&lt;\/p&gt;&lt;pre class=\"brush:php;toolbar:false;\"&gt;&nbsp;&nbsp;findNearestItem(offset:&nbsp;number)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;offset&nbsp;=&nbsp;Math.max(0,&nbsp;offset);&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredSizeAndPosition&nbsp;=&nbsp;this.getSizeAndPositionOfLastMeasuredItem();&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredIndex&nbsp;=&nbsp;Math.max(0,&nbsp;this.lastMeasuredIndex);&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(lastMeasuredSizeAndPosition.offset&nbsp;&amp;gt;=&nbsp;offset)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.binarySearch({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;high:&nbsp;lastMeasuredIndex,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;low:&nbsp;0,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.exponentialSearch({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index:&nbsp;lastMeasuredIndex,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}\n\n&nbsp;private&nbsp;binarySearch({\n&nbsp;&nbsp;&nbsp;&nbsp;low,\n&nbsp;&nbsp;&nbsp;&nbsp;high,\n&nbsp;&nbsp;&nbsp;&nbsp;offset,\n&nbsp;&nbsp;}:&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;low:&nbsp;number;\n&nbsp;&nbsp;&nbsp;&nbsp;high:&nbsp;number;\n&nbsp;&nbsp;&nbsp;&nbsp;offset:&nbsp;number;\n&nbsp;&nbsp;})&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;middle&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;currentOffset&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(low&nbsp;&nbsp;offset)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;high&nbsp;=&nbsp;middle&nbsp;-&nbsp;1;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(low&nbsp;&amp;gt;&nbsp;0)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;low&nbsp;-&nbsp;1;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;\n&nbsp;&nbsp;}\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5bf9\u4e8e\u641c\u7d22\u6ca1\u6709\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\u7684\u67e5\u627e\uff0c\u5148\u4f7f\u7528\u6307\u6570\u67e5\u627e\u7f29\u5c0f\u67e5\u627e\u8303\u56f4\uff0c\u518d\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u3002<\/p>\n<pre>private&nbsp;exponentialSearch({\n&nbsp;&nbsp;&nbsp;&nbsp;index,\n&nbsp;&nbsp;&nbsp;&nbsp;offset,\n&nbsp;&nbsp;}:&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;index:&nbsp;number;\n&nbsp;&nbsp;&nbsp;&nbsp;offset:&nbsp;number;\n&nbsp;&nbsp;})&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;interval&nbsp;=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index&nbsp;&lt;h3 data-id=\"heading-4\"&gt;3.\u8ba1\u7b97startIndex\u3001endIndex&lt;\/h3&gt;&lt;p&gt;\u6211\u4eec\u77e5\u9053\u4e86\u300c\u53ef\u89c6\u533a\u57df\u300d\u5c3a\u5bf8containerSize\uff0c\u6eda\u52a8\u504f\u79fb\u91cfoffset\uff0c\u5728\u52a0\u4e0a\u9884\u6e32\u67d3\u7684\u6761\u6570overscanCount\u8fdb\u884c\u8c03\u6574\uff0c\u5c31\u53ef\u4ee5\u8ba1\u7b97\u51fa\u300c\u53ef\u89c6\u533a\u57df\u300d\u8d77\u59cb\u9879\u7684\u7d22\u5f15\u503cstartIndex\u548c\u7ed3\u675f\u9879\u7d22\u5f15\u503cendIndex\uff0c\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a&lt;\/p&gt;&lt;p&gt;1.\u627e\u5230\u8ddd\u79bboffset\u6700\u8fd1\u7684\u7d22\u5f15\u503c\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f\u8d77\u59cb\u9879\u7684\u7d22\u5f15\u503cstartIndex\uff1b&lt;\/p&gt;&lt;p&gt;2.\u901a\u8fc7startIndex\u83b7\u53d6\u6b64\u9879\u7684offset\u548csize\uff0c\u518d\u5bf9offset\u8fdb\u884c\u8c03\u6574\uff1b&lt;\/p&gt;&lt;p&gt;3.offset\u52a0\u4e0acontainerSize\u5f97\u5230\u7ed3\u675f\u9879\u7684maxOffset\uff0c\u4ecestartIndex\u5f00\u59cb\u7d2f\u52a0\uff0c\u76f4\u5230\u8d8a\u8fc7maxOffset\uff0c\u5f97\u5230\u7ed3\u675f\u9879\u7d22\u5f15\u503cendIndex\u3002&lt;\/p&gt;&lt;p&gt;js\u4ee3\u7801\u5982\u4e0b\uff1a&lt;\/p&gt;&lt;pre class=\"brush:php;toolbar:false;\"&gt;&nbsp;getVisibleRange({\n&nbsp;&nbsp;&nbsp;&nbsp;containerSize,\n&nbsp;&nbsp;&nbsp;&nbsp;offset,\n&nbsp;&nbsp;&nbsp;&nbsp;overscanCount,\n&nbsp;&nbsp;}:&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;containerSize:&nbsp;number;\n&nbsp;&nbsp;&nbsp;&nbsp;offset:&nbsp;number;\n&nbsp;&nbsp;&nbsp;&nbsp;overscanCount:&nbsp;number;\n&nbsp;&nbsp;}):&nbsp;{&nbsp;start?:&nbsp;number;&nbsp;stop?:&nbsp;number&nbsp;}&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;maxOffset&nbsp;=&nbsp;offset&nbsp;+&nbsp;containerSize;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;start&nbsp;=&nbsp;this.findNearestItem(offset);&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;datum&nbsp;=&nbsp;this.getSizeAndPositionForIndex(start);\n&nbsp;&nbsp;&nbsp;&nbsp;offset&nbsp;=&nbsp;datum.offset&nbsp;+&nbsp;datum.size;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;stop&nbsp;=&nbsp;start;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(offset&nbsp;&lt;h3 data-id=\"heading-5\"&gt;3.\u76d1\u542cscroll\u4e8b\u4ef6\uff0c\u5b9e\u73b0\u865a\u62df\u5217\u8868\u6eda\u52a8&lt;\/h3&gt;&lt;p&gt;\u73b0\u5728\u53ef\u4ee5\u901a\u8fc7\u76d1\u542cscroll\u4e8b\u4ef6\uff0c\u52a8\u6001\u66f4\u65b0startIndex\u3001endIndex\u3001totalSize\u3001offset\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u865a\u62df\u5217\u8868\u6eda\u52a8\u3002&lt;\/p&gt;&lt;p&gt;js\u4ee3\u7801\u5982\u4e0b\uff1a&lt;\/p&gt;&lt;pre class=\"brush:php;toolbar:false;\"&gt;&nbsp;&nbsp;getItemStyle(index)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;style&nbsp;=&nbsp;this.styleCache[index];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(style)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;style;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;{&nbsp;scrollDirection&nbsp;}&nbsp;=&nbsp;this.data;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;=&nbsp;this.sizeAndPositionManager.getSizeAndPositionForIndex(index);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;cumputedStyle&nbsp;=&nbsp;styleToCssString({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;position:&nbsp;'absolute',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;top:&nbsp;0,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left:&nbsp;0,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width:&nbsp;'100%',\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[positionProp[scrollDirection]]:&nbsp;offset,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[sizeProp[scrollDirection]]:&nbsp;size,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.styleCache[index]&nbsp;=&nbsp;cumputedStyle;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;cumputedStyle;\n&nbsp;&nbsp;},\n&nbsp;&nbsp;\n&nbsp;&nbsp;observeScroll(offset:&nbsp;number)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;{&nbsp;scrollDirection,&nbsp;overscanCount,&nbsp;visibleRange&nbsp;}&nbsp;=&nbsp;this.data;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;{&nbsp;start,&nbsp;stop&nbsp;}&nbsp;=&nbsp;this.sizeAndPositionManager.getVisibleRange({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;containerSize:&nbsp;this.data[sizeProp[scrollDirection]]&nbsp;||&nbsp;0,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overscanCount,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;totalSize&nbsp;=&nbsp;this.sizeAndPositionManager.getTotalSize();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(totalSize&nbsp;!==&nbsp;this.data.totalSize)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setData({&nbsp;totalSize&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(visibleRange.start&nbsp;!==&nbsp;start&nbsp;||&nbsp;visibleRange.stop&nbsp;!==&nbsp;stop)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;styleItems:&nbsp;string[]&nbsp;=&nbsp;[];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isNumber(start)&nbsp;&amp;amp;&amp;amp;&nbsp;isNumber(stop))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;index&nbsp;=&nbsp;start&nbsp;-&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(++index&nbsp;&lt;p&gt;\u5728\u8c03\u7528\u7684\u65f6\u5019\uff0c\u901a\u8fc7render\u4e8b\u4ef6\u56de\u8c03\u51fa\u6765\u7684startIndex, stopIndex\uff0cstyleItems\uff0c\u622a\u53d6\u957f\u5217\u8868\u300c\u53ef\u89c6\u533a\u57df\u300d\u7684\u6570\u636e\uff0c\u5728\u628a\u5217\u8868\u9879\u76ee\u7684itemSize\u548coffset\u901a\u8fc7\u7edd\u5bf9\u5b9a\u4f4d\u7684\u65b9\u5f0f\u5e94\u7528\u5728\u5217\u8868\u4e0a&lt;\/p&gt;&lt;p&gt;\u4ee3\u7801\u5982\u4e0b\uff1a&lt;\/p&gt;&lt;pre class=\"brush:php;toolbar:false;\"&gt;let&nbsp;list&nbsp;=&nbsp;Array.from({&nbsp;length:&nbsp;10000&nbsp;}).map((_,&nbsp;index)&nbsp;=&amp;gt;&nbsp;index);\n\nPage({&nbsp;&nbsp;data:&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;itemSize:&nbsp;index&nbsp;=&amp;gt;&nbsp;50&nbsp;*&nbsp;((index&nbsp;%&nbsp;3)&nbsp;+&nbsp;1),&nbsp;&nbsp;&nbsp;&nbsp;styleItems:&nbsp;null,&nbsp;&nbsp;&nbsp;&nbsp;itemCount:&nbsp;list.length,&nbsp;&nbsp;&nbsp;&nbsp;list:&nbsp;[],\n&nbsp;&nbsp;},\n&nbsp;&nbsp;onReady()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;this.virtualListRef&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.virtualListRef&nbsp;||&nbsp;this.selectComponent('#virtual-list');\n&nbsp;&nbsp;},\n\n&nbsp;&nbsp;slice(e)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;{&nbsp;startIndex,&nbsp;stopIndex,&nbsp;styleItems&nbsp;}&nbsp;=&nbsp;e.detail;&nbsp;&nbsp;&nbsp;&nbsp;this.setData({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list:&nbsp;list.slice(startIndex,&nbsp;stopIndex&nbsp;+&nbsp;1),\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;styleItems,\n&nbsp;&nbsp;&nbsp;&nbsp;});\n&nbsp;&nbsp;},\n\n&nbsp;&nbsp;loadMore()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;setTimeout(()&nbsp;=&amp;gt;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;appendList&nbsp;=&nbsp;Array.from({&nbsp;length:&nbsp;10&nbsp;}).map(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(_,&nbsp;index)&nbsp;=&amp;gt;&nbsp;list.length&nbsp;+&nbsp;index,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list&nbsp;=&nbsp;list.concat(appendList);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.setData({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemCount:&nbsp;list.length,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list:&nbsp;this.data.list.concat(appendList),\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;500);\n&nbsp;&nbsp;},\n});\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>&lt;view&gt;\n&nbsp;&nbsp;&lt;virtual-list&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;view&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;view&gt;{{&nbsp;item&nbsp;+&nbsp;1&nbsp;}}&lt;\/view&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/view&gt;\n&nbsp;&nbsp;&lt;\/virtual-list&gt;\n&nbsp;&nbsp;{{itemCount}}&lt;\/view&gt;\u590d\u5236\u4ee3\u7801<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236<br \/>\n<img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/60be67a2898dbc138b74316ca45efcae-1.png\" style=\"max-width:90%\" title=\"\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3\u63d2\u56fe2\" alt=\"\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3\u63d2\u56fe2\" \/> <\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<p>\u5728\u5199\u8fd9\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684virtual-list\u7ec4\u4ef6\u8fc7\u7a0b\u4e2d\uff0c\u4e3b\u8981\u53c2\u8003\u4e86\u4e00\u4e9b\u4f18\u79c0\u7684\u5f00\u6e90\u865a\u62df\u5217\u8868\u5b9e\u73b0\u65b9\u6848\uff1a<\/p>\n<ul>\n<li>react-tiny-virtual-list<\/li>\n<li>react-virtualized<\/li>\n<li>react-window<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u89e3\u91ca\u5df2\u7ecf\u521d\u6b65\u5b9e\u73b0\u4e86\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u73af\u5883\u4e2d\u5b9e\u73b0\u4e86\u865a\u62df\u5217\u8868\uff0c\u5e76\u4e14\u5bf9\u865a\u62df\u5217\u8868\u7684\u539f\u7406\u6709\u4e86\u66f4\u52a0\u6df1\u5165\u7684\u4e86\u89e3\u3002\u4f46\u662f\u5bf9\u4e8e\u7011\u5e03\u6d41\u5e03\u5c40\uff0c\u5217\u8868\u9879\u5c3a\u5bf8\u4e0d\u53ef\u9884\u6d4b\u7b49\u573a\u666f\u4f9d\u7136\u65e0\u6cd5\u9002\u7528\u3002\u5728\u5feb\u901f\u6eda\u52a8\u8fc7\u7a0b\u4e2d\uff0c\u4f9d\u7136\u4f1a\u51fa\u73b0\u6765\u4e0d\u53ca\u6e32\u67d3\u800c\u767d\u5c4f\uff0c\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u300c\u53ef\u89c6\u533a\u57df\u300d\u5916\u9884\u6e32\u67d3\u7684item\u6761\u6570overscanCount\u6765\u5f97\u5230\u4e00\u5b9a\u7684\u7f13\u89e3\u3002<\/p>\n<blockquote><p>\n  \u60f3\u4e86\u89e3\u66f4\u591a\u7f16\u7a0b\u5b66\u4e60\uff0c\u656c\u8bf7\u5173\u6ce8php\u57f9\u8bad\u680f\u76ee\uff01\n<\/p><\/blockquote>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u5b9e\u73b0virtual-list\u7684\u65b9\u6cd5\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a\u3011 \u80cc\u666f \u5c0f\u7a0b\u5e8f\u5728\u5f88\u591a\u573a\u666f\u4e0b\u9762\u4f1a\u9047\u5230\u957f\u5217\u8868\u7684\u4ea4\u4e92\uff0c\u5f53\u4e00\u4e2a\u9875\u9762\u6e32\u67d3\u8fc7\u591a\u7684wxml\u8282\u70b9\u7684\u65f6\u5019\uff0c\u4f1a\u9020\u6210\u5c0f\u7a0b\u5e8f\u9875\u9762\u7684\u5361\u987f\u548c\u767d\u5c4f\u3002\u539f\u56e0\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a 1.\u5217\u8868\u6570\u636e\u91cf\u5927\uff0c\u521d\u59cb\u5316setData\u548c\u521d\u59cb\u5316\u6e32\u67d3\u5217\u8868wxml\u8017\u65f6\u90fd\u6bd4\u8f83\u957f\uff1b 2.\u6e32\u67d3\u7684wxml\u8282\u70b9\u6bd4\u8f83\u591a\uff0c\u6bcf\u6b21setData\u66f4\u65b0\u89c6\u56fe\u90fd\u9700\u8981\u521b\u5efa\u65b0\u7684\u865a\u62df\u6811\uff0c\u548c\u65e7\u6811\u7684diff\u64cd\u4f5c\u8017\u65f6\u6bd4\u8f83\u9ad8\uff1b 3.\u6e32\u67d3\u7684wxml\u8282\u70b9\u6bd4\u8f83\u591a\uff0cpage\u80fd\u591f\u5bb9\u7eb3\u7684wxml\u662f\u6709\u9650\u7684\uff0c\u5360\u7528\u7684\u5185\u5b58\u9ad8\u3002 \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u672c\u8eab\u7684scroll-view\u6ca1\u6709\u9488\u5bf9\u957f\u5217\u8868\u505a\u4f18\u5316\uff0c\u5b98\u65b9\u7ec4\u4ef6recycle-view\u5c31\u662f\u4e00\u4e2a\u7c7b\u4f3cvirtual-list\u7684\u957f\u5217\u8868\u7ec4\u4ef6\u3002\u73b0\u5728\u6211\u4eec\u8981\u5256\u6790\u865a\u62df\u5217\u8868\u7684\u539f\u7406\uff0c\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\u7684virtual-list\u3002 \u5b9e\u73b0\u539f\u7406 \u9996\u5148\u6211\u4eec\u8981\u4e86\u89e3\u4ec0\u4e48\u662fvirtual-list\uff0c\u8fd9\u662f\u4e00\u79cd\u521d\u59cb\u5316\u53ea\u52a0\u8f7d\u300c\u53ef\u89c6\u533a\u57df\u300d\u53ca\u5176\u9644\u8fd1dom\u5143\u7d20\uff0c\u5e76\u4e14\u5728\u6eda\u52a8\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u590d\u7528dom\u5143\u7d20\u53ea\u6e32\u67d3\u300c\u53ef\u89c6\u533a\u57df\u300d\u53ca\u5176\u9644\u8fd1dom\u5143\u7d20\u7684\u6eda\u52a8\u5217\u8868\u524d\u7aef\u4f18\u5316\u6280\u672f\u3002\u76f8\u6bd4\u4f20\u7edf\u7684\u5217\u8868\u65b9\u5f0f\u53ef\u4ee5\u5230\u8fbe\u6781\u9ad8\u7684\u521d\u6b21\u6e32\u67d3\u6027\u80fd\uff0c\u5e76\u4e14\u5728\u6eda\u52a8\u8fc7\u7a0b\u4e2d\u53ea\u7ef4\u6301\u8d85\u8f7b\u91cf\u7684dom\u7ed3\u6784\u3002 \u865a\u62df\u5217\u8868\u6700\u91cd\u8981\u7684\u51e0\u4e2a\u6982\u5ff5\uff1a \u53ef\u6eda\u52a8\u533a\u57df\uff1a\u6bd4\u5982\u5217\u8868\u5bb9\u5668\u7684\u9ad8\u5ea6\u662f600\uff0c\u5185\u90e8\u5143\u7d20\u7684\u9ad8\u5ea6\u4e4b\u548c\u8d85\u8fc7\u4e86\u5bb9\u5668\u9ad8\u5ea6\uff0c\u8fd9\u4e00\u5757\u533a\u57df\u5c31\u53ef\u4ee5\u6eda\u52a8\uff0c\u5c31\u662f\u300c\u53ef\u6eda\u52a8\u533a\u57df\u300d\uff1b \u53ef\u89c6\u533a\u57df\uff1a\u6bd4\u5982\u5217\u8868\u5bb9\u5668\u7684\u9ad8\u5ea6\u662f600\uff0c\u53f3\u4fa7\u6709\u7eb5\u5411\u6eda\u52a8\u6761\u53ef\u4ee5\u6eda\u52a8\uff0c\u89c6\u89c9\u53ef\u89c1\u7684\u5185\u90e8\u533a\u57df\u5c31\u662f\u300c\u53ef\u89c6\u533a\u57df\u300d\u3002 \u5b9e\u73b0\u865a\u62df\u5217\u8868\u7684\u6838\u5fc3\u5c31\u662f\u76d1\u542cscroll\u4e8b\u4ef6\uff0c\u901a\u8fc7\u6eda\u52a8\u8ddd\u79bboffset\u548c\u6eda\u52a8\u7684\u5143\u7d20\u7684\u5c3a\u5bf8\u4e4b\u548ctotalSize\u52a8\u6001\u8c03\u6574\u300c\u53ef\u89c6\u533a\u57df\u300d\u6570\u636e\u6e32\u67d3\u7684\u9876\u90e8\u8ddd\u79bb\u548c\u524d\u540e\u622a\u53d6\u7d22\u5f15\u503c\uff0c\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a 1.\u76d1\u542cscroll\u4e8b\u4ef6\u7684scrollTop\/scrollLeft\uff0c\u8ba1\u7b97\u300c\u53ef\u89c6\u533a\u57df\u300d\u8d77\u59cb\u9879\u7684\u7d22\u5f15\u503cstartIndex\u548c\u7ed3\u675f\u9879\u7d22\u5f15\u503cendIndex\uff1b 2.\u901a\u8fc7startIndex\u548cendIndex\u622a\u53d6\u957f\u5217\u8868\u7684\u300c\u53ef\u89c6\u533a\u57df\u300d\u7684\u6570\u636e\u9879\uff0c\u66f4\u65b0\u5230\u5217\u8868\u4e2d\uff1b 3.\u8ba1\u7b97\u53ef\u6eda\u52a8\u533a\u57df\u7684\u9ad8\u5ea6\u548citem\u7684\u504f\u79fb\u91cf\uff0c\u5e76\u5e94\u7528\u5728\u53ef\u6eda\u52a8\u533a\u57df\u548citem\u4e0a\u3002 1.\u5217\u8868\u9879\u7684\u5bbd\/\u9ad8\u548c\u6eda\u52a8\u504f\u79fb\u91cf \u5728\u865a\u62df\u5217\u8868\u4e2d\uff0c\u4f9d\u8d56\u6bcf\u4e00\u4e2a\u5217\u8868\u9879\u7684\u5bbd\/\u9ad8\u6765\u8ba1\u7b97\u300c\u53ef\u6eda\u52a8\u533a\u57df\u300d\uff0c\u800c\u4e14\u53ef\u80fd\u662f\u9700\u8981\u81ea\u5b9a\u4e49\u7684\uff0c\u5b9a\u4e49itemSizeGetter\u51fd\u6570\u6765\u8ba1\u7b97\u5217\u8868\u9879\u5bbd\/\u9ad8\u3002 itemSizeGetter(itemSize)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(index:&nbsp;number)&nbsp;=&amp;gt;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isFunction(itemSize))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;itemSize(index); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;isArray(itemSize)&nbsp;?&nbsp;itemSize[index]&nbsp;:&nbsp;itemSize; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;}\u590d\u5236\u4ee3\u7801 \u767b\u5f55\u540e\u590d\u5236 \u6eda\u52a8\u8fc7\u7a0b\u4e2d\uff0c\u4e0d\u4f1a\u8ba1\u7b97\u6ca1\u6709\u51fa\u73b0\u8fc7\u7684\u5217\u8868\u9879\u7684itemSize\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f1a\u4f7f\u7528\u4e00\u4e2a\u9884\u4f30\u7684\u5217\u8868\u9879estimatedItemSize\uff0c\u76ee\u7684\u5c31\u662f\u5728\u8ba1\u7b97\u300c\u53ef\u6eda\u52a8\u533a\u57df\u300d\u9ad8\u5ea6\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u6d4b\u91cf\u8fc7\u7684itemSize\u7528estimatedItemSize\u4ee3\u66ff\u3002 getSizeAndPositionOfLastMeasuredItem()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.lastMeasuredIndex&nbsp;&amp;gt;=&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;this.itemSizeAndPositionData[this.lastMeasuredIndex] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;{&nbsp;offset:&nbsp;0,&nbsp;size:&nbsp;0&nbsp;}; &nbsp;&nbsp;} getTotalSize():&nbsp;number&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredSizeAndPosition&nbsp;=&nbsp;this.getSizeAndPositionOfLastMeasuredItem();&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastMeasuredSizeAndPosition.offset&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastMeasuredSizeAndPosition.size&nbsp;+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(this.itemCount&nbsp;&#8211;&nbsp;this.lastMeasuredIndex&nbsp;&#8211;&nbsp;1)&nbsp;*&nbsp;this.estimatedItemSize &nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;}\u590d\u5236\u4ee3\u7801 \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u770b\u5230\u4e86\u662f\u76f4\u63a5\u901a\u8fc7\u7f13\u5b58\u547d\u4e2d\u6700\u8fd1\u4e00\u4e2a\u8ba1\u7b97\u8fc7\u7684\u5217\u8868\u9879\u7684itemSize\u548coffset\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u83b7\u53d6\u6bcf\u4e00\u4e2a\u5217\u8868\u9879\u7684\u4e24\u4e2a\u53c2\u6570\u65f6\u5019\uff0c\u90fd\u5bf9\u5176\u505a\u4e86\u7f13\u5b58\u3002 &nbsp;getSizeAndPositionForIndex(index:&nbsp;number)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(index&nbsp;&amp;gt;&nbsp;this.lastMeasuredIndex)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredSizeAndPosition&nbsp;=&nbsp;this.getSizeAndPositionOfLastMeasuredItem();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;offset&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastMeasuredSizeAndPosition.offset&nbsp;+&nbsp;lastMeasuredSizeAndPosition.size;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(let&nbsp;i&nbsp;=&nbsp;this.lastMeasuredIndex&nbsp;+&nbsp;1;&nbsp;i&nbsp;&lt;h3 data-id=&#8221;heading-3&#8243;&gt;2.\u6839\u636e\u504f\u79fb\u91cf\u641c\u7d22\u7d22\u5f15\u503c&lt;\/h3&gt;&lt;p&gt;\u5728\u6eda\u52a8\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u901a\u8fc7\u6eda\u52a8\u504f\u79fb\u91cfoffset\u8ba1\u7b97\u51fa\u5c55\u793a\u5728\u300c\u53ef\u89c6\u533a\u57df\u300d\u9996\u9879\u6570\u636e\u7684\u7d22\u5f15\u503c\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4ece0\u5f00\u59cb\u8ba1\u7b97\u6bcf\u4e00\u5217\u8868\u9879\u7684itemSize\uff0c\u7d2f\u52a0\u5230\u4e00\u65e6\u8d85\u8fc7offset\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e2a\u7d22\u5f15\u503c\u3002\u4f46\u662f\u5728\u6570\u636e\u91cf\u592a\u5927\u548c\u9891\u7e41\u89e6\u53d1\u7684\u6eda\u52a8\u4e8b\u4ef6\u4e2d\uff0c\u4f1a\u6709\u8f83\u5927\u7684\u6027\u80fd\u635f\u8017\u3002\u597d\u5728\u5217\u8868\u9879\u7684\u6eda\u52a8\u8ddd\u79bb\u662f\u5b8c\u5168\u5347\u5e8f\u6392\u5217\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u5bf9\u5df2\u7ecf\u7f13\u5b58\u7684\u6570\u636e\u505a\u4e8c\u5206\u67e5\u627e\uff0c\u628a\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u5230 O(lgN) \u3002&lt;\/p&gt;&lt;p&gt;js\u4ee3\u7801\u5982\u4e0b\uff1a&lt;\/p&gt;&lt;pre class=&#8221;brush:php;toolbar:false;&#8221;&gt;&nbsp;&nbsp;findNearestItem(offset:&nbsp;number)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;offset&nbsp;=&nbsp;Math.max(0,&nbsp;offset);&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredSizeAndPosition&nbsp;=&nbsp;this.getSizeAndPositionOfLastMeasuredItem();&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;lastMeasuredIndex&nbsp;=&nbsp;Math.max(0,&nbsp;this.lastMeasuredIndex);&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(lastMeasuredSizeAndPosition.offset&nbsp;&amp;gt;=&nbsp;offset)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.binarySearch({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;high:&nbsp;lastMeasuredIndex,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;low:&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;this.exponentialSearch({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index:&nbsp;lastMeasuredIndex, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;private&nbsp;binarySearch({ &nbsp;&nbsp;&nbsp;&nbsp;low, &nbsp;&nbsp;&nbsp;&nbsp;high, &nbsp;&nbsp;&nbsp;&nbsp;offset, &nbsp;&nbsp;}:&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;low:&nbsp;number; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-33610","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=33610"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33610\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}