Advertisement
alexolliveira

Scrolling JS with bug

Dec 4th, 2022
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. windowOnScroll();
  2. function windowOnScroll() {
  3.   window.addEventListener("scroll", function(e){
  4.     const {scrollHeight,scrollTop,clientHeight} = document.documentElement
  5.     if (scrollTop + clientHeight > scrollHeight -5){
  6.       if(document.querySelectorAll(".post-item").length < document.getElementById("total_count").value) {
  7.         var lastId = document.querySelectorAll(".post-item");
  8.         var lastchild = lastId[lastId.length-1];
  9.         var id=lastchild.getAttribute("id");
  10.         getMoreData(id);
  11.       }
  12.     }
  13.   });
  14. }
  15. function getMoreData(id) {
  16.   window.removeEventListener("scroll",windowOnScroll())
  17.   document.querySelector('.ajax-loader').classList.remove("none");
  18.   var formdata=new FormData();
  19.   formdata.append("lastId",id);
  20.   fetch('getMoreData.php',{
  21.     method: 'POST',
  22.     body:formdata
  23.   }).then(function(response){
  24.     response.text().then(function(result){
  25.       setTimeout(function() {
  26.         document.querySelector('.ajax-loader').classList.add("none");
  27.         document.getElementById("post-list").innerHTML+=result;
  28.         windowOnScroll();
  29.       }, 1000);
  30.     })
  31.   }).catch(function(err){
  32.     console.error(err);
  33.   });
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement