加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

twitter-bootstrap – 如何在Bootstrap中设置ScrollSpy的偏移量

发布时间:2020-12-17 23:37:05 所属栏目:安全 来源:网络整理
导读:我有一个网站的navbar固定在顶部和3个divs在主要内容区域下面。 我试图使用scrollspy从引导框架。 当滚动经过div时,我成功地突出显示菜单中的不同标题。 我也有它,所以当你点击菜单,它会滚动到页面的正确部分。但是,偏移是不正确的(它不考虑导航栏,所以
我有一个网站的navbar固定在顶部和3个divs在主要内容区域下面。

我试图使用scrollspy从引导框架。

当滚动经过div时,我成功地突出显示菜单中的不同标题。

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

我看到在Bootstrap page它提到一个偏移选项,但我不知道如何使用它。

我也不是什么意思,当它说你可以使用scrollspy $(‘#navbar’)。scrollspy(),我不知道在哪里包括所以我没有,一切似乎都工作(除了偏移)。

我认为偏移量可能是body-tag的数据偏移= ’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>

解决方法

Bootstrap使用偏移来仅解析窥探,而不是滚动。这意味着滚动到正确的地方取决于你。

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

var offset = 80;

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读