Skip to content

Instantly share code, notes, and snippets.

@ashumeow
Forked from wusuopubupt/subnav.css
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashumeow/8998608 to your computer and use it in GitHub Desktop.
Save ashumeow/8998608 to your computer and use it in GitHub Desktop.
section {
padding-top: 60;
}
.subnav {
margin-bottom: 60;
width: 100;
height: 36;
background-color: #eeeeee; /* Old browsers */
background-repeat: repeat-x; /* Repeat the gradient */
background-image: url(""); /* try this*/
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f5f5f5', endColorstr='#eeeeee',GradientType=0 ); /* IE6-9 */
background-image: url("") /* try this*/
border: 1px solid #e5e5e5;
-webkit-border-radius: 4;
-moz-border-radius: 4;
border-radius: 4;
}
.subnav .nav {
margin-bottom: 0;
}
.subnav .nav > li > a {
margin: 0;
padding-top: 11;
padding-bottom: 11;
border-left: 1px solid #f5f5f5;
border-right: 1px solid #e5e5e5;
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
}
.subnav .nav > .active > a,
.subnav .nav > .active > a:hover {
padding-left: 13;
color: #777;
background-color: #e9e9e9;
border-right-color: #ddd;
border-left: 0;
}
.subnav .nav > .active > a .caret,
.subnav .nav > .active > a:hover .caret {
border-top-color: #777;
}
.subnav .nav > li:first-child > a,
.subnav .nav > li:first-child > a:hover {
border-left: 0;
padding-left: 12;
-webkit-border-radius: 4 0 0 4;
-moz-border-radius: 4px 0 0 4;
border-radius: 4 0 0 4;
}
.subnav .nav > li:last-child > a {
border-right: 0;
}
.subnav .dropdown-menu {
-webkit-border-radius: 0 0 4px 4px;
-moz-border-radius: 0 0 4px 4px;
border-radius: 0 0 4px 4px;
}
@media (max-width: 767px) {
.subnav {
position: static;
top: auto;
z-index: auto;
width: auto;
height: auto;
background: #fff; /* whole background property since we use a background-image for gradient */
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
}
.subnav .nav > li {
float: none;
}
.subnav .nav > li > a {
border: 0;
}
.subnav .nav > li + li > a {
border-top: 1px solid #e5e5e5;
}
.subnav .nav > li:first-child > a,
.subnav .nav > li:first-child > a:hover {
-webkit-border-radius: 4 4 0 0;
-moz-border-radius: 4 4 0 0;
border-radius: 4 4 0 0;
}
}
@media (min-width: 980px) {
.subnav-fixed {
position: fixed;
top: 40px;
left: 0;
right: 0;
z-index: 1020; /* 10 less than .navbar-fixed to prevent any overlap */
border-color: #d5d5d5;
border-width: 0 0 1; /* drop the border on the fixed edges */
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
box-shadow: inset 0 1 0 #fff, 0 1 5 rgba(0,0,0,.1);
filter: progid:DXImageTransform.Microsoft.gradient(enabled=false); /* IE6-9 */
}
.subnav-fixed .nav {
width: 938;
margin: 0 auto;
padding: 0 1;
}
.subnav .nav > li:first-child > a,
.subnav .nav > li:first-child > a:hover {
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
}
}
@media (min-width: 1210px) {
.subnav-fixed .nav {
width: 1168; /* 2px less to account for left/right borders being removed when in fixed mode */
}
}
<body data-spy="scroll" data-target=".subnav" data-offset="100">
<div class="container">
<div class="subnav">
<ul class="nav nav-pills">
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
</ul>
</div>
</div>
<section id="section1">
<h1>Section 1</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</section>
<section id="section2">
<h1>Section 2</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</section>
<section id="section3">
<h1>Section 3</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</section>
</body>
(function ($) {
$(function(){
// fix sub nav on scroll
var $win = $(window),
$body = $('body'),
$nav = $('.subnav'),
navHeight = $('.navbar').first().height(),
subnavHeight = $('.subnav').first().height(),
subnavTop = $('.subnav').length && $('.subnav').offset().top - navHeight,
marginTop = parseInt($body.css('margin-top'), 10);
isFixed = 0;
processScroll();
$win.on('scroll', processScroll);
function processScroll() {
var i, scrollTop = $win.scrollTop();
if (scrollTop >= subnavTop && !isFixed) {
isFixed = 1;
$nav.addClass('subnav-fixed');
$body.css('margin-top', marginTop + subnavHeight + 'px');
} else if (scrollTop <= subnavTop && isFixed) {
isFixed = 0;
$nav.removeClass('subnav-fixed');
$body.css('margin-top', marginTop + 'px');
}
}
});
})(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment