javascript - scrollTop not working when I reach a certain div -
i trying implement on scroll load.
this code on-scroll
$(window).on("scroll", function(){ if( ($(window).scrolltop()-350) + $(window).height() == $(document).height()){ loaddata(); } }); i subtracting 350 scrolltop because want ajax request fire on div comes viewport.
why code not working.
here loaddata function has ajax request.
however, when run code, works:
$(window).on("scroll", function(){ if($(window).scrolltop() + $(window).height() == $(document).height()){ loaddata(); } }); but, have go end of page ajax request fire.
why code not working subtract 350 scrolltop??? need fire ajax when reach div, how do that??
there 2 separate issues here; 1 you're testing exact scroll position, other want adding 350px scroll position rather subtracting (so function fire 350px before end of window, rather 350px after end of it.)
try this:
if( $(window).scrolltop() + $(window).height() + 350 >= $(document).height()) { loaddata(); } but remember scroll events fire continuously , want loaddata() fire once! therefore:
var loadeddata = false; $(window).on("scroll", function() { if (loadeddata) return; if ($(window).scrolltop() + $(window).height() + 350 >= $(document).height()) { alert("fire loaddata now"); loadeddata = true; loaddata(); } }); ...and have loaddata() function set loadeddata false re-enable scroll trigger, if necessary.
Comments
Post a Comment