Below I will share with you an example of pull-up loading written in native ajax. It has a good reference value and I hope it will be helpful to everyone.
The idea of pull-up loading
1 Pull-up loading is to trigger the ajax event to request data when the screen is pulled to the bottom
2. All the codes below that need to obtain the height of the screen, document height and scrolling height are already compatible and can be used directly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Javascript:
alert(document.body.clientWidth);
alert(document.body.clientHeight);
alert(document.body.offsetWidth);
alert(document.body.offsetHeight);
alert(document.body.scrollWidth);
alert(document.body.scrollHeight);
alert(document.body.scrollTop);
alert(document.body.scrollLeft);
alert(window.screenTop);
alert(window.screenLeft);
alert(window.screen.height);
alert(window.screen.width);
alert(window.screen.availHeight);
alert(window.screen.availWidth);
Jquery
alert($(window).height());
alert($(document).height());
alert($(document.body).height());
alert($(document.body).outerHeight(true));
alert($(window).width());
alert($(document).width());
alert($(document.body).width());
alert($(document.body).outerWidth(true));
|
Copy after login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function getScrollTop() {
var scrollTop = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
} else if (document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
}
function getClientHeight() {
var clientHeight = 0;
if (document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
} else {
clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
return clientHeight;
}
function getScrollHeight() {
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}
|
Copy after login
1 2 3 4 5 6 7 8 9 10 11 | var upDown = function (opt) {
opt = opt || {};
var up = opt.up || function () {
};
window.onscroll = function () {
if (getScrollTop() + getClientHeight() == getScrollHeight()) {
if (is_scroll === true){
up();
}}
}
};
|
Copy after login
3. First, load the first page by default and call the upDown method in window.onload
1 2 3 4 5 6 | window.onload = function () {
getData();
upDown({
up: getData
});
}
|
Copy after login
4. When the page scrolls to the bottom, the up() method is triggered, and up calls the getdata method. Here is how to get the data
Define two variables globally var is_scroll = true;var count = 0;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | var is_scroll = true;
var count = 0;
function getAjax() {
var el, li;
var xhr = new XMLHttpRequest();
xhr.open('get', 'page' + count + '.json');
xhr.send();
xhr.onreadystatechange = function () {
var loadingEnd = document.getElementById('loadingEnd');
var dropLoad = document.getElementById('dropLoad');
if (xhr.readyState === 4 && xhr.status === 200) {
var res = xhr.responseText;
var data = JSON.parse(res);
allData = allData.concat(data);
if (data.length === 0) {
is_scroll = true
loadingEnd.style.display = 'block';
}
el = document.querySelector( "#wrapper ul" );
for ( var k in data) {
li = document.createElement('li');
li.innerHTML = "<p class='item-top'><span class='item-title'>" + data[k].name + "</span><span class='item-money'>" + data[k].money + "</span></p><p class='item-time'>" + data[k].time + "</p><p class='bottom-line'></p>" ;
el.appendChild(li, el.childNodes[0]);
}
dropLoad.style.display = 'block';
} else {
setTimeout( function () {
dropLoad.style.display = 'none';
}, 500)
}
};
}
|
Copy after login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <style>
.drop-load {
text-align: center;
height: 80px;
line-height: 50px;
}
.drop-load .loading {
display: inline-block;
height: 15px;
width: 15px;
border-radius: 100%;
margin: 6px;
border: 2px solid #666;
border-bottom-color: transparent;
vertical-align: middle;
-webkit-animation: rotate 0.75s linear infinite;
animation: rotate 0.75s linear infinite;
}
@-webkit-keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
}
50% {
-webkit-transform: rotate(180deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes rotate {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(360deg);
}
}
.loadingEnd {
font-size: 0.3rem;
color: black;
width: 100%;
height: 40px;
text-align: center;
}
</style>
|
Copy after login
1 2 3 4 5 6 7 8 9 10 11 | <body>
<p>
<ul>
</ul>
</p>
<p id= "dropLoad" class = "drop-load" style= "display: none" >
<span class = "loading" ></span>
<span>加载中</span>
</p>
<p id= "loadingEnd" class = "loadingEnd" style= "display: none" >到底了</p>
</body>
|
Copy after login
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
jquery ajaxHow to implement paging function
Use JQuery to operate Ajax (with case)
jquery ajaxImplementing asynchronous submission of form data
The above is the detailed content of Pull-up loading example written in native ajax (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!