javascript - 为什么 Bootstrap 的 scrollspy 不起作用?

标签 javascript twitter-bootstrap

我有一个简单的 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>

来源:http://jsfiddle.net/mekwall/up4nu/

关于javascript - 为什么 Bootstrap 的 scrollspy 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11909474/

相关文章:

javascript - 通过在 JavaScript 中连接 2 个字符串来设置 'calc()' 值不起作用

javascript - 强制 Canvas 更新

Javascript 对象在其声明内实例化

html - Bootstrap 侧边栏主题

html - Bootstrap在同一个页面使用多个容器是否合法

JavaScript For 循环后门?

javascript - Angular 文件上传更新范围 onCompleteItem

javascript - 启动弹出按钮脚本

css - 为什么我的表格不会在我的网站上使用 Bootstrap 与页面中心对齐,但在 codepen 上可以吗?

html - 导航栏在移动设备上的 Bootstrap 中无法正确显示