如何使用Bootstrap滚动间谍?

问题描述 投票:26回答:8

我不能让滚动间谍与垂直导航一起正常工作。您可以在下面找到我使用的代码。出于某种原因,只有“两个”变得活跃。

谁知道出了什么问题呢?

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Twitter Bootstrap Scroll Spy Playground</title>
    <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="span3">
            <ul class="navbar nav nav-list affix">
              <li class="active"><a href="#one">One</a></li>
              <li><a href="#two">Two</a></li>
            </ul>
        </div>
        <div class="span1" data-spy="scroll">
            <section id="one">
            <h1>One</h1>
            <h2>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. </h2>
            </section>
            <section id="two">
            <h1>Two</h1>
           <h2>Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</h2>
            </section>   
        </div>
      </div>
    </div>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="bootstrap/js/bootstrap.js"></script>
</body>
</html>
twitter-bootstrap twitter-bootstrap-2
8个回答
36
投票

在为自己排序时,我发现被监视的元素需要有一个滚动条。

看看这个小提琴:http://jsfiddle.net/adamp/qgU6h/1/

您既可以直接监视body元素($('body').scrollspy()),也可以为内容指定明确的高度并强制它显示滚动条。

(如果你看一下Bootstrap documentation example,它会做固定高度的技巧。)


2
投票

你可以像http://jsfiddle.net/mCxqY/这样做

<body data-spy="scroll" data-target="#navbar">
    <div id="post1" class="box">
        <h1>Post 1</h1>
        <p> Scroll Down↓</p>
    </div>
    <div id="post2" class="box"><h1>Post 2</h1></div>
    <div id="post3" class="box"><h1>Post 3</h1></div>

    <div id="navbar">
        <ul class="nav">
            <li><a href="#post1">Post 1</a></li>
            <li><a href="#post2">Post 2</a></li>
            <li><a href="#post3">Post 3</a></li>
        </ul>
    </div>

    <script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-scrollspy.js"></script>
</body>​

CSS

@import url(http://twitter.github.com/bootstrap/assets/css/bootstrap.css);
.box{
    margin: 20px; padding: 15px;
    background: #eee;
    height: 500px;
}
#navbar{
    position: fixed;
    bottom: 0; left: 20px;
    width: 100%;
    background: #fff;
}
.nav li a{
    float: left;
    width: 80px;
    padding: 15px 0;
}
.nav li a:hover{
    color: #f33 !important;
    background: none;
}
.nav li.active a{
    color: #f55;
    text-decoration: underline;
}


2
投票

谢谢杰瑞克,我能够借助它来制作我的作品

height: 100%;

在我的CSS的身体选择器

也是Bootstrap http://getbootstrap.com/javascript/#scrollspy的推荐

我也在体内添加了position: relative;

我的CSS中的正文选择器的开头是这样的:

body {
    position: relative;
    height: 100%;
    /* … */
}

在HTML中的<body>我不得不删除

data-offset="0"

所以它现在就像这样

<body data-spy="scroll" data-target="#navigation">

1
投票

我必须做两件事才能让scrolllspy为我工作。

首先,我必须给<body>元素一个高度:

<style>

  .body
  {
    height: 100%;
  }

</style>    

<body data-spy="scroll" data-target="#scrollElement" data-offset="0">

我认为这是因为scroll-spy试图获取你正在监视的元素的高度,但是如果没有为你的元素指定高度,它会传递一个null(或0?)值,似乎打破了剧本的计算。

其次,我必须确保我的id元素之间有足够的垂直空间(导航锚点指向)。

我无法弄清楚所需的确切空间量(它确实根据数据偏移属性而变化,所以它可能与此有关),但我认为如果你有一个高度设置为你的间谍元素和scrollspy仍然不起作用 - 只需在id元素之间添加更多内容。


0
投票
$(window).scroll(function() {
    if ($(".navbar").offset().top > 50) {
        $(".navbar-fixed-top").addClass("top-nav-collapse");
    } else {
        $(".navbar-fixed-top").removeClass("top-nav-collapse");
    }
});

//jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1500, 'easeInOutExpo');
        event.preventDefault();
    });
});

0
投票

我是这样做的。对于数据目标,我使用类名.navbar

      <body data-spy="scroll" data-target=".navbar" data-offset="50">
       <nav class="navbar navbar-default navbar-fixed-top" >

0
投票
.nameClasse {
  position: relative;
  overflow: auto;
  height: 330px;
}

<div data-spy="scroll" data-target="#navbarVertical" data-offset="0" class="nameClass">

0
投票

经过数小时的挫折和研究,​​我发现许多人对Bootstrap Affix的文档(www.mtjhax.wordpress.com)也不满意。毕竟环顾四周,Bootply上的这个解决方案对我有用:http://bootply.com/63937

假设您的导航具有ID“navbar”,正文应如下所示:

<body data-spy="scroll" data-target="#navbar">
    ...
</body>

导航栏本身应如下:

<div class="navbar" id="navbar">
    <ul class="nav navbar-nav">
        <li>...</li>
        <li>...</li>
        <li>...</li>
    </ul>               
</div>

然后你初始化了插件......

$('#nav').affix({
    offset: {top: 0}
}); 

...并给导航正确的样式:

#nav.affix {
    position: fixed;
    top: 0;
    width: 100%
}
© www.soinside.com 2019 - 2024. All rights reserved.