Last active
August 29, 2015 14:07
-
-
Save TechFounder/f7690f4b2d96bb7a6367 to your computer and use it in GitHub Desktop.
How to setup a fixed navbar using javascript when you have an extra header on top of it. Or, a fading navbar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The basic idea here is that you set a num of pixels before triggering the css "fixed" | |
# which would freeze whatever you div attribute you set, in this case"nav". | |
# This is a simple way to do it but there's a library you can use to do the same thing. | |
# http://imakewebthings.com/jquery-waypoints/shortcuts/sticky-elements/ | |
num = 250 #number of pixels before modifying styles | |
$(window).bind "scroll", -> | |
if $(window).scrollTop() > num | |
$("nav").addClass "fixed" | |
else | |
$("nav").removeClass "fixed" | |
# Here's another way to write the code | |
$(window).on "scroll", -> | |
if $(window).scrollTop() >= 462 and not $("nav").hasClass("naxed-top") | |
$("nav").addClass "fixed" | |
else $("nav").removeClass "navbar-fixed-top" if $(window).scrollTop() < 462 and $("nav").hasClass("navbar-fixed-top") | |
# This is a way to first hide the navbar and then make it appear once it passes .navbar height | |
$('.homepage-navbar').hide() | |
starting_position = $('.navbar').outerHeight(true) | |
$(window).scroll -> | |
yPos = ($(window).scrollTop()) | |
if yPos > starting_position | |
$('.homepage-navbar').fadeIn() | |
else | |
$('.homepage-navbar').fadeOut() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.fixed { | |
width: 100%; | |
position: fixed; | |
top: 0; | |
z-index: 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment