twitter-bootstrap 如何使用 Bootstrap 滚动间谍?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13134013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Bootstrap scroll spy?
提问by user1780429
I cannot quite get the scroll spy to work properly with a vertical nav. Below you can find the code I use. For some reason, only "Two" gets active.
我无法让滚动间谍与垂直导航正常工作。您可以在下面找到我使用的代码。出于某种原因,只有“两个”处于活动状态。
Anyone has an idea as of what is wrong?
任何人都知道出了什么问题?
<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>
回答by adam-p
In sorting this out for myself, I've found that the element being spied upon needs to have a scroll bar.
在为自己解决这个问题时,我发现被监视的元素需要有一个滚动条。
Take a look at this Fiddle: http://jsfiddle.net/adamp/qgU6h/1/
看看这个小提琴:http: //jsfiddle.net/adamp/qgU6h/1/
You can either spy directly on the body element ($('body').scrollspy()) or give your content an explicit height and force it to show a scrollbar.
您可以直接监视 body 元素 ( $('body').scrollspy()) 或为您的内容指定一个明确的高度并强制它显示滚动条。
(If you look at the Bootstrap documentation example, it does the fixed-height trick.)
(如果您查看 Bootstrap文档示例,它会执行固定高度的技巧。)
回答by NullPoiиteя
you can do this like http://jsfiddle.net/mCxqY/
你可以这样做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
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;
}
?
?
回答by Daniel G Pare
Thanks Jerreck I was able to make mine works with the help of
感谢 Jerreck,我能够在以下人员的帮助下完成我的作品
height: 100%;
in the bodyselector of my CSS
在我的 CSS的正文选择器中
Also as recommended by the guys at Bootstrap http://getbootstrap.com/javascript/#scrollspy
也按照 Bootstrap http://getbootstrap.com/javascript/#scrollspy的人的推荐
I've added position: relative;in the body too.
我也在position: relative;正文中添加了。
The beginning of the body selector in my CSS goes like this:
我的 CSS 中 body 选择器的开头是这样的:
body {
position: relative;
height: 100%;
/* … */
}
In the <body>in HTML I had to remove
在<body>HTML 中,我必须删除
data-offset="0"
so it is now like this
所以现在是这样的
<body data-spy="scroll" data-target="#navigation">
回答by Jerreck
I had to do two things to get scrollspy to work for me.
我必须做两件事才能让 scrollspy 对我来说有效。
First, I had to give a height to the <body>element:
首先,我必须给<body>元素一个高度:
<style>
.body
{
height: 100%;
}
</style>
<body data-spy="scroll" data-target="#scrollElement" data-offset="0">
I thinkthis is because scroll-spy is trying to get the height of the element that you're spying on, but if there isn't a height specified for your element, it gets passed a null (or 0?) value, which seems to be breaking the script's calculations.
我认为这是因为 scroll-spy 试图获取您正在监视的元素的高度,但是如果没有为您的元素指定高度,它会传递一个 null(或 0?)值,其中似乎正在破坏脚本的计算。
Second, I had to make sure that there was enough vertical space between my id elements (that the nav anchors are pointing towards).
其次,我必须确保我的 id 元素之间有足够的垂直空间(导航锚指向的)。
I couldn't figure out the exact amount of space necessary (it did vary depending on the data-offset attribute, so it probably has something to do with that), but I reckon that if you have a height set to your spy element and scrollspy still isn't working - just add more content between your id elements.
我无法弄清楚所需的确切空间量(它确实因数据偏移属性而异,所以它可能与此有关),但我认为如果您为 spy 元素设置了高度并且scrollspy 仍然无法正常工作 - 只需在您的 id 元素之间添加更多内容。
回答by Manikandan Jyothimani
$(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();
});
});
回答by Ayush Chaurasia
I did it like this. for data target I use the class name .navbar
我是这样做的。对于数据目标,我使用类名.navbar
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<nav class="navbar navbar-default navbar-fixed-top" >
回答by expedito freitas
.nameClasse {
position: relative;
overflow: auto;
height: 330px;
}
<div data-spy="scroll" data-target="#navbarVertical" data-offset="0" class="nameClass">
回答by radu.luchian
After hours of frustration and research, I came to find that many people are also not pleased with Bootstrap Affix's documentation (www.mtjhax.wordpress.com). After all the looking around though, this solution on Bootply worked for me: http://bootply.com/63937
经过数小时的挫折和研究,我发现很多人对 Bootstrap Affix 的文档(www.mtjhax.wordpress.com)也不满意。经过所有环顾四周,Bootply 上的这个解决方案对我有用:http://bootply.com/63937
Say your navigation had the id "navbar", the body should look like this:
假设您的导航的 id 为“navbar”,正文应如下所示:
<body data-spy="scroll" data-target="#navbar">
...
</body>
And the navbar itself should be as follows:
导航栏本身应如下所示:
<div class="navbar" id="navbar">
<ul class="nav navbar-nav">
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
You then initialized the plugin...
然后你初始化了插件......
$('#nav').affix({
offset: {top: 0}
});
... and give the navigation proper styling:
...并为导航提供适当的样式:
#nav.affix {
position: fixed;
top: 0;
width: 100%
}

