Replicating Bootstraps main nav and subnav

前端 未结 5 1538
暗喜
暗喜 2020-12-07 08:29

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

5条回答
  •  广开言路
    2020-12-07 08:58

    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');
    });
    

提交回复
热议问题