Created
August 29, 2012 22:21
-
-
Save PrototypeAlex/3519662 to your computer and use it in GitHub Desktop.
Dynamically sized Nav menus with fixed scrolling
This file contains hidden or 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
# Menubar Scrolling | |
header_h = -> $('#header').outerHeight() | |
content_h = -> $('#content .container .row').outerHeight() - 22 | |
footer_h = -> $('#footer').outerHeight() | |
left_column_h = -> $('.content_left_column').outerHeight() | |
right_column_h = -> $('.content_right_column').outerHeight() | |
nav_and_flash_h = -> $('#content-header').outerHeight() + $('.flash_container').outerHeight() | |
sticky_nav_h = -> $('#sticky-nav').outerHeight() | |
sticky_nav_top_margin = 0 | |
current_bottom_of_sticky_nav = -> | |
$(window).scrollTop() + sticky_nav_h() | |
sticky_nav_top_max = -> header_h() + nav_and_flash_h() | |
sticky_nav_bottom_max = -> header_h() + content_h() | |
sticky_nav_top_margin_max = -> $(window).height() - sticky_nav_h() - 22 | |
sticky_nav_is_at_top = -> return $(window).scrollTop() > sticky_nav_top_max() | |
sticky_nav_is_at_bottom = -> | |
if sticky_nav_is_larger_than_window() | |
return current_bottom_of_sticky_nav() + sticky_nav_top_margin > sticky_nav_bottom_max() | |
else | |
return current_bottom_of_sticky_nav() > sticky_nav_bottom_max() | |
sticky_nav_can_float = -> right_column_h() < left_column_h() | |
sticky_nav_is_larger_than_window = -> sticky_nav_h() > $(window).height() | |
previous_scroll_top = $(window).scrollTop() | |
scrolling_up = -> return previous_scroll_top > $(window).scrollTop() | |
$(window).scroll -> | |
if sticky_nav_can_float() && sticky_nav_is_at_top() | |
$('#sticky-nav').addClass('fixed').css('top', '0').css('margin-top', calculate_sticky_nav_top_margin() + 'px') | |
else | |
$('#sticky-nav').removeClass('fixed').css('margin-top', '0') | |
sticky_nav_top_margin = 0 | |
previous_scroll_top = $(window).scrollTop() | |
calculate_sticky_nav_top_margin = -> | |
if sticky_nav_is_at_bottom() | |
# If the nav is larger than than window | |
if sticky_nav_is_larger_than_window() && scrolling_up() && sticky_nav_top_margin < 0 | |
# Substitute the scroll distance from the top margin | |
return sticky_nav_top_margin = Math.min(0, sticky_nav_top_margin + (previous_scroll_top - $(window).scrollTop())) | |
else | |
return sticky_nav_top_margin = (header_h() + content_h()) - current_bottom_of_sticky_nav() | |
else if sticky_nav_is_larger_than_window() | |
if scrolling_up() && sticky_nav_top_margin < 0 | |
return sticky_nav_top_margin = Math.min(0, sticky_nav_top_margin + (previous_scroll_top - $(window).scrollTop())) | |
if !scrolling_up() && sticky_nav_top_margin > sticky_nav_top_margin_max() | |
return sticky_nav_top_margin = Math.max(sticky_nav_top_margin_max(), sticky_nav_top_margin - ($(window).scrollTop() - previous_scroll_top)) | |
else | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment