0

I need to add the function when the scroll bar reached the particular div.

How to check the scroll bar reached the particular div

Mano M
  • 155
  • 1
  • 9

2 Answers2

1

Assuming you have such markup

<div>
  <div id="one">One and first</div>
  <div id="two">Two</div>
  <div id="three">Three and last</div>
</div>

Using vanilla JS, this can be done to save the position of your target element, I targeting #two, and then test current scrolling offset if it reaches the target position.

const ofsetOfTwo = document.getElementById('#two').offset().top; 
window.onscroll = function(e) {  
   if (window.scrollY === divTowPosition) {
      //Do stuff...
   }
}
Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30
0

Try getting the offset and compare with current scroll, try

var targetOffset = $("div").offset().top;

$(window).scroll(function(){
    if ( $w.scrollTop() > targetOffset ) {   
        // Do stuffs here
    }
});

you can refer HERE.

Sarath
  • 2,318
  • 1
  • 12
  • 24