Pull-up loading example written in native ajax (graphic tutorial)
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
Javascript: alert(document.body.clientWidth); //网页可见区域宽(body) alert(document.body.clientHeight); //网页可见区域高(body) alert(document.body.offsetWidth); //网页可见区域宽(body),包括border、margin等 alert(document.body.offsetHeight); //网页可见区域宽(body),包括border、margin等 alert(document.body.scrollWidth); //网页正文全文宽,包括有滚动条时的未见区域 alert(document.body.scrollHeight); //网页正文全文高,包括有滚动条时的未见区域 alert(document.body.scrollTop); //网页被卷去的Top(滚动条) alert(document.body.scrollLeft); //网页被卷去的Left(滚动条) alert(window.screenTop); //浏览器距离Top alert(window.screenLeft); //浏览器距离Left 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()); //浏览器当前窗口文档body的高度 alert($(document.body).outerHeight(true)); //浏览器当前窗口文档body的总高度 包括border padding margin alert($(window).width()); //浏览器当前窗口可视区域宽度 alert($(document).width()); //浏览器当前窗口文档对象宽度 alert($(document.body).width()); //浏览器当前窗口文档body的宽度 alert($(document.body).outerWidth(true)); //浏览器当前窗口文档body的总宽度 包括border padding margin
//获取滚动条当前的位置 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); }
var upDown = function (opt) { opt = opt || {}; var up = opt.up || function () { }; window.onscroll = function () { if (getScrollTop() + getClientHeight() == getScrollHeight()) { //距离顶部+当前高度 >=文档总高度 即代表滑动到底部 if(is_scroll === true){ //当这个为true的时候调用up方法 ....is_scroll没看懂往下看 up(); }} } };
3. First, load the first page by default and call the upDown method in window.onload
window.onload = function () { getData();//页面加载完就显示了第一页 upDown({ up: getData }); }
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;
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) { //当获取到的数据长度为0 说明没有count+是请求不到数据了 is_scroll = true // 定义为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) } }; }
<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>
<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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

If you encounter freezing issues when inserting hyperlinks into Outlook, it may be due to unstable network connections, old Outlook versions, interference from antivirus software, or add-in conflicts. These factors may cause Outlook to fail to handle hyperlink operations properly. Fix Outlook freezes when inserting hyperlinks Use the following fixes to fix Outlook freezes when inserting hyperlinks: Check installed add-ins Update Outlook Temporarily disable your antivirus software and then try creating a new user profile Fix Office apps Program Uninstall and reinstall Office Let’s get started. 1] Check the installed add-ins. It may be that an add-in installed in Outlook is causing the problem.

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:
