删除类时CSS过渡不起作用

时间:2018-06-20 04:40:50

标签: javascript jquery css

我想在滚动时添加一个固定的标题,并且想在添加/删除该类时添加一个过渡。当前,仅当添加类时,动画才起作用。

$(window).scroll(function() {
      var scroll = $(window).scrollTop();

      if (scroll >= 800) {
          $(".nav").addClass("sticky");
      } else {
        $(".nav").removeClass("sticky");
      }
    });
.nav {
  font-weight: 600;
  text-transform: uppercase;
  transition: all 400ms ease;

  &__link {
    text-decoration: none;
    color: white;

    &:not(:last-child) {
      margin-right: 6rem;
    }
  }
}

.sticky {
  position: fixed;
  top: 0;
  left: 0;
  padding: 1rem 0;
  width: 100%;
  z-index: 1;
  text-align: center;
  background: #222;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav class="nav">
        <a href="#" class="nav__link">Home</a>
        <a href="#" class="nav__link">About us</a>
        <a href="#" class="nav__link">Menu</a>
        <a href="#" class="nav__link">Drinks</a>
        <a href="#" class="nav__link">Reservations</a>
      </nav>

3 个答案:

答案 0 :(得分:1)

$(document).ready(function() {
            
            if ($('.navbar').length > 0) {
                $(window).on("scroll load resize", function() {
                    checkScroll();
                });
            }
});


function checkScroll() {
            var startY = $('.navbar').height() * 2;
            if ($(window).scrollTop() > startY) {
                $('.navbar').addClass("scrolled");
            } else {
                $('.navbar').removeClass("scrolled");
            }
        }
p{
padding-top:72px;
}

.navbar{
    background: #ffee58;
    font-weight: 600;
    text-transform: uppercase;
    -webkit-transition: all 0.6s ease-out;
    -moz-transition: all 0.6s ease-out;
    -o-transition: all 0.6s ease-out;
    -ms-transition: all 0.6s ease-out;
    transition: all 0.6s ease-out;
}

.navbar.scrolled {
    background: #37474f;
    /* IE */
    box-shadow: 0 4px 2px -2px rgba(0, 0, 0, .2);
    z-index: 999;
    /* NON-IE */
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
    
<header>
<nav class="navbar fixed-top stroke">
        <a href="#" class="nav__link">Home</a>
        <a href="#" class="nav__link">About us</a>
        <a href="#" class="nav__link">Menu</a>
        <a href="#" class="nav__link">Drinks</a>
        <a href="#" class="nav__link">Reservations</a>
</nav>
</header>
<body>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
<p>
</body>

答案 1 :(得分:1)

在您的CSS中,positiontext-align等属性不支持转换。因此,我将该属性从CSS中移出,现在通过JS进行注入(不带延迟)。我还对导航应用了position:absolute并将其对齐在正文的顶部。它通常表现为浮动块元素。可能是您必须为procedure元素应用上边距以避免重叠。如果可以,您可以尝试以下示例。 <div class="spacer"></div>是使页面可滚动的虚拟元素。

$(window).scroll(function () {
    var scroll = $(window).scrollTop();

    if (scroll >= 800) {
        $(".nav").css({
            'position': 'fixed',
            'text-align': 'center'
        });
        $(".nav").addClass("sticky");
    } else {
        $(".nav").removeClass("sticky");
        setTimeout(function (){
            $(".nav").css({
                'position': 'static',
                'text-align': 'left'
            });
        },400);
    }
});
.nav {
    position:absolute;
    top: 0;
    left: 0;
    width: 100%;
    font-weight: 600;
    text-transform: uppercase;
    transition: all 400ms ease;

    &__link {
        text-decoration: none;
        color: white;

        &:not(:last-child) {
            margin-right: 6rem;
        }
    }
}

.sticky {
    padding: 1rem 0;
    width: 100%;
    z-index: 1;
    text-align: center;
    background: #ccc;
    position: fixed;
}
.spacer{
    width: 100%;
    height: 2000px;
    float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav">
    <a href="#" class="nav__link">Home</a>
    <a href="#" class="nav__link">About us</a>
    <a href="#" class="nav__link">Menu</a>
    <a href="#" class="nav__link">Drinks</a>
    <a href="#" class="nav__link">Reservations</a>
</nav>
<div class="spacer"></div>

答案 2 :(得分:0)

也尝试为.sticky类添加过渡

.sticky {
  position: fixed;
  top: 0;
  left: 0;
  padding: 1rem 0;
  width: 100%;
  z-index: 1;
  text-align: center;
  background: #222;
 transition: all 400ms ease;
}