Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Twitter Bootstrap's ScrollSpy, where exactly can I put data-spy="scroll"?

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 ?

like image 479
João Pinto Jerónimo Avatar asked Jul 25 '12 21:07

João Pinto Jerónimo


People also ask

How do I use scrollspy in Bootstrap?

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 .

How do I spy on a page in Bootstrap?

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.

What is the default size of data-spy="scroll'' in Bootstrap?

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: ... ...

How do I spy on the scroll of an element?

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.


1 Answers

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;}

like image 91
baptme Avatar answered Sep 29 '22 03:09

baptme