0

I have an element on which I trigger the function 'scrollIntoView()'. And now I have to detect and catch this event when it is triggered.

I trigger it like this:

$('.element').scrollIntoView();

So I tried to catch it with jquery with something like this:

$('.element').on('scroll', function() { // STUFF });
$('.element').on('scrollIntoView', function() { // STUFF });

But unfortunately it's not working.

Is this even possible? Which event does it trigger?

elbecita
  • 2,616
  • 19
  • 28
thmspl
  • 2,437
  • 3
  • 22
  • 48

1 Answers1

1
$.fn.inView = function(){
      var win = $(window);
      var obj = $(this);
      var scrollPosition = win.scrollTop();
      var visibleArea = win.scrollTop() + win.height() ;
      var objEndPos = (obj.offset().top + obj.outerHeight());
      return(visibleArea >= objEndPos && scrollPosition <= objEndPos ? true : false)
  };

this will work. But inside of a scroll you can do

if(element.inView()) {
  do this 
}
kenny
  • 438
  • 4
  • 14
  • 1
    Also, don't bind directly to a scroll event. Use a requestAnimationFrame() to improve performance. https://css-tricks.com/using-requestanimationframe/ – kenny Jan 15 '18 at 19:44