13

Setch.me loading normally on desktop but not trigerring on mobile unless if I click on photographers/makeup artists, I've added height=device-height after searching for a solution here but that didn't work.

$(window).scroll(function() { 
    if($(window).scrollTop() + $(window).height() >= $(document).height()) { 
        track_page++; 
        load_contents(track_page); 
    }
vsync
  • 118,978
  • 58
  • 307
  • 400
Kareem Kamal
  • 1,028
  • 1
  • 8
  • 21
  • Possible duplicate of [jQuery live scroll event on mobile (work around)](https://stackoverflow.com/questions/18753367/jquery-live-scroll-event-on-mobile-work-around) – Blue Jun 25 '17 at 08:30

6 Answers6

41

Try this:

$(document.body).on('touchmove', onScroll); // for mobile
$(window).on('scroll', onScroll); 

// callback
function onScroll(){ 
    if( $(window).scrollTop() + window.innerHeight >= document.body.scrollHeight ) { 
        track_page++; 
        load_contents(track_page); 
    }
}
Community
  • 1
  • 1
vsync
  • 118,978
  • 58
  • 307
  • 400
6

var addition_constant = 0;
$(document.body).on('touchmove', onScroll); // for mobile
$(window).on('scroll', onScroll);

function onScroll() {
  var addition = ($(window).scrollTop() + window.innerHeight);

  var scrollHeight = (document.body.scrollHeight - 1);
  if (addition > scrollHeight && addition_constant < addition) {

    addition_constant = addition;

    loadmorecontest();
  }
}
1

The "scrollTop" value must be rounded on mobile devices using "Math.ceil". (It's decimal based on the resolution of the screen), so:

if (el.scrollHeight <= Math.ceil(el.scrollTop) + el.offsetHeight) {
    // so something...
}
0

In addition to previous comments, $(window).scrollTop() seems not to work on mobile and should be replaced with document.body.scrollTop

user194324
  • 38
  • 1
  • 6
0

It seems bit irrelevant but since I found myself here and my reason that it not worked is the element that I was trying to scroll has;

overflow-y: hidden

When I remove that it worked perfectly. I just wrote that for if it is also someone's case.

Mutlu
  • 13
  • 3
-1

Hi you didn't close correctly your event, this should be like this:

$(window).scroll(function() { 
if($(window).scrollTop() + $(window).height() >= $(document).height()) { 
    track_page++; 
    load_contents(track_page); 
}});
Faithium
  • 111
  • 1
  • 2
  • 11