What is the simplest way to lazy load a long list ( ) of many (65+) list items?
By lazy load I mean only load the list items that are visible in the viewport. Once a list item is visible in the viewport then load it.
I've looked at many infinite scrolling and lazy load plugins, but none of them seem to be just for a really long list.
They all seems to be for a really long list of images, or a list that has pagination with previous and next buttons.
My HTML is just a list of text with many list items like so:
<ul>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<ul>
I would greatly appreciate any and all help in writing the javascript or jquery so that only those that are currently in the viewport are loaded and then rest are hidden and then when teh user scrolls and others e into view in the viewport, they are loaded.
And, if possible, display a <div>loading...</div>
while those are loading.
The part that I really do not know how to do is to load or show only what is visible in the viewport in the beginning and then one could load, with jQuery's .load(), or show, with jQuery's .show() those that are in the viewport. But I don't know how to tell what is in the viewport and what is not.
I would greatly appreciate any and all help with this!
Thanks in Advance!
What is the simplest way to lazy load a long list ( ) of many (65+) list items?
By lazy load I mean only load the list items that are visible in the viewport. Once a list item is visible in the viewport then load it.
I've looked at many infinite scrolling and lazy load plugins, but none of them seem to be just for a really long list.
They all seems to be for a really long list of images, or a list that has pagination with previous and next buttons.
My HTML is just a list of text with many list items like so:
<ul>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<li>list item</li>
<ul>
I would greatly appreciate any and all help in writing the javascript or jquery so that only those that are currently in the viewport are loaded and then rest are hidden and then when teh user scrolls and others e into view in the viewport, they are loaded.
And, if possible, display a <div>loading...</div>
while those are loading.
The part that I really do not know how to do is to load or show only what is visible in the viewport in the beginning and then one could load, with jQuery's .load(), or show, with jQuery's .show() those that are in the viewport. But I don't know how to tell what is in the viewport and what is not.
I would greatly appreciate any and all help with this!
Thanks in Advance!
Share Improve this question edited Feb 4, 2020 at 15:03 Stephan 43.1k66 gold badges244 silver badges340 bronze badges asked May 29, 2013 at 0:18 irfan mirirfan mir 4032 gold badges5 silver badges10 bronze badges 2- Are you trying to do this to save page loading time or just for the effect? – James Commented May 29, 2013 at 0:29
- @james just for effect. But, I would like for it to not weigh down the page as much as possible. – irfan mir Commented May 29, 2013 at 0:32
2 Answers
Reset to default 11As you've said this is just for effect here is a nice little solution I found a while ago.
Let's assume your markup is like this
<ul id="yourlist">
<li>list item</li> ... etc
1) Slice the first twenty elements and hide the rest.
$("#yourlist li").slice(20).hide();
2) We set two variables. Min and Max (the difference being 20 at all times). This will load the next 20 in the list. It then checks for window scroll and if the window height es within 400 px of the bottom of the page (or div if you change the code) then it will run the next function which is to slice the min to max and fadeIn those items. Then it counts on so that it's ready to run the next time.
var mincount = 20;
var maxcount = 40;
$(window).scroll(function()
{
if($(window).scrollTop() + $(window).height() >= $(document).height() - 400) {
$("#yourlist li").slice(mincount,maxcount).fadeIn(1200);
mincount = mincount+20;
maxcount = maxcount+20;
}
});
Here is a JS FIDDLE OF IT WORKING http://jsfiddle/ymyx8/1/ with some background colours on the li's so you can see it loading
Edit: to add loading DIV
As per your question in the ments: The issue is that nothing is actually 'loading'. Usually when you see this occurring it's because an AJAX call is made and the loading is shown and hidden on the success function. The effect can be mimicked using a delay like so:
JS FIDDLE http://jsfiddle/ymyx8/3/
You could also to it on callback functions on the fadeOut etc.
There is a bug with the example above on some browsers/systems (looking at you Apple) in which the code does not work if you don't have enough li
items to trigger the page to scroll (eg. 7). Since there's no scrollbar the code does nothing.
Here's a possibly hacky fix...
http://jsfiddle/7meuchoy/6/