Home  >  Article  >  Backend Development  >  Native ajax waterfall flow demo example sharing

Native ajax waterfall flow demo example sharing

小云云
小云云Original
2017-12-26 11:13:491447browse

This article mainly brings you a native ajax waterfall flow demo sharing (a must-read article). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

It is simply divided into three documents, with detailed comments: img; ajax.php; demo.php

Put the picture 1.jpg; 2.jpg; 3 in the img folder. .jpg....

ajax.php page

<?php
  //模拟从数据库读取数据
  $arr = array();
  $op = opendir(&#39;./img&#39;);  //打开目录

  //循环读取目录
  while (($file = readdir($op)) !== false) {
    //过滤点和点点
    if ($file == &#39;.&#39; || $file == &#39;..&#39;) {
      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/&#39;+obj[k]+&#39;" 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>

Related recommendations:

JS implementation of waterfall flow layout example analysis

Use JavaScript to create waterfall flow effect

Recommend 5 good-looking jquery waterfall flow effect codes

The above is the detailed content of Native ajax waterfall flow demo example sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn