Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use css bootstrap list-group with affix to create a sticky menu in a column?

I am trying to create a sticky menu using CSS Bootstrap affix and list-group menu.

I manage to get most of it to work except for when the user scrolls down.

When the user scrolls down, the menu seems to take the entire with of the page.

I tried to set it up via data attributes

using something like this

<div class="container">
    <div class="row">
        <div class="col-md-3" id="leftCol">

            <div data-spy="affix">

                <div class="list-group list-group-root well">
                    <a class="list-group-item" href="#introduction">Introduction</a>
                    <a class="list-group-item" href="#features">Features</a>
                    <a class="list-group-item" href="#dependencies">Dependencies</a>
                </div>

            </div>

        </div>
        <div class="col-md-9" id="mainCol">

            Some long text for the body along with some tables.

        </div>
    </div>
</div>

But the data attribute did not make the menu stick! it just kept it on the top.

So I tried to use JS to get the job done like this

$(function(){

    $('#leftCol').affix({
      offset: {
        top: 100,
        bottom: function () {
          return (this.bottom = $('.footer').outerHeight(true))
        }
      }
    });


});

I created jsFiddle to show you the current behavior.

How can I fix this affix so when the user scrolls down the menu maintain the same shape?

like image 353
Junior Avatar asked Dec 29 '16 01:12

Junior


People also ask

How do I create a sticky header in bootstrap?

Sticky Header is nothing but navigation bar, if we want to make navigation bar becomes stick at the top (sticky header) then use offsetTop within the JavaScript file. Include bootstrap feature in our application we must specify some pre-defined libraries inside our application.

Can you put a list group within Bootstrap panel?

Include list groups within any panel. Create a panel by adding class . panel to the <div> element.

What is the use of affix plugin in bootstrap?

The Affix plugin allows an element to become affixed (locked) to an area on the page. This is often used with navigation menus or social icon buttons, to make them "stick" at a specific area while scrolling up and down the page.

How do I use sticky tops in bootstrap?

Sticky top Position an element at the top of the viewport, from edge to edge, but only after you scroll past it. The .sticky-top utility uses CSS's position: sticky , which isn't fully supported in all browsers. IE11 and IE10 will render position: sticky as position: relative .


1 Answers

First of all, you should use either data-attributes or JS.

I updated your jsFiddle. The position of id="leftCol" was changed:

<div class="col-md-3" >

        <div id="leftCol">

              ...

        </div>

    </div>

and style was added:

#leftCol {
   width: 220px;
}

Also, you should add media queries to remove affix from mobile view.

like image 107
Evgeny Kuznetsov Avatar answered Oct 09 '22 00:10

Evgeny Kuznetsov