Detect scroll direction using jQeury

I have a content div on a web page that allows users to jump to any point on the current page.  These documents are pretty long and so the content div moves up and down to stay on screen with use of scrollTop().

However, as these documents become larger, the content div is becoming larger than the viewport and as it lines up with the top of the page, it is sometimes impossible to get to the bottom of the content div.

I have come up with a solution that requires me to know what direction the viewport is scrolling in.  Can this be done?  If I can find the scrolltop() value on scroll start and scrol end I could work out the direction so this may also be an option

Many Thanks
LVL 6
snavebelacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahomeCommented:
Hi use the scroll()-event to know when scrolling happens and save the scrollTop() in a variable, so you know next time the difference. See code example:

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snavebelacAuthor Commented:
Great - Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.