0

I have a header that appears when the page scrolls down. I am trying to add css transitions to make it fade in and out because I've read that using javascript for fading is not as efficient.

.header-wrapper {
    top:0;
    left:0;
    right:0;
    position: fixed;
    display:none;
    height: 60px;
    border-top: 1px solid #000;
    background: red;
    z-index: 1;
}
.header-wrapper.active {
     display:block;   
}
.header {
    background-color:#000;
    height:80px;
}

Here is the js fiddle

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

    // if above 300 and doesn't have active class yet
    if (y > 300 && !$('.header-wrapper').hasClass('active')) {
        $('.header-wrapper').addClass('active');

    // if below 300 has still has active class
    } else if(y <= 300 && $('.header-wrapper').hasClass('active')) {
        $('.header-wrapper').removeClass('active');
    }
    });
user2684452
  • 701
  • 2
  • 14
  • 31

3 Answers3

1

Transitions are added with the css3 property transition.

One common reason for confusion: you can only transition properties that accept numeric values. Thus, you can't transition between display: block and display: none.

However you can transition between opacity: 0 and opacity: 1 with:

transition: 0.5s opacity

That would look something like this:

.bottomMenu {
    ...
    opacity: 0;
    transition: 0.5s opacity;
    ...
}
.bottomMenu.active {
     opacity: 1;   
}

For your particular case, I might recommend transitioning the height between 0 and 60px.

For that you can use:

transition: 0.5s height

So:

.bottomMenu {
    ...
    height: 0;
    transition: 0.5s height;
    ...
}
.bottomMenu.active {
     height: 80px;   
}
FreePender
  • 4,770
  • 2
  • 18
  • 15
  • This seems like it would work for making the header appear and disappear with a fade, however I'm already achieving that with my javascript. See edit. I am just wanting to keep it how it is and ease it in and out... – user2684452 Sep 12 '14 at 17:19
  • As it says in my comment above, you can't transition from "Display: block" to "Display: none" you have to use "Opacity: 0" to "Opacity: 1" – FreePender Sep 12 '14 at 18:30
1

To animate the opacity the element must be visible. So remove the display:none and make it fully transparent (opacity:0). You can then use CSS transitions to animate the opacity when the classname changes:

.bottomMenu {
    ...
    display:block;
    opacity: 0;
    transition: opacity .25s ease-in-out;
    -moz-transition: opacity .25s ease-in-out;
    -webkit-transition: opacity .25s ease-in-out;
}
.bottomMenu.active {
     opacity:1   
}

http://jsfiddle.net/oL9ro4gL/6/

Furthermore, you're not restricted to just animating the opacity:

.bottomMenu {
    ...
   transition: all .25s ease-in-out;
}
.bottomMenu.active {
    opacity:1;
    height: 60px;
    background-color: blue;
    transform:rotate(180deg);
    color:white;
    font-size:40px;
    etc...
}

http://jsfiddle.net/oL9ro4gL/8/

Moob
  • 14,420
  • 1
  • 34
  • 47
-1

Unfortunately, you can't animate the display property. See this question and its suggestions for workarounds.

Community
  • 1
  • 1
seancdavis
  • 2,739
  • 2
  • 26
  • 36