如何在Bootstrap中设置ScrollSpy的偏移量?

Lan*_*ngo 95 twitter-bootstrap

我有一个网站,导航栏固定在顶部,主要内容区域下面有3个div.

我正在尝试从bootstrap框架中使用scrollspy.

当你滚动浏览div时,我已成功突出显示菜单中的不同标题.

我也有它,所以当你点击菜单时,它会滚动到页面的正确部分.但是,偏移是不正确的(它不考虑导航栏,所以我需要偏移约40像素)

我在Bootstrap页面上看到它提到了一个偏移选项,但我不确定如何使用它.

当它说你可以使用scrollspy时$('#navbar').scrollspy(),我也不是它的意思,我不知道在哪里包含它所以我没有,一切似乎都在工作(除了偏移).

我认为偏移可能是data-offset='10'身体标签,但它对我没有任何作用.

我觉得这是非常明显的事情,我只是错过了它.有帮助吗?

我的代码是

...
<!-- note: the data-offset doesn't do anything for me -->
<body data-spy="scroll" data-offset="20">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
    <div class="container">
    <a class="brand" href="#">VIPS</a>
    <ul class="nav">
        <li class="active">
             <a href="#trafficContainer">Traffic</a>
        </li>
        <li class="">
        <a href="#responseContainer">Response Times</a>
        </li>
        <li class="">
        <a href="#cpuContainer">CPU</a>
        </li>
      </ul>
    </div>
</div>
</div>
<div class="container">
<div class="row">
    <div class="span12">
    <div id="trafficContainer" class="graph" style="position: relative;">
    <!-- graph goes here -->
    </div>
    </div>
</div>
<div class="row">
    <div class="span12">
    <div id="responseContainer" class="graph" style="position: relative;">
    <!-- graph goes here -->
    </div>
    </div>
</div>
<div class="row">
    <div class="span12">
    <div id="cpuContainer" class="graph" style="position: relative;">
    <!-- graph goes here -->
    </div>
    </div>
</div>
</div>

...
<script src="assets/js/jquery-1.7.1.min.js"></script>
<script src="assets/js/bootstrap-scrollspy.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Kur*_*UXD 109

Bootstrap使用偏移来仅解决间谍,而不是滚动.这意味着滚动到适当的位置取决于您.

试试这个,它适用于我:为导航点击添加一个事件处理程序.

var offset = 80;

$('.navbar li a').click(function(event) {
    event.preventDefault();
    $($(this).attr('href'))[0].scrollIntoView();
    scrollBy(0, -offset);
});
Run Code Online (Sandbox Code Playgroud)

我在这里找到了它:https://github.com/twitter/bootstrap/issues/3316

  • 要适当地更新URL的哈希部分,请将`window.location.hash = $(this).attr('href')`添加到此函数中. (8认同)
  • 动态获胜:`var navOffset = $('#navbar').height();`和`scrollBy(0,-navOffset);` (4认同)
  • 谢谢你的回答.我花了很长时间才意识到偏移不会影响滚动.这意味着任何使用固定顶部的人都需要以这种方式手动调整. (3认同)
  • 谢谢!FWIW,将数据偏移放在体内也很重要,因为提问者说它没有做任何事情,但它需要适当的间谍活动.有一个全面的答案可能会有所帮助. (2认同)

acj*_*jay 74

蒂姆提到的诀窍是利用填充.所以问题是,您的浏览器总是希望将锚点滚动到窗口的正确顶部.如果您将锚点设置为文本实际开始的位置,它将被菜单栏遮挡.相反,您所做的是将锚点设置为容器<section><div>标签的id,导航/导航栏将自动使用该ID.然后,你必须设置你padding-top的容器偏移量你想要的,而margin-top对容器的相反padding-top.现在您的容器块和锚点从页面的正确顶部开始,但内部的内容直到菜单栏下方才开始.

如果你使用常规锚点,你可以通过在容器中使用负margin-top来完成同样的事情,但是没有填充.然后将常规<a target="...">锚点作为容器的第一个子节点.然后使用相反但相等的方式设置第二个子元素的样式margin-top,但使用选择器.container:first-child +.

这一切都假定您的<body>标签已经将其边距设置为在标题下方开始,这是常态(否则页面会立即使用遮挡文本进行渲染).

以下是这个实例的一个例子.您可以通过将#the-elements-id添加到网址末尾来链接带有ID的网页上的任何内容.如果你把所有你的h标签ad dt标签与ids链接(像github一样)与某种类型的脚本注入一个小的链接到它们的左边 ; 将以下内容添加到CSS中将为您处理导航栏偏移:

body {
    margin-top: 40px;/* make room for the nav bar */
}

/* make room for the nav bar */
h1[id],
h2[id],
h3[id],
h4[id],
h5[id],
h6[id],
dt[id]{
    padding-top: 60px;
    margin-top: -40px;
}
Run Code Online (Sandbox Code Playgroud)

  • 喜欢填充顶部,负边距底部(在同一元素上)组合.更轻松然后搞乱js. (9认同)
  • 这是非常好的答案.还要注意设置数据偏移量. (2认同)

cog*_*ell 10

你可以随意更改scrollspy的偏移(假设你监视身体):

offsetValue = 40;
$('body').data().scrollspy.options.offset = offsetValue;
// force scrollspy to recalculate the offsets to your targets
$('body').data().scrollspy.process();
Run Code Online (Sandbox Code Playgroud)

如果您需要动态更改偏移值,这也适用.(我喜欢在下一部分大约在窗口的一半时切换的scrollspy,所以我需要在窗口调整大小时更改偏移量.)

  • 谢谢你 - 对Bootstrap 3.0 $('body')进行一点调整后效果很好.data()['bs.scrollspy'].options.offset = newOffset; //设置新的偏移量$('body').data()['bs.scrollspy'].process(); //强制scrollspy重新计算目标的偏移量$('body').scrollspy('refresh'); //刷新scrollspy. (2认同)

小智 8

如果你想要一个10的偏移量,你会把它放在脑中:

<script>
  $('#navbar').scrollspy({
    offset: 10
  });
</script>
Run Code Online (Sandbox Code Playgroud)

你的身体标签看起来像这样:

<body data-spy="scroll">
Run Code Online (Sandbox Code Playgroud)

  • 这不会抵消内容,它会抵消导航. (6认同)

Wil*_*hes 5

引导问题#3316开始

[this]仅用于修改滚动计算-我们不会移动实际的锚点

因此,设置offset仅会影响scrollspy认为页面已滚动到的位置。它不影响滚动,当你点击一个锚标记。

使用固定的导航栏意味着浏览器滚动到错误的位置。您可以使用JavaScript修复此问题:

var navOffset = $('.navbar').height();

$('.navbar li a').click(function(event) {
    var href = $(this).attr('href');

    // Don't let the browser scroll, but still update the current address
    // in the browser.
    event.preventDefault();
    window.location.hash = href;

    // Explicitly scroll to where the browser thinks the element
    // is, but apply the offset.
    $(href)[0].scrollIntoView();
    window.scrollBy(0, -navOffset);
});
Run Code Online (Sandbox Code Playgroud)

但是,为了确保scrollspy突出显示正确的当前元素,您仍然需要设置偏移量:

<body class="container" data-spy="scroll" data-target=".navbar" data-offset="70">
Run Code Online (Sandbox Code Playgroud)

这是有关JSFiddle的完整演示


另外,您也可以按照CSS技巧的建议将其填充到锚标签上。

a[id]:before { 
  display: block; 
  content: " ";
  // Use the browser inspector to find out the correct value here.
  margin-top: -285px; 
  height: 285px; 
  visibility: hidden; 
}
Run Code Online (Sandbox Code Playgroud)