Introduction
In Facebook you might saw status updates of your friends are dynamically loaded when you scroll the browser’s scroll bar. So in this article i am going to explain how we can achieve this using jQuery and Ajax.
Using the code
The solution includes 2 web forms(Default.aspx
and AjaxProcess.aspx
). The Default.aspx
contain a Div with ID “myDiv
“. Initially the Div contain some static data, then data is dynamically appended to the Div using jquery and ajax.
2 | <p>Static data initially rendered.</p> |
The second Web Form AjaxProcess.aspx
contains a web method GetData()
, that is called using ajax to retrieve data.
2 | public static string GetData() |
4 | string resp = string.Empty; |
5 | resp += "<p>This content is dynamically appended to the existing content on scrolling.</p>" ; |
Now we can add some jquery script in Default.aspx
, that will be fired on page scroll and invoke the GetData()
method.
01 | $(document).ready(function () { |
03 | $(window). scroll (function () { |
04 | if ($(window).scrollTop() == $(document).height() - $(window).height()) { |
13 | url: "AjaxProcess.aspx/GetData" , |
15 | contentType: "application/json; charset=utf-8" , |
20 | success: function (msg) { |
21 | $( "#myDiv" ).append(msg.d); |
24 | Error: function (x, e) { |
Here, to check whether the scroll has moved at the bottom, the following condition is used.
1 | $(window). scroll (function () { |
2 | if ($(window).scrollTop() == $(document).height() - $(window).height()) { |
This condition will identify whether the scroll has moved at the bottom or not. If it has moved at the bottom, dynamic data will get loaded from the server and get appended to myDiv
.
1 | success: function (msg) { |
2 | $( "#myDiv" ).append(msg.d); |
Download Code
source : http://vivekcek.wordpress.com/tag/jquery-scroll-data-load/
No comments:
Post a Comment