I am using the Twitter Bootstrap Affix JS component. My UL list is affixing properly when I scroll the page down. Also - when I click on the individual list items (much like Twi
Yes, you need to also use the ScrollSpy plugin. You can activate it through markup or through JS. Letting #scroll-pane
be the element that triggers scroll events and #navbar
be the element containing the ul.nav
, the following should get it working:
JS
$('#scroll-pane').scrollspy({target: '#navbar'});
Use either the HTML or the JS, but not both.
Additional Info
When the ScrollSpy plugin is passed a target, like scrollspy({target: '#navbar'})
, this is used to construct a selector of the form
target + ' .nav li > a'
which, in this example would give
'#navbar .nav li > a'
It is important to understand the stipulations that this selector creates.
- The original
target
must be an element which contains an element with class nav
. So .nav
should probably never be your target.
- The
.nav
element must contain list items.
- Those list items must have a
as a direct child.
The
elements selected by this are then filtered out by those whose data-target
or href
begin with a '#'. These href
's are in turn used to select the elements contained in the element to which the ScrollSpy plugin is attached. Offsets of those selected are stored, and when a scroll occurs, a comparison of the current scroll offset is made with the stored values, updating the corresponding
element with the class active
.
Hopefully, this summary can aid in better understanding what one might be tripping over when attempting to implement the plugin, without having to dig into the code in further detail.
讨论(0)