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.
- $(document).ready(function(){
- function scrollFunc(e) {
- if ( typeof scrollFunc.x == 'undefined' ) {
- scrollFunc.x=window.pageXOffset;
- scrollFunc.y=window.pageYOffset;
- }
- var diffX=scrollFunc.x-window.pageXOffset;
- var diffY=scrollFunc.y-window.pageYOffset;
- if(diffX<0){
- // Scroll right
- }
- else if(diffX>0){
- // Scroll left
- }
- else if(diffY<0){
- // Scroll down
- $('#topNav').fadeIn();
- }
- else if(diffY>0){
- // Scroll up
- if(document.body.scrollTop === 0){
- $('#topNav').fadeOut();
- }
- }
- else{
- // First scroll event
- $('#topNav').css('position','fixed').show().fadeIn();
- }
- scrollFunc.x=window.pageXOffset;
- scrollFunc.y=window.pageYOffset;
- }
- window.onscroll=scrollFunc;
- });