DevelopPHP.com
Web Development Training System

Scroll Load Dynamic Content When User Reach Bottom Ajax

Learn to make dynamic page content appear only when user scrolls down to the bottom of your page content using JavaScript. We are using the scroll event for the window object and several DOM properties to create the functionality. It serves as a smart way for dynamic websites to load only the data required to fill the window, then load more as the user scrolls down the page. You can see this effect if you have a Facebook account and you scroll down your timeline. Facebook executes new Ajax requests each time you scroll to the bottom of the page content, to load in more data to the page magically with Ajax. This way they do not have to load a lot of data to the page initially, and only spend dynamic data loading energy if the user scrolls down the page to see more and more data. Other large scale dynamic websites also use this approach to achieve efficient database data rendering in a smart modern way. <!DOCTYPE html> <html> <head> <script> function yHandler(){ // Watch video for line by line explanation of the code // http://www.youtube.com/watch?v=eziREnZPml4 var wrap = document.getElementById('wrap'); var contentHeight = wrap.offsetHeight; var yOffset = window.pageYOffset; var y = yOffset + window.innerHeight; if(y >= contentHeight){ // Ajax call to get more dynamic data goes here wrap.innerHTML += '<div class="newData"></div>'; } var status = document.getElementById('status'); status.innerHTML = contentHeight+" | "+y; } window.onscroll = yHandler; </script> <style> div#status{position:fixed; font-size:24px;} div#wrap{width:800px; margin:0px auto;} div.newData{height:1000px; background:#09F; margin:10px 0px;} </style> </head> <body> <div id="status">0 | 0</div> <div id="wrap"><img src="temp9.jpg"></div> </body> </html>
ScrollLoadDynamicContentWhenUserReachBottomAjax
Table of Contents Terms Of Use Subscribe Top ↑