简体   繁体   中英

Fire event when scrolled to element on mobile

I have this code that detects when the user scrolls past the bottom of an element:

var scrolltriggered = false;

$(document).on('scroll', function() {
   if(scrolltriggered === false && 
      $(window).scrollTop() >= $('.elementclass').offset().top +
      $('.elementclass').outerHeight() - window.innerHeight ){
      scrolltriggered = true;
      /* Actions */
   }
});

I have added a fiddle to demonstrate it in action: https://jsfiddle.net/e54cmrvg/

This works for mouse scrolling, but not for touch scrolling on mobile devices.

Any suggestions as to how I can enable the function for mobile devices as well?

The easiest hack-around is to use touchend like this:

$(document).on('scroll touchend', function(){});

Meaning that when the user finishes touching the screen, you will perform the same operation which checks offsetTop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM