[Do it yourself] Virtual Scroll

2022-03-22  本文已影响0人  小丸子啦啦啦呀

What is Virtual Scroll ?

Have your ever seen a dropdown that contains 100000+ options? I have. It turned out that the overlay show the options was stuck when i scroll in the overlay, Obviously, it's undurable.
So, how to handle this problem? Virtual Scroll may help.
Virtual Scroll is a tech that let browser always render specific number of items rather than all the 100000+ items. In detail, it figures out what content should be rendered and figure out the vretical offset position of it, so it can make you feel like all the items are rendered.

Virtual Scroll , Lazy Loading and Infinate Scroll

When i try to implement a demo based on Virtual Scroll at the begining, I was totally messed up with Lazy Loading and Infinate Scroll Although I did not realized tha time. These 3 concepts confused me a lot. so, What's the diffrence between them?

Build my own Virtual Scroll

In this Part, I will implement 2 demo based on adam klein's post.

Basic Virtual Scroll Demo

we assumed that the height of each row is fixed.
<p class="codepen" data-height="300" data-default-tab="html,result" data-slug-hash="bGaNpJg" data-user="liuqiao" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
<span>See the Pen <a href="https://codepen.io/liuqiao/pen/bGaNpJg">
Basic Virtual Scroll</a> by liuqiao (<a href="https://codepen.io/liuqiao">@liuqiao</a>)
on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>

Advanced Virtual Scroll Demo

If the height of each row is dynamic, what should we do?

<p class="codepen" data-height="300" data-default-tab="html,result" data-slug-hash="QWaKQJx" data-user="liuqiao" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
<span>See the Pen <a href="https://codepen.io/liuqiao/pen/QWaKQJx">
Untitled</a> by liuqiao (<a href="https://codepen.io/liuqiao">@liuqiao</a>)
on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>

References

  1. build-your-own-virtual-scroll
上一篇 下一篇

猜你喜欢

热点阅读