简体   繁体   English

向上滚动时自动显示/隐藏导航更改颜色

[英]Auto show/hide nav changing color when scrolling up

I would like to change the nav color when scrolling up, coming back to its default color at the top. 我想在向上滚动时更改导航颜色,然后回到顶部的默认颜色。

So, we'd have four steps: 因此,我们将分四个步骤:

1) Nav shows when loading page with its original color 1)加载原始颜色的页面时,Nav显示

2) Nav hides when scrolling down 2)向下滚动时Nav隐藏

3) Nav changes its color while scrolling up, BUT 3)导航在向上滚动的同时更改其颜色,但是

4) It turns to its original color when reaching top 4)到达顶部时变为原始颜色

I have this initial code (steps 1 & 2): 我有以下初始代码(步骤1和2):

$(function(){
var prevScroll = 0,
curDir = 'down',
prevDir = 'up';

$(window).scroll(function(){
    if($(this).scrollTop() >= prevScroll){
      curDir = 'down';
      if(curDir != prevDir){
      $('nav').stop();
        $('nav').animate({ top: '-100px' }, 300);
      prevDir = curDir;
      }
  } else {
      curDir = 'up';
      if(curDir != prevDir){
      $('nav').stop();
      $('nav').animate({ top: '0px' }, 300);
      prevDir = curDir;
      }
  }
  prevScroll = $(this).scrollTop();
});
})

Take a look at the fiddle: https://jsfiddle.net/grguryrw/ 看看小提琴: https : //jsfiddle.net/grguryrw/

 var prevScrollTop = 0; $(document).scroll(function(){ var currentScrollTop = $(document).scrollTop(); if(currentScrollTop < 1){ $('header').removeClass('another-color'); $('header').addClass('original-color'); } else if(currentScrollTop > prevScrollTop){ $('header').animate({ top: '-55px' }, 300); } else { $('header').removeClass('original-color'); $('header').addClass('another-color'); $('header').animate({ top: '0px' }, 300); } prevScrollTop = currentScrollTop; }); 
 header { height: 55px; width: 100%; position: fixed; } .original-color { background-color: #0078a3; } .another-color { background-color: red; } div { padding-top: 60px; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header class='original-color'> </header> <div> Did the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offsDid the rules change..? The year Chelsea won UCL being 6th in the league they were directly seeded into the Next Years UCL and 4th placed Tottenham did not qualify for UCL or Play Play offs </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM