What is the correct way to nest classes for use with Javascript?
I have an animated header using jQuery, that shrinks after 175px on the page; I also want its opacity to go down using background: rgba() (not opacity:;).
Unfortunately Bootstrap uses two separate divs and classes for the background-color and height. One class is .navbar-default which affects the colour, the other is .navbar-brand which affects the height.
Here is my code:
JS: as you can see, I've set .shrink to be added to .navbar-brand and .navbar-default when scrolling past 175px. Else, the .shrink class is removed.
<script>
$(function(){
var shrinkHeader = 175;
$(window).scroll(function() {
var scroll = getCurrentScroll();
if ( scroll >= shrinkHeader ) {
$('.navbar-brand, .navbar-default').addClass('shrink');
}
else {
$('.navbar-brand, .navbar-default').removeClass('shrink');
}
});
function getCurrentScroll() {
return window.pageYOffset || document.documentElement.scrollTop;
}
});
</script>
HTML: This is a typical Bootstrap navbar layout.
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container navbarfix">
<div class="navbar-header">
<a class="navbar-brand page-scroll svg" href="link">
<!-- logo code here -->
</a>
</div>
<!-- links here -->
</div>
</nav>
SASS: This is my SASS code, which is very similar to CSS so I'm sure you can work it out.
As you can see the shrink class should change the height and background of both nested classes, but it only changes the height. So my question is how do I format this correctly?
$header_l: 180px
$header_s: 105px
.navbar-brand
height: $header_l
transition: height 0.2s
.navbar-default
background: rgb(241,241,242)
background: transparent\9
background: rgba(241,241,242,1)
transition: all 0.1s
.shrink
.navbar-default
background: rgba(241,241,242,0.75)
.navbar-brand
height: $header_s
CSS (the SASS compiles to this)
.navbar-default {
background: #f1f1f2;
background: transparent\9;
background: #f1f1f2;
-webkit-transition: all 0.1s;
transition: all 0.1s
}
.navbar-brand {
height: 180px;
-webkit-transition: height 0.2s;
transition: height 0.2s;
}
.shrink .navbar-default {
background:rgba(241,241,242,0.75);
}
.shrink .navbar-brand {
height:105px;
}