Help fading element in on scroll down and fade it out when scroll up has reached scrollTop

Help fading element in on scroll down and fade it out when scroll up has reached scrollTop

I am trying to fade an element, #topNav, in when the scroll-bar goes down and then fade it out when the scroll-bar goes up and reaches the scrollTop.

With this code, it fades in on scrollDown and when it hits the top.
  1. $(document).ready(function(){
  2.       function scrollFunc(e) {
  3.             if ( typeof scrollFunc.x == 'undefined' ) {
  4. scrollFunc.x=window.pageXOffset;
  5. scrollFunc.y=window.pageYOffset;
  6. }
  7. var diffX=scrollFunc.x-window.pageXOffset;
  8. var diffY=scrollFunc.y-window.pageYOffset;

  9. if(diffX<0){
  10.             // Scroll right
  11. }
  12. else if(diffX>0){
  13.       // Scroll left
  14. }
  15. else if(diffY<0){
  16.       // Scroll down
  17.       $('#topNav').fadeIn();
  18. }
  19. else if(diffY>0){
  20. // Scroll up
  21. if(document.body.scrollTop === 0){
  22. $('#topNav').fadeOut();
  23. }
  24. }
  25. else{
  26.       // First scroll event
  27.       $('#topNav').css('position','fixed').show().fadeIn();
  28. }
  29. scrollFunc.x=window.pageXOffset;
  30. scrollFunc.y=window.pageYOffset;
  31. }
  32. window.onscroll=scrollFunc;
  33. });