javascript - 菜单图标根据 HTML 类/属性更改颜色

标签 javascript jquery html css

我有一个固定的菜单,需要根据不同部分的背景颜色更改颜色。

我已经开始使用数据颜色属性,但我在弄清楚如何删除和添加类到#open-button 时遇到了问题。我能够添加类(class),但删除了我正在努力学习的正确类(class)。

Here is my fiddle .

还有我的代码:

<div id="top-wrapper">
<div class="menu-button" id="open-button"><span></span></div>
</div>

<section class="section black-bg" data-color="icon-white">
  Section One is black
</section>
<section class="section white-bg" data-color="icon-black">
  Section Two is white
</section>
<section class="section black-bg" data-color="icon-white">
  Section Three is black
</section>
<section class="section white-bg" data-color="icon-black">
  Section Four is White
</section>

jQuery:

$(function(){
$(window).on('scroll', function() {
        var scrollTop = $(this).scrollTop();
        $('.section').each(function() {
            var topDistance = $(this).offset().top;
            if ( (topDistance) < scrollTop ) {
                $('#open-button').addClass($(this).attr('data-color'));
            }
        });
    });
})

最佳答案

您可以使用 removeClass()具有使用正则表达式的功能。

此正则表达式将匹配 icon-*

$(function() {
  $(window).on('scroll', function() {
    var scrollTop = $(this).scrollTop();
    $('.section').each(function() {
      var topDistance = $(this).offset().top;
      if ((topDistance) < scrollTop) {
        //Add this
        $("#open-button").removeClass(function(index, className) {
          return (className.match(/(^|\s)icon-\S+/g) || []).join(' ');
        });
        //
        $('#open-button').addClass($(this).attr('data-color'));
      }
    });
  });
})
.section {
  height: 500px;
  width: 100%;
}

.black-bg {
  background: #000000;
  color: #ffffff;
}

.white-bg {
  background: #ffffff;
  color: #000000;
}

#top-wrapper {
  position: fixed;
  z-index: 1005;
  width: 125px;
  top: 40px;
  left: 47px;
}

#open-button {
  z-index: 10005;
  display: block;
  width: 30px;
  height: 40px;
  margin: 20px 0 0 20px;
  float: right;
  position: relative;
  background: #fff;
}

#open-button.icon-black {
  background: #000;
}

#open-button.icon-white {
  background: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="top-wrapper">
  <div class="menu-button" id="open-button"><span></span></div>
</div>

<section class="section black-bg" data-color="icon-white">
  Section One is black
</section>
<section class="section white-bg" data-color="icon-black">
  Section Two is white
</section>
<section class="section black-bg" data-color="icon-white">
  Section Three is black
</section>
<section class="section white-bg" data-color="icon-black">
  Section Four is White
</section>

关于javascript - 菜单图标根据 HTML 类/属性更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45876818/

相关文章:

javascript - Edge 浏览器中的 focus()

javascript - 为什么我不能在方法中引用从 JavaScript 原型(prototype)继承的方法

javascript - 为什么 jquery offsetParent() 返回 html 而 native javascript 返回 body

css - 当 div 设置其父级的高度时,使 div 成为其容器的 100%

html - 有没有办法水平移动包含边距 : 0 auto with extra pixel to the left or right? 的元素

javascript - 删除 cookie 键值对

php - 当页面加载到 div 中时,让 jQuery 的函数运行

php - 在动态生成的每个内容上显示 &lt;textarea&gt;

php - Facebook 登录 : How to combine JavaScript with PHP SDK?

c# - 使用 C# 设置 HTML 输入文本框的显示文本