Articles in this section
Category / Section

How to achieve virtual scrolling for contents using the ejScroller?

2 mins read

How to achieve virtual scrolling for contents using the ejScroller?

What is Virtual Scrolling?

The virtual scrolling is used to display the large amounts of records without paging. When scrolling with the vertical scrollbar, AJAX requests will be executed to load and refresh the existing records.

How to achieve this virtual scrolling?

We can achieve this virtual scrolling in ejScroller using our existing event “scroll” and method “refresh” of ejScroller.

“Scroll” event will be trigger at that time of scrolling and the refresh method used to refresh the vertical scrollbar handle based on height of the target area content.

When the scroll bar handle reaches its end points we can upload the data using AJAX request in scroll event. Also we need to call refresh methods to refresh the scroll bar based on current content (after new content loaded success fully).

Please refer the below code snippet

contentLoaded = false;

$("#scrollcontent").ejScroller({

scroll: function (e) {

if (obj) {

scrollerpositon=e.scrollTop;

if (!contentLoaded && (document.getElementsByClassName("sampleContent")[0].scrollHeight-e.scrollTop) <= obj.option("height")) {

$.ajax({

url: "../Content/samplecontent/mvccontent.html", success: function (result) {

newitem = $("

").addClass("newitem");

newitem.text(result);

newitem.appendTo(".sampleContent");

contentLoaded = true;

obj.refresh();

obj.option("scrollTop", scroll);

 

}

});

}

}

}

});
obj = $("#scrollcontent").data("ejScroller");


In this code, document.getElementsByClassName("sampleContent")[0].scrollHeight-e.scrollTop > obj.option(“height”) is used to find whether the vertical scroll bar handle reaches is end points in scroll bar. When the handle is reaches its end points, we have loaded the data and then appends this to target pane of scroll bar.

Like this whenever the scroll bar’s vertical handle reaches its end points another new content will be loaded and this process will be continued until all the contents will get loaded successfully.

SampleVirtual scrolling using scroller

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied