Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating menu bar using HTML/CSS?

wondered if any one knew of a way of creating a floating menu bar that sticks to a point on a page until the browser window gets far enough down the page and unsticks it and then the menu bar begins to scroll along with it. The effect I want is the exact same as this http://www.jtricks.com/javascript/navigation/floating.html javascript menu. However, I really want to do this with CSS. I am aware I can make the div Absolutely positioned and it will move down the page, I tried making one DIV relative positioned (parent div) and then another div inside this which was absolute positioned, but I could not get this to work. Does any one know how to make this work with CSS or does it need to be JS?

Thanks in advance.

Jon.

like image 309
Jon Kyte Avatar asked Mar 15 '11 18:03

Jon Kyte


People also ask

How do you make a floating menu bar in CSS?

Also known as "fixed menus" and "hovering menus", floating menus stay in a fixed position when you scroll the page. They appear to "float" on top of the page as you scroll. Creating a floating menu is very simple and quite painless. The operative code is position:fixed .

What is a floating menu bar?

What is a Floating Navigation Menu? A floating navigation menu, sometimes called a sticky navigation menu, is a menu that stays visible on the page even as you scroll down. No matter where a visitor is on the page, they'll see your menu options either along the top or side of the page.

How do I show menu bar in HTML?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to make a Navigation Bar. Step 2: Now, we have to define the <nav> tag in the <body> tag where we want to make the bar.

How do you float a menu on the right?

Add float:right to the ul's . dropdown class which put your entire menu at right side. Add float:left to the li which helps your sub-menu to stay align. Show activity on this post.


3 Answers

I believe using javascript is the only solution to get the effect you described. Here's a quick demo of a banner that starts in a absolute position and goes to fixed when the user scrolls.

<div style="height:1000px;width:500px;">

    <div id="floatbar" style="background:gray;
                                width:200px;
                                height:40px;
                                position:absolute;
                                left:0;top:200px;">
    </div>
</div>

$(window).scroll(function(){
    if ($(window).scrollTop() >= 200)
    {
        $("#floatbar").css({position:'fixed',left:'0',top:'0'});
    }
    else
    {
        $("#floatbar").css({position:'absolute',left:'0',top:'200px'});
    }
});
like image 112
agradl Avatar answered Oct 17 '22 08:10

agradl


well if you do NOT need the animation, than just use
position: fixed;
in the css.

if you want it animated you need to use javascript. for example in jquery:

$(window).scroll(function(){
   $('#menu').css({
       right: 0,
       top: 0
   })
})
like image 23
Naftali Avatar answered Oct 17 '22 07:10

Naftali


Well you can't do it with absolute positioned div inside of a relative. Fixed position is basically an absolute positioned div, positioned relatively to the window. I'd say you definately need javascript here.

like image 28
Stanislav Ageev Avatar answered Oct 17 '22 07:10

Stanislav Ageev