I'm implementing the collapsing sticky header from this article on how to create an animated sticky header, with CSS3 and jQuery.
The header is currently animating all CSS changes with the following line:
<!-- language: lang-css -->transition: all 0.4s ease;
When the sticky header is applied the following css properties change:
<!-- language: lang-css --> font-size: 72px; /* --> 24px; */
line-height: 108px; /* --> 48px; */
height: 108px; /* --> 48px; */
background: #335C7D; /* --> #efc47D; */
text-align: center; /* --> left; */
padding-left: auto; /* --> 20px; */
So transition all should gracefully move between the existing value and new value for each of those properties.
However, I'm noticing that when I scroll down, the text nicely animates smaller and to the left. However, when I scroll up, the text seems to jump out of the middle instead of moving right when it loses the text-align:left
property.
$(window).scroll(function () {
var belowTop = $(this).scrollTop() > 30;
$('header').toggleClass('sticky', belowTop);
});
<!-- language: lang-css -->
/*reset */
* {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
header {
position: fixed;
width: 100%;
font-family: 'PT Sans', sans-serif;
transition: all 0.4s ease;
color: #fff;
font-size: 72px;
line-height: 108px;
height: 108px;
background: #335C7D;
text-align: center;
}
header.sticky {
font-size: 24px;
line-height: 48px;
height: 48px;
background: #efc47D;
text-align: left;
padding-left: 20px;
}
<!-- language: lang-html -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header><h1>Sticky Header</h1></header>
<img src="http://i.stack.imgur.com/8pezV.jpg" alt="Big Image" />
<!-- end snippet -->