6

我已经实现了一个自动浮动子导航栏,直接取自 Bootstrap文档 CSS 和 JS,在我正在处理的站点中。它只出现在一个视图上,但这是一个 Rails 视图,因此它是根据加载的对象动态生成的。

我发现,当出现在子导航栏下方的内容足够长时,子导航栏的行为会按预期工作 - 在子导航栏滚动出视图的那一刻添加 subnav-fixed 类.

但是,如果页面比这短,则子导航栏将在实际看不见之前固定,这会产生非常不和谐的跳跃,更不用说您可以看到栏曾经所在的空间,这是您不应该看到的能够看到。

我应该补充一点,我正在使用固定(主)导航栏,并考虑了适当的主体填充。

似乎问题出$('.subnav').offset.top在返回的值上。

谁能更好地使用 jQuery / JS 可以帮助诊断这个问题,并想出一种方法来使 subnav 栏只有在它被滚动出视图时才变得固定?

Javascript:

var $win = $(window)
  , $nav = $('.subnav')
  , navTop = $('.subnav').length && $('.subnav').offset().top
  , isFixed = 0
    , $hiddenName = $('.subnav .hide')

processScroll()

$nav.on('click', function () {
  if (!isFixed) setTimeout(function () {  $win.scrollTop($win.scrollTop() - 47) }, 10)
})

$win.on('scroll', processScroll)

function processScroll() {
  var i, scrollTop = $win.scrollTop()
  if (scrollTop >= navTop && !isFixed) {
    isFixed = 1
    $nav.addClass('subnav-fixed')
        $hiddenName.removeClass('hide')
        if (!$('.subnav li').hasClass('active')) {
            $('.subnav li:eq(1)').addClass('active')
        }
  } else if (scrollTop <= navTop && isFixed) {
    isFixed = 0
    $nav.removeClass('subnav-fixed')
        $hiddenName.addClass('hide')
        $('.subnav li').removeClass('active')
  }
}

Replicating Bootstraps main nav and subnav - 我见过这个问题,但我认为它不能避免这个问题,因为它仍然使用.offset()

更新:

仍然没有运气。navTop随着页面长度变短,似乎仍然变短,这没有任何意义,因为它使用offset().top. 有什么我没有得到的关于该方法如何工作的东西吗?

更新 2

看起来这可能会在 Bootstrap 2.1.0 中得到解决,因为它实际上将包含一个子导航栏作为一个真正的组件,以及一个用于处理粘性行为的 jQuery 插件。不过,我想了解为什么 offset() 函数如此不可靠。

4

1 回答 1

2

我也采用了相同的 JS 代码,并将其改编为我的网站(仍在建设中)。这是一些代码(工作)

HTML

<div class="fix_on_top">
    <div class="container">
        <ul class="breadcrumb">
            <li><a href="#">Home</a> <span class="divider">/</span></li>
            <li><a href="#">Packages</a> <span class="divider">/</span></li>
            <li class="active">Professional courses - I am a breadcrumb; I'll fix myself below the top navbar when you scroll the page</li>
        </ul>
    </div>
</div>

少(CSS)

.fixed_on_top {
    left: 0;
    right: 0;
    z-index: 1020;
    position: fixed;
    top:$navbarHeight;
}

JavaScript

var $window     = $(window),
    $fix_on_top = $('.fix_on_top'),
    fixed_Top   = $('.fix_on_top').length && $('.fix_on_top').offset().top - 40,
    isFixed     = 0;

process_scroll();

// hack sad times - holdover until rewrite for 2.1
$fix_on_top.on('click', function () {
    if (!isFixed) setTimeout(function () {
        $window.scrollTop($window.scrollTop() - 47)
    }, 10);
});

$window.on('scroll', process_scroll);

function process_scroll()
{
    var i, scrollTop    = $window.scrollTop();

    if (scrollTop >= fixed_Top && !isFixed) {
        isFixed         = 1;
        $fix_on_top.addClass('fixed_on_top');
    }
    else if (scrollTop <= fixed_Top && isFixed)
    {
        isFixed         = 0;
        $fix_on_top.removeClass('fixed_on_top');
    }
}

我还改编了适用于 THEAD 的原始代码(用于另一个网站)(表格 - 我每个网页都有一个表格,id =“tablesortable”)

// Automatically add the class "fix_on_scroll" on #tablesortable's thead
if ($('#tablesortable thead').length)
{
    var thead_cells = new Array();
    $('#tablesortable thead').addClass('fix_on_scroll');
    // Get the width of each cells in thead
    $('#tablesortable thead').find('td, th').each(function(i, v) {
        thead_cells.push($(this).width());
    });
    // Keep same with in tbody and tfoot
    $('#tablesortable tbody tr:first').children('td, th').each(function(i, v) {
        $(this).width(thead_cells[i]);
    });
    $('#tablesortable tfoot tr:first').children('td, th').each(function(i, v) {
        $(this).width(thead_cells[i]);
    });
}

// Fix all elements (with class .fix_on_scroll) just below the top menu on scroll
// (Modified version from homepage of Twitter's Bootstrap - js/application.js)
var $window     = $(window),
$fix_on_scroll  = $('.fix_on_scroll'),
fixed_elem_top  = $('.fix_on_scroll').length && $('.fix_on_scroll').offset().top - 26,
isFixed     = 0;

process_scroll();

// hack sad times - holdover until rewrite for 2.1
$fix_on_scroll.on('click', function () {
    if (!isFixed) setTimeout(function () {$window.scrollTop($window.scrollTop() - 40)}, 10)
});

$window.on('scroll', process_scroll);

function process_scroll()
{
    var i, scrollTop = $window.scrollTop();
    if (scrollTop >= fixed_elem_top && !isFixed)
    {
        isFixed = 1;
        $fix_on_scroll.addClass('fixed_on_scroll');

        // Keep original width of td/th
        if ($fix_on_scroll.is('thead'))
        {
            $fix_on_scroll.find('td, th').each(function(i, v) {
                $(this).width(thead_cells[i]);
            });
        }
    }
    else if (scrollTop <= fixed_elem_top && isFixed)
    {
        isFixed = 0
        $fix_on_scroll.removeClass('fixed_on_scroll')
    }
}

我的(改编的)代码正在工作。你可以使用它们。

于 2012-08-18T16:38:23.183 回答