Saturday, May 11, 2024
31
rated 0 times [  34] [ 3]  / answers: 1 / hits: 32107  / 13 Years ago, tue, february 7, 2012, 12:00:00

I need to quickly knock up the functionality of the twitter bootstraps main navigation and sub navigation e.g. http://twitter.github.com/bootstrap/scaffolding.html (when you scroll the subnav becomes fixed to that main navigation)



Has anyone implemented this or are there any tutorials?


More From » twitter-bootstrap

 Answers
67

Here is my code to implement this feature:



$(document).scroll(function(){
// If has not activated (has no attribute data-top
if (!$('.subnav').attr('data-top')) {
// If already fixed, then do nothing
if ($('.subnav').hasClass('subnav-fixed')) return;
// Remember top position
var offset = $('.subnav').offset()
$('.subnav').attr('data-top', offset.top);
}

if ($('.subnav').attr('data-top') - $('.subnav').outerHeight() <= $(this).scrollTop())
$('.subnav').addClass('subnav-fixed');
else
$('.subnav').removeClass('subnav-fixed');
});

[#87608] Sunday, February 5, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mari

Total Points: 305
Total Questions: 100
Total Answers: 98

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
mari questions
Mon, Feb 18, 19, 00:00, 5 Years ago
Wed, Jan 2, 19, 00:00, 5 Years ago
Tue, Dec 25, 18, 00:00, 6 Years ago
;