开发者

How can I run a function when the user scrolls [duplicate]

开发者 https://www.devze.com 2023-03-13 03:40 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_运维百科 Possible Duplicate: How can I determine the direction of a jQuery scroll event?
This question already has answers here: Closed 11 years ago. 开发者_运维百科

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');  
  }


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


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.


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 );

});
0

精彩评论

暂无评论...
验证码 换一张
取 消