我有一个简单的 Bootstrap 模态设置,在模态中我满足于 nav
和 scrollspy 设置。但是,它不起作用。我看到它已激活,但导航从未更新。
这里的完整源代码太长了,你看不到我得到的效果,所以我设置了一个 jsfiddle
我正在使用 data-spy="scroll"
方法来启用它。我做错了什么?为什么导航没有根据您的位置进行更新?
最佳答案
bootstrap scrollspy 仍然有一些错误(我真的很喜欢 bootsrap)。 我为此使用了一个自定义脚本,它工作得更好并且更灵活:
<script type="text/javascript">
$(document).ready(function(){
var lastId, topMenu = $(".menu-sidebar"),
topMenuHeight = topMenu.outerHeight() + 150,
menuItems = topMenu.find("a"),
scrollItems = menuItems.map(function() {
var item = $($(this).attr("href"));
if (item.length) {
return item;
}
});
menuItems.click(function(e) {
var href = $(this).attr("href"),
topMenuHeight = topMenu.outerHeight() + 20,
offsetTop = href === "#" ? 0 : $(href).offset().top - topMenuHeight + 1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});
$(window).scroll(function() {
var fromTop = $(this).scrollTop() + topMenuHeight;
var cur = scrollItems.map(function() {
if ($(this).offset().top < fromTop) return this;
});
cur = cur[cur.length - 1];
var id = cur && cur.length ? cur[0].id : "";
if (lastId !== id) {
lastId = id;
menuItems.parent().removeClass("active").end().filter("[href=#" + id + "]").parent().addClass("active");
}
});
});
</script>
关于javascript - 为什么 Bootstrap 的 scrollspy 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11909474/