Hi,
thank you for this post. Most of the time I am a backend developer, but have to work on frontend code from time to time. Which is why I really like these little "How to's"
It really took me a while to understand this line of code
First thing that I do not quiet understand is that the boolean result of
(scrollHeight-300)>=scrollPos// result is 0 or 1
is devided by scrollHeight, I wouldn't consider this clean code. The division itself does not even add anything to the logic. The result of the division is checked to be zero. The result will only be zero if the check of
(scrollHeight-300)>=scrollPos
returned fales. Which means we could just write:
if(((scrollHeight-300)>=scrollPos)==false)
I would even go further and change the operators, reuslting in:
For the love of whatever you believe in use Typescript. Why would anyone not use Typescript? :-)
This solution kind of works but it will get triggered several times and if you want to fetch stuff from API it will make many unnecessary requests. So how about this:
to make it even better, we can make it execute before hitting the bottom and again executing only once:
letpage=1;letcurrentscrollHeight:0;letlockScroll:false;$(window).on("scroll",()=>{constscrollHeight=$(document).height()asnumber;constscrollPos=Math.floor($(window).height()+$(window).scrollTop());constisBottom=scrollHeight-300<scrollPos;if(isBottom&¤tscrollHeight<scrollHeight){$.ajax({method:"POST",url:"/api/search",dataType:"json",contentType:"application/json; charset=utf-8",data:JSON.stringify({page:this.page})}asany).done((posts:Post[])=>{this.page=this.page+1;// do whatever});currentscrollHeight=scrollHeight;}});
Dude! i made an account here just to thank you men, i spend a whole day trying to make this script work with a custom post type in wordpress, i end up in this article again after close it (i didn't read the comments the first time) and i realize that the main problem for me was "if(((scrollHeight - 300) >= scrollPos) / scrollHeight == 0)".
The script ran like four times every time, i just use your code but with my ajax and bum! thanks a lot good man
Hi,
thank you for this post. Most of the time I am a backend developer, but have to work on frontend code from time to time. Which is why I really like these little "How to's"
It really took me a while to understand this line of code
First thing that I do not quiet understand is that the boolean result of
is devided by
scrollHeight, I wouldn't consider this clean code. The division itself does not even add anything to the logic. The result of the division is checked to be zero. The result will only be zero if the check ofreturned fales. Which means we could just write:
I would even go further and change the operators, reuslting in:
Am I missing something?
Few improvements here:
to make it even better, we can make it execute before hitting the bottom and again executing only once:
Check an example: fullscreen-modal.azurewebsites.net
Dude! i made an account here just to thank you men, i spend a whole day trying to make this script work with a custom post type in wordpress, i end up in this article again after close it (i didn't read the comments the first time) and i realize that the main problem for me was "if(((scrollHeight - 300) >= scrollPos) / scrollHeight == 0)".
The script ran like four times every time, i just use your code but with my ajax and bum! thanks a lot good man
Awesome! I'm glad it helped you!