首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >addClass removeClass不工作

addClass removeClass不工作
EN

Stack Overflow用户
提问于 2016-06-18 07:35:26
回答 4查看 1.4K关注 0票数 4

我第一次在这里问问题。我搜索了很多问题,但找不到类似的问题。

我正在构建一个带有bootstrap3的公司网站。我检查一下,看看最新版本的镀铬和safari是否有效。

我试图让我的肚脐缩小它的高度和改变背景色使用JQuery addClass和removeClass,但它似乎根本不起作用。当我通过JQuery更改CSS属性时,它可以工作。我可以改变背景色。因此,我试图通过Jquery更改多个CSS属性,但无法工作。它只允许我改变背景色。

代码语言:javascript
复制
$(document).ready(function() {
  $(window).scroll(function() {
    if ($(window).scrollTop() > 50) {
      $("#top-bar").addClass('animated-header');
    } else {
      $("#top-bar").removeClass('animated-header');
    }
  });

  $("#clients-logo").owlCarousel({
    itemsCustom: false,
    pagination: false,
    items: 5,
    autoplay: true,
  })

}); //this is the part doesn't work

$(document).ready(function() {
  $(window).scroll(function() {
    if ($(window).scrollTop() > 50) {
      $("#top-bar").css("background-color", "#ef7c7c");
    } else {
      $("#top-bar").css("background-color", "transparent");
    }
  });

  $("#clients-logo").owlCarousel({
    itemsCustom: false,
    pagination: false,
    items: 5,
    autoplay: true,
  })
}); //this part works perfectly
代码语言:javascript
复制
#top-bar {
  background: transparent;
  color: #fff;
  -webkit-transition: all 0.2s ease-out 0s;
  transition: all 0.2s ease-out 0s;
  padding: 30px 0;
  height: 15%;
}
#top-bar .animated-header {
  /*Background Color of nav bar when scrolled*/
  background-color: #ef7c7c;
  padding: 10px 0;
  height: 10%;
  -webkit-box-shadow: 0 0 3px 0 rgba(0, 0, 0, 0.1);
  box-shadow: 0 0 3px 0 rgba(0, 0, 0, 0.1);
}
代码语言:javascript
复制
<header id="top-bar" class="navbar-fixed-top animated-header">
  <div class="container">

非常感谢您的帮助!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-06-18 07:50:56

CSS代码中有错误:

代码语言:javascript
复制
#top-bar .animated-header
    /*   ^      
        Here the typo, you specified this properties to
        element, that will be a children of a #tob-bar
        element
    */

所以,在这里,正确的一个:

代码语言:javascript
复制
#top-bar.animated-header
票数 4
EN

Stack Overflow用户

发布于 2016-06-18 07:44:11

问题不在使用.addClass()的JS中,问题是您的CSS中的选择器是错误的。这是:

代码语言:javascript
复制
#top-bar .animated-header {

应该是这样:

代码语言:javascript
复制
#top-bar.animated-header {

也就是说,删除.之前的空格,因为选择器将元素与animated-header类匹配,该类类是#top-bar的后代。如果没有空格,它与#top-bar元素本身匹配,如果它还有animated-header类的话。

票数 9
EN

Stack Overflow用户

发布于 2016-06-18 07:51:22

最好在css中应用所有的样式。将#top-bar .animated-header更改为#top-bar.animated-header,因为这里有一个额外的空间,导致您的样式没有被应用。但是,如果您真的想使用jQuery应用样式,可以这样做:

代码语言:javascript
复制
$(document).ready(function(){
    $(window).scroll(function () {
        if ($(window).scrollTop() > 50) {
            $("#top-bar").addClass('animated-header').css({'background-color': '#ef7c7c', 'height': '10%'});
        } else {
            $("#top-bar").removeClass('animated-header').css({'background-color': '', 'height': ''});
        }
    });
});

代码语言:javascript
复制
$(document).ready(function(){
    $(window).scroll(function () {
        if ($(window).scrollTop() > 50) {
            $("#top-bar").addClass('animated-header');
        } else {
            $("#top-bar").removeClass('animated-header');
        }
    });
});
代码语言:javascript
复制
body {
  height: 1000px;
}
#top-bar {
  background: transparent;
  color: #fff;
  -webkit-transition: all 0.2s ease-out 0s;
  transition: all 0.2s ease-out 0s;
  padding: 30px 0;
  height: 15%;
}
#top-bar.animated-header {/*Background Color of nav bar when scrolled*/
  background-color: #ef7c7c;
  padding: 10px 0;
  height: 10%;
  -webkit-box-shadow: 0 0 3px 0 rgba(0, 0, 0, 0.1);
  box-shadow: 0 0 3px 0 rgba(0, 0, 0, 0.1);
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<header id="top-bar" class="navbar-fixed-top animated-header">
            <div class="container">

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37894515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档