0

How can I make an effect when mouse scrolls to a particular position?

<div id="target">
  <!-- some data here -->
</div>

jQuery

var target = $('#target');

if(target.scrollTop() > 10){
  alert('');
}
CroMagnon
  • 1,206
  • 7
  • 20
  • 32
Daniel
  • 1,358
  • 5
  • 18
  • 35

2 Answers2

0

You might have to select the entire HTML document for the scroll function, instead of just one specific div

jQuery

$(document).scroll(function(){  
    if ($(window).scrollTop() > 10){
      // if the current scroll of the window is greater than 10px
    alert('');
}
});
Jayleen
  • 75
  • 7
0

You need to put your code inside $(window).on('scroll', function(){}); which will fire each time the window is scrolled like this:

    $(window).on('scroll', function(){
    var target = $('#target');
    if(target.scrollTop() > 10){
    console.log("Scrolled 10px");
    alert('');
       }
    });
Unmitigated
  • 46,070
  • 7
  • 44
  • 60