jquery - 添加类不起作用。我的选择器不够具体吗?

标签 jquery css scroll

$(window).scroll(function() {
    if ($(this).scrollTop() > 150) {
        $( ".background" ).fadeOut();
      	$("a, h1").addClass("fade");
    } else {
        $( ".background" ).fadeIn();
      	$("a, h1").removeClass("fade");
    }
});
//Add Class
.fade
  color: #BBB

//Main
body
  margin: 0
  padding: 0
  font-family: Helvetica

.header
  width: 100%
  height: 100px
  position: fixed
  top: 0px
  z-index: 3
  .background, .labels
    position: absolute
    top: 0px
    width: 100%
    height: 100%
  .labels
    background-color: transparent
  .background
    background-color: #0097A7    
  h1
    float: left
    padding: 11px 0 0 40px
    color: #FFF
  .nav
    float: right
    list-style-type: none
    margin-top: 40px
    li
      display: inline
      padding-right: 60px
      a
        color: #FFF
        font-size: 19px
        text-decoration: none

.content
  width: 100%
  height: 5000px
  background-color: #FFF
  

  
body
  .header
    .background
    .labels
      h1 JB
      ul.nav
        li 
          a(href="#") Work
        li 
          a(href="#") About Me
        li 
          a(href="#") Contact
   .content

试图让标题从蓝绿色淡出到白色(正在 工作),同时让文本从白色变为黑色。我最终会在 css 中添加一个转换来更改文本,但现在我正在尝试让 addClass 正常工作。

编辑: 我得到它与以下工作:

.fade-in
  transition: opacity, 1s, ease
  color: #000 !important

.fade-out
  transition: opacity, 1s, ease
  color: #FFF !important

$(window).scroll(function() {
    if ($(this).scrollTop() > 150) {
        $( ".background" ).fadeOut();
        $("a, h1").removeClass("fade-out");
        $("a, h1").addClass("fade-in");
    } else {
        $( ".background" ).fadeIn();
        $("a, h1").removeClass("fade-in");
        $("a, h1").addClass("fade-out");
    }
});

感谢您的帮助!

最佳答案

试试这个,让我知道它是否有效。如果不能,你能做一个 fiddle 吗?我绝对可以修复它,但您的标记显示不正确。

$(window).scroll(function() {
    if ($(this).scrollTop() > 150) {
        $( ".background" ).fadeOut();
        $("h1, li a").addClass("fade");
    } else {
        $( ".background" ).fadeIn();
        $("h1, li a").removeClass("fade");
    }
});

关于jquery - 添加类不起作用。我的选择器不够具体吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865241/

相关文章:

jquery - jqGrid 中的时间格式

c# - 允许 Windows 窗体 C# 的像素滚动?

css - Chrome 在 z 索引方面是否正在退化(或者我做错了什么)?

javascript - JQuery 在 div 内滚动

javascript - 为什么我不能在控制台中循环滚动浏览器窗口?

javascript - 如何通过导航栏的高度更改平滑滚动 JQuery 动画的偏移量

javascript - 在循环中切换函数

Chrome(webkit)和Firefox之间的jQuery定位差异

html - 我在屏幕底部设置了背景,但当我进入全屏时,下方有一个空白区域。为什么会这样?

jQuery:单击更改类