Home >Web Front-end >JS Tutorial >Ajax waterfall flow realizes demo sharing (with code)
This time I will bring you ajax waterfall flow to realize demo sharing (with code). What are the precautions for ajax waterfall flow to realize demo sharing. The following is a practical case, let's take a look.
Recently I heard from friends that there are a lot of waterfalls, so I went to study it myself. I made a simple native demo to share with everyone...Simple It is divided into three documents with detailed comments: img; ajax.php; demo.php
#img folder Input picture 1.jpg; 2.jpg; 3.jpg....
ajax.php page
<?php //模拟从数据库读取数据 $arr = array(); $op = opendir('./img'); //打开目录 //循环读取目录 while (($file = readdir($op)) !== false) { //过滤点和点点 if ($file == '.' || $file == '..') { continue; } $arr[] = $file; } closedir($op); //关闭目录 echo json_encode($arr);
demo.html page
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>瀑布流</title> <style> li{ list-style: none; float: left; margin:4px; } img{ border:4px solid black; } </style> </head> <body> <ul id="ul"> <!-- <li><img src="./img/1.jpg" height="300" alt=""></li> --> </ul> </body> <script> //找对象 var ul = document.getElementById('ul'); //拿数据 function getData() { var ajax = new XMLHttpRequest(); ajax.open('get', 'ajax.php', true); ajax.send(); ajax.onreadystatechange = function() { if (ajax.readyState == 4 && ajax.status == 200) { var res = ajax.responseText; //处理结果 var obj = JSON.parse(res); for (var k in obj) { // obj[k]; //创建节点 var li = document.createElement('li'); li.innerHTML = '<img src="./img/'+obj[k]+'" height="300" />'; ul.appendChild(li); } } } } getData(); var timer; //判断滚动条的高度,加载第二批文件 window.onscroll = function() { //获取三高 var zGao = document.documentElement.scrollHeight;//总高度 var lGao = document.documentElement.clientHeight;//浏览器可用高度 var gGao = document.body.scrollTop || document.documentElement.scrollTop;//滚出去的高度 // console.log(zGao, lGao, gGao); document.title = zGao + '_' + lGao + '_' + gGao; if (zGao - lGao - gGao < 500) { clearTimeout(timer); //用一次性定时器解决连续加载的问题 timer = setTimeout(function(){ getData(); }, 200) } } </script> </html>I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How to configure Google Chrome to support AJAX requests of the file protocol
How to use php to receive ajax submissions Data to the background
The above is the detailed content of Ajax waterfall flow realizes demo sharing (with code). For more information, please follow other related articles on the PHP Chinese website!