CSS转换无法使用jQuery .addClass和.removeClass

时间:2016-10-27 03:52:08

标签: jquery css css3

我有一个顶部固定导航,当用户滚动时,我加载其他样式,如将其更改为白色。

我遇到转换css元素无效的问题。它没有像我想要的那样动画下来。我想从Slack复制相同风格的导航: https://slack.com/customers

我下面有jQuery和CSS。我还设置了一个jsfiddle来查看我遇到的问题: https://jsfiddle.net/a1Ltya6f/

我目前的jQuery看起来像这样:

$(document).ready(function () {
  $(window).scroll(function() {
  // 100 = The point you would like to fade the nav in.

    if ($(window).scrollTop() > 50 ){
        $('.top').addClass('fixed');

    } else {

      $('.top').removeClass('fixed');
    };
  });
});

这是我的CSS:

nav.top {
    position: fixed;
    top: 0;
    width: 100%;
    height: 70px;
    z-index: 99;
    -webkit-transform: translate3d(0,0,0);
    -moz-transform: translate3d(0,0,0);
    -ms-transform: translate3d(0,0,0);
    transform: translate3d(0,0,0);
}
.fixed {
    -webkit-transform: translate3d(0,-80px,0);
    -moz-transform: translate3d(0,-80px,0);
    -ms-transform: translate3d(0,-80px,0);
    transform: translate3d(0,-80px,0);
    background: #fff;
    box-shadow: 0 1px 1px rgba(0,0,0,.1);
}

nav.top ul {
    list-style-type: none;
    margin: 0 20px 0 0;
    padding: 0;
    float: right;
    line-height: 4.375rem;
}
nav.top ul li {
    display: inline-block;
}
nav.top ul a.btn_sticky {
    color: #404B55;
    box-shadow: inset 0 0 0 2px #404b55;
}
nav.top ul a {
    color: #404B55;
    text-decoration: none;
    display: inline-block;
    font-size: .9375rem;
    font-weight: 700;
    margin-left: 9px;
    position: relative;
    cursor: pointer;
    line-height: 1em;
    padding: 8px 7px 9px;
    border-radius: 5px;
    opacity: .8;
}

1 个答案:

答案 0 :(得分:0)

(nav.top)

中缺少转换代码

过渡:所有.4s;

您可以根据您的需要调整时间顺利。

并在css中添加带有固定类的导航

$(document).ready(function () {
  $(window).scroll(function() {
  // 100 = The point you would like to fade the nav in.

  	if ($(window).scrollTop() > 50 ){
   		$('.top').addClass('fixed');

    } else {

      $('.top').removeClass('fixed');
   	};
  });
});
body {
  margin: 0;
}
nav.top {
    position: fixed;
    top: 0;
    width: 100%;
    height: 70px;
    z-index: 99;
    -webkit-transform: translate3d(0,0,0);
    -moz-transform: translate3d(0,0,0);
    -ms-transform: translate3d(0,0,0);
    transform: translate3d(0,0,0);
    transition:all .4s;
}
nav.fixed {
    -webkit-transform: translate3d(0,-80px,0);
    -moz-transform: translate3d(0,-80px,0);
    -ms-transform: translate3d(0,-80px,0);
    transform: translate3d(0,-80px,0);
    background: #fff;
    box-shadow: 0 1px 1px rgba(0,0,0,.1);
}

nav.top ul {
    list-style-type: none;
    margin: 0 20px 0 0;
    padding: 0;
    float: right;
    line-height: 4.375rem;
}
nav.top ul li {
    display: inline-block;
}
nav.top ul a.btn_sticky {
    color: #404B55;
    box-shadow: inset 0 0 0 2px #404b55;
}
nav.top ul a {
    color: #404B55;
    text-decoration: none;
    display: inline-block;
    font-size: .9375rem;
    font-weight: 700;
    margin-left: 9px;
    position: relative;
    cursor: pointer;
    line-height: 1em;
    padding: 8px 7px 9px;
    border-radius: 5px;
    opacity: .8;
}

.block {
  background-color: #1C90F3;
  height: 1200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="top">
       <ul>
          <li><a href="">Product</a></li>
          <li><a href="">Pricing</a></li>
          <li><a href="">Support</a></li>
          <li><a href="">Create a new team</a></li>
          <li><a href="">Find your team</a></li>
          <li class="sign_in"><a href="" class="btn_sticky">Sign in</a></li>
       </ul>   
</nav>

<div class="block"></div>

nav.fixed