/    Sign up×
Community /Pin to ProfileBookmark

Floating menu

Anyone have code for a drop down menu that floats so when
you scroll down the page the menu stays at the top of the screen?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JonaFeb 04.2005 — [font=trebuchet ms]You can do this purely with CSS. However, because Internet Explorer does not understand CSS 2, JavaScript must be employed for it. Try the following.[/font]

[b]The Script. Goes in HEAD tags.[/b]
<i>
</i>&lt;script type="text/javascript"&gt;&lt;!--
function floatMenu(){
if(!document.getElementById) return;
var menu = document.getElementById('menu');
menu.style.top = ((!document.all)?(window.pageYOffset):(document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop)+"px";
}
window.onscroll = floatMenu;
//--&gt;&lt;/script&gt;
&lt;style type="text/css"&gt;&lt;!--
#menu {
position:fixed;
top: 0px;
left:0px;
}
--&gt;&lt;/style&gt;


[b]The HTML. Goes in BODY tags.[/b]
<i>
</i>&lt;div id="menu"&gt;
&lt;ul&gt;
&lt;li&gt;Menu item&lt;/li&gt;
&lt;li&gt;Menu item&lt;/li&gt;
&lt;li&gt;Menu item&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
[font=trebuchet ms][/font]
Copy linkTweet thisAlerts:
@ranosbauthorFeb 08.2005 — That script only puts 3 txt lines on a page...
Copy linkTweet thisAlerts:
@JPnycFeb 08.2005 — What he gave you there was the css for making the menu fixed, and the script which corrects IE's CSS shortcomings. HE didn't give you the menu code, but that you can get all over the web.
Copy linkTweet thisAlerts:
@DonnaLPDec 31.2005 — I tried the code myself. It works, but my problem is that I need the menu to stay below the Navagation menu. Right now when you go to the page the menu is in a "fixed" spot. But then it jumps to the top of the page.

How can I get it to stay were I want it to then scroll down/up with the page?

http://www.johnstoncountyarts.org/Gallery_testingmenu.html

I have tried different code from different js forums and tried to change the code that is there now, but I'm missing something. But I believe it is the "menu.style.top" that I need to change, but I don't know how.

Thanks for the code and the help!

Donna
Copy linkTweet thisAlerts:
@JonaJan 01.2006 — <i>
</i>&lt;script type="text/javascript"&gt;&lt;!--
function floatMenu(){
if(!document.getElementById) return;
var menu = document.getElementById('menu');
var distY = ((!document.all)?(window.pageYOffset):(document.documentElement)?document.documentElement.scrollTop: document.body.scrollTop);
if(distY &lt; 600){ // 600 would be the height of your header area, you may need to change this
distY = 600; // this is also the height of your header and will keep the menu here instead of at 0
}
menu.style.top = distY+"px";
}
window.onscroll = floatMenu;
//--&gt;&lt;/script&gt;
&lt;style type="text/css"&gt;&lt;!--
#menu {
position:fixed;
top: 0px;
left:0px;
}
--&gt;&lt;/style&gt;
×

Success!

Help @ranosb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...