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

标签 javascript jquery css

我想在向上滚动时更改导航颜色,回到顶部的默认颜色。

所以,我们有四个步骤:

1) Nav 在加载页面时以其原始颜色显示

2) 向下滚动时导航隐藏

3) Nav 在向上滚动时改变了它的颜色,但是

4) 到达顶部时变成原来的颜色

我有这个初始代码(第 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();
});
})

最佳答案

看看 fiddle :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>

关于javascript - 向上滚动时自动显示/隐藏导航更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493149/

相关文章:

javascript - 如何暂时禁用 d3.js 中的缩放

javascript - Fabric JS/Canvas 中的逐行文本背景颜色填充

html - 标题位置 CSS

html - CSS -> border-bottom 不显示 1.5px

javascript - JS中如何获取子串

javascript - 在 Javascript AngularJS 中从旧帖子中确定新帖子,过滤与否

javascript - 使用位置 : relative; 在图片区域内包含文本

javascript - Jquery 验证带有索引的复选框

javascript - 如果单击按钮,jQuery 会阻止重新加载

css - 通过 CSS 创建透明黑色覆盖的问题