It's true that you will have performance cost when scrolling a large list. Even if you set display:none;
or visibility:hidden;
to every element that was not visible on the viewport, you will have some lag because every element still being recalculated when you scrolling the list.
To avoid the recalculation, every element should be moved to Virtual DOM so it will never being rendered and some property (like height/width) will default to 0
.
There are some library that can handle the virtual scroll like Clusterize, ngx-ui-scroll, hyperlist, etc.
But some of them doesn't support dynamic row height, some of them aren't MIT licensed, and some of them have a little feature for manipulating the content list. So I decided to build a MVW library that can handle dynamic row height like this example. And yes, it's able to use scrollTo(index)
for automatically scroll to the selected index.
The implementation is just like
<div sf-controller="large-list">
<ul class="sf-virtual-scroll">
<li sf-repeat-this="x in list">
{{x.id}}
</li>
</ul>
</div>
sf.model.for('large-list', function(self){
self.list = [];
for(var i = 0; i < 1000; i++){
self.list.push({id:i});
}
});
You could manipulate the displayed list with the default array function like push
, splice
, pop
.
Or try it here