twitter-bootstrap Bootstrap - Affix & scrollspy - 在滚动不工作时处于活动状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15430020/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 16:39:08  来源:igfitidea点击:

Bootstrap - Affix & scrollspy - active on scroll not working

twitter-bootstrap

提问by Valentin Despa

I have the following jsfiddle

我有以下jsfiddle

I can't get the:

我无法得到:

  • navspy to properly work
  • clicking an menu item should also mark it as active
  • the last element in the list get's selected with no apparent reason.
  • navspy 正常工作
  • 单击菜单项也应将其标记为活动
  • 列表中的最后一个元素被无缘无故地选中。

I am stuck on this. Anybody willing to enlighten me what I did wrong?

我坚持这一点。有人愿意启发我我做错了什么吗?

回答by Zim

You need to add 'scrollspy' to the body tag in order for it to activate the menu items.

您需要将 'scrollspy' 添加到 body 标签中,以便它激活菜单项。

<body data-spy="scroll" data-target=".navspy">

http://jsfiddle.net/skelly/yPgUu/253/

http://jsfiddle.net/skelly/yPgUu/253/

回答by David Taiaroa

Picking up on the heavy lifting by @Skelly, here's a variation: http://jsfiddle.net/panchroma/TcyBY/

接受@Skelly 的繁重工作,这里有一个变体:http: //jsfiddle.net/panchroma/TcyBY/

I've moved the scrollspy call out of the body tag to a separate js

我已经将 scrollspy 调用从 body 标签移到了一个单独的 js

$('body').scrollspy({ target: '.sidebar-nav' }); 

The documentation on the Bootstrap site for scrollspy could probably be clearer. Here's a link which might help explain some of the blips: http://mtjhax.wordpress.com/2013/02/11/twitter-bootstraps-scrollspy-plugin-needs-better-docs/

滚动间谍 Bootstrap 站点上的文档可能会更清楚。这是一个可能有助于解释一些问题的链接:http: //mtjhax.wordpress.com/2013/02/11/twitter-bootstraps-scrollspy-plugin-needs-better-docs/

Hope this helps!

希望这可以帮助!