wis*_*sew 6 javascript css jquery ruby-on-rails twitter-bootstrap
我已经在我正在研究的网站上实现了一个自动浮动的子空栏,直接来自Bootstrap docs CSS 和JS.它只出现在一个视图上,但这是一个Rails视图,因此它是根据加载的对象动态生成的.
我发现,当subnav栏下面显示的内容足够长时,subnav栏的行为按预期工作 - 当subnav栏滚出视图时,就会添加subnav-fixed类.
如果页面比这个短,那么subnav bar会在它实际看不到之前变得固定,这会产生一个非常不稳定的跳跃,更不用说你可以看到这个柱子曾经的空间,你不应该能够看到.
我应该补充一点,我使用的是固定(主)导航栏,并考虑了适当的主体填充.
似乎问题$('.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')
}
}
Run Code Online (Sandbox Code Playgroud)
复制Bootstraps主导航和subnav - 我已经看到了这个问题,但我不认为它避免了这个问题,因为它仍然使用.offset()
更新:
仍然没有运气.navTop随着页面长度变短,似乎仍然会缩短,因为它正在使用,所以没有任何意义offset().top.有什么关于这种方法是如何工作的,我没有得到?
更新2
看起来这可能会被Bootstrap 2.1.0解决,因为它实际上包含一个subnav bar作为一个真正的组件,以及一个用于处理粘滞行为的jQuery插件.不过,我想了解为什么offset()函数如此不可靠.
小智 2
我还采用了相同的 JS 代码,并将其改编到我的网站(仍在建设中)。这是一些代码(工作)
<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>
Run Code Online (Sandbox Code Playgroud)
.fixed_on_top {
left: 0;
right: 0;
z-index: 1020;
position: fixed;
top:$navbarHeight;
}
Run Code Online (Sandbox Code Playgroud)
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');
}
}
Run Code Online (Sandbox Code Playgroud)
我还改编了适用于 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')
}
}
Run Code Online (Sandbox Code Playgroud)
我的(改编后的)代码正在运行。你可以使用它们。