-2

Possible Duplicate:
How can I determine the direction of a jQuery scroll event?

I need a function that detects if I scrolling UP or Down in Javascript/jquery to Fire Some events.

Something Like This:

if(ScrollTop)
  {
    alert('SCROLL TOP');    
  }
else if(ScrollDown)
  {
    alert('SCROLL DOWN');  
  }
Community
  • 1
  • 1
Sbml
  • 1,907
  • 2
  • 16
  • 26

3 Answers3

4

C: http://api.jquery.com/scroll/

George Cummins
  • 28,485
  • 8
  • 71
  • 90
1

It's simple solution working in all browsers: jQuery.

Here is the solution: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_25533492.html

<html>
<head>
  <style>
  div { color:blue; }
  p { color:green; }
  span { color:red; display:none; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
        <div>Try scrolling the iframe.</div>
  <p>Paragraph - <span>Scroll happened!</span></p>
<script>
    $("p").clone().appendTo(document.body);
    $("p").clone().appendTo(document.body);
    $("p").clone().appendTo(document.body);

    var lastScrollTop = 0
    var currentScrollTop = 0
    $(window).scroll(function (event) { 
        lastScrollTop = currentScrollTop
        currentScrollTop = $(document).scrollTop()
        if (currentScrollTop > lastScrollTop) 
            $("span").text("going down")            
        else
            $("span").text("going up")
         $("span").css("display", "inline").fadeOut("slow"); 

    });
</script>
</body>
</html>

Basically it's just detecting current scroll position.

Kaminari
  • 1,387
  • 3
  • 17
  • 32
1

Something like this http://jsfiddle.net/SjFNq/

$(window).scroll(function(){
    var prevTop = $(window).data("top");
    var  newTop = $(this).scrollTop();
    if(prevTop)
    {
      if(prevTop>newTop )
      {
        alert("top");
      }
    else{
      alert("bottom");
       }
    }
    $(window).data("top",newTop );

});
Jishnu A P
  • 14,202
  • 8
  • 40
  • 50