Advertisement
MizunoBrasil

Ajax Scroll (index.php)

Dec 30th, 2022 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.84 KB | None | 0 0
  1. index.php
  2.  
  3.  
  4. <!DOCTYPE html>
  5. <html>
  6.  <head>
  7.   <title>Postagens</title>
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  9.   <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />-->
  10.   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  11.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  12.  </head>
  13.  <body>
  14.   <div class="container">
  15.    <h1 align="center">Postagens</a></h1>
  16.    <hr>
  17.    
  18.    
  19.    <br />
  20.    <div id="load_data"></div>
  21.    <div id="load_data_message"></div>
  22.    <br />
  23.    <br />
  24.    <br />
  25.    <br />
  26.    <br />
  27.    <br />
  28.   </div>
  29.  </body>
  30. </html>
  31. <script>
  32.  
  33. $(document).ready(function(){
  34.  
  35.  var limit = 9;
  36.  var start = 0;
  37.  var action = 'inactive';
  38.  function load_country_data(limit, start)
  39.  {
  40.   $.ajax({
  41.    url:"fetch.php",
  42.    method:"POST",
  43.    data:{limit:limit, start:start},
  44.    cache:false,
  45.    success:function(data)
  46.    {
  47.     $('#load_data').append(data);
  48.     if(data == '')
  49.     {
  50.      $('#load_data_message').html("<button type='button' class='btn btn-info'>Não foram encontradas novas publicações</button>");
  51.      action = 'active';
  52.     }
  53.     else
  54.     {
  55.      $('#load_data_message').html("<button type='button' class='btn btn-warning'>Carregando mais publicações, aguarde....</button>");
  56.      action = "inactive";
  57.     }
  58.    }
  59.   });
  60.  }
  61.  
  62.  if(action == 'inactive')
  63.  {
  64.   action = 'active';
  65.   load_country_data(limit, start);
  66.  }
  67.  $(window).scroll(function(){
  68.   if($(window).scrollTop() + $(window).height() > $("#load_data").height() && action == 'inactive')
  69.  {
  70.   action = 'active';
  71.    start = start + limit;
  72.    setTimeout(function(){
  73.     load_country_data(limit, start);
  74.    }, 1000);
  75.   }
  76.  });
  77.  
  78. });
  79. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement