Use jQuery or native js to scroll and load new page data.
I believe many people have seen waterfall Stream Image la S. These images are dynamically loaded and the results are very good, and the pressure on servers is relatively low, I believe you have seen this effect when using the mouse: Enter the QQ space and pull down the space. When it reaches the bottom, the rest of the talk or log will be dynamically loaded, today, let's take a look at their implementation ideas and js Code for controlling dynamic loading.
The following code mainly controls the loading events when the scroll bar is pulled down. You can load images or record data.
After loading the jQuery library, we can use it like this:
$ (Window ). scroll (function () {var scrollTop = $ (this ). scrollTop (); var scrollHeight = $ (document ). height (); var returns wheight = $ (this ). height (); if (scrollTop + windowHeight = scrollHeight) {// This is the event triggered when the scroll bar reaches the bottom. Write the data to be loaded here, or pull the scroll bar/var page = Number ($ ("# redgiftNextPage "). attr ('currentpage') + 1; // redgiftList (page); // $ ("# redgiftNextPage "). attr ('currentpage', page + 1 );}});
Resolution:
To judge the scroll bar to the bottom, three DOM attribute values are required, namely scrollTop, clientHeight, and scrollHeight.
- ScrollTop indicates the scroll distance of the scroll bar on the Y axis.
- ClientHeight is the height of the visible area of the content.
- ScrollHeight is the height of the visible area of the content plus the overflow (scroll) distance.
From the introduction of these three attributes, we can see that the condition from the scroll bar to the bottom is scrollTop + clientHeight = scrollHeight.
We can do this for pure js:
window.onscroll = function() { var scrollTop = document.body.scrollTop; var offsetHeight = document.body.offsetHeight; var scrollHeight = document.body.scrollHeight; if (scrollTop == scrollHeight - offsetHeight) { page++; loadList(page); } }; function loadList(page) { page = page || 1; if (isLoad) { getJSON('/forum.php?mod=hot&page=' + page).then(function(data) { if (data.code == 200) { data = data.data; if (data && Object.keys(data).length > 0) { for (var k in data) { var v = data[k]; detailTemplate = detailTemplate.cloneNode(true); var userInfoObj = detailTemplate.getElementsByClassName('user-info')[0]; userInfoObj.getElementsByClassName('name')[0].innerText = v.author; userInfoObj.getElementsByClassName('avatar')[0].src = v.avatar; userInfoObj.getElementsByClassName('post-time')[0].innerHTML = v.lastpost; detailTemplate.getElementsByClassName('title')[0].innerText = v.subject; detailTemplate.getElementsByClassName('desc')[0].innerText = v.subject; var extInfoObj = detailTemplate.getElementsByClassName('ext-info')[0]; extInfoObj.getElementsByClassName('from')[0].innerText = v.fname; extInfoObj.getElementsByClassName('view-time')[0].innerText = v.views; postListObj.appendChild(detailTemplate); } } else { isLoad = false; } } else { isLoad = false; } }, function(status) { console.log('Something went wrong, status is ' + status); }); } }
Articles you may be interested in:
- Implementation principle of jQuery Scrolling Image Loading
- Jquery Method for Rolling Data Loading
- Implementation Scheme of jquery browser rolling loading technology
- Implementation of jQuery Scrolling Image loading effect