It says clearly on the documentation
just add data-spy="scroll" to the element you want to spy on (most typically this would be the body)
But it seems I can only get it to work if I put it on the body. When I put it in any other element that I wish to spy on, the last element of the nav gets selected.
Here it's on the body, and it works, and this is the same exact thing but with data-spy="scroll"
on the element I want to spy on, and it fails (only the last element gets activated).
Am I doing something wrong or is this a bug ?
It must be used on a Bootstrap nav component or list group . Scrollspy requires position: relative; on the element you’re spying on, usually the <body> . When spying on elements other than the <body>, be sure to have a height set and overflow-y: scroll; applied. Anchors ( <a>) are required and must point to an element with that id .
The Bootstrap scrollspy has basically two components — the target (e.g. nav or list group) and the scrollable area to spy on, which is often the <body> section. The data-bs-spy="scroll" attribute ( line no-01) is applied to the element you want to spy on, which is simply the <body> element in our case.
Default is 10 pixels. Requires relative positioning: The element with data-spy="scroll" requires the CSS position property, with a value of "relative" to work properly. In this example, we use Bootstrap's vertical navigation pills as menu: ... ...
The data-bs-spy="scroll" attribute ( line no-01) is applied to the element you want to spy on, which is simply the <body> element in our case.
Your 2nd example can be fixed by using:
#TryToPutDataSpyHere{
display:inline;
}
But for some reason it doesn't works on the demo
I managed to reproduce your issue from the doc: http://jsfiddle.net/baptme/KbphR/
But it only works if I had the following css code (used on the doc):
.scrollspy-example {
height: 200px;
overflow: auto;
position: relative;
}
http://jsfiddle.net/baptme/KbphR/1/
It seems like height: 200px;
and overflow: auto;
are both necessary
Not in you case maybe because of your .box{height: 500px;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With