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,218
  • 7
  • 20
  • 32
Daniel
  • 1,438
  • 6
  • 20
  • 37

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
  • 174
  • 1
  • 12
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
  • 76,500
  • 11
  • 62
  • 80