1

I want to add a fixed header when I scroll and I would like to add a transition when the class is added/removed. Currently the animation is only working when the class is added.

$(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>

4
  • Its throwing error Uncaught ReferenceError: $ is not defined Commented Jun 20, 2018 at 4:45
  • It's working fine on my end, I checked the console. Commented Jun 20, 2018 at 4:49
  • Once you remove the class, you're removing position: fixed - that's why you won't see a transition. Commented Jun 20, 2018 at 4:51
  • And what can I do to solve this problem then? Commented Jun 20, 2018 at 4:52

3 Answers 3

1

In your css, the properties like position, text-align etc do not support transition.So I moved that properties from css and is now injecting through js with/with out a delay. Also I applied position:absolute for the navigation and aligned it at the top of the body. It normally behaves as a floated block element. May be you have to apply a top margin for the proceeding element to avoid overlap. If that is okay, you may try the below example. <div class="spacer"></div> is a dummy element to make the page scroll-able.

$(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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! This is working for me, now I'll break up the code to understand what's going on here.
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>

Comments

0

Try adding transition for .sticky class also

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

1 Comment

Yeah I tried that but it doesn't work, I thought it would :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.