31
May

This sliding hover effect script is an easy method to add some flavor to your navigation. Using the CSS you can easily customize the navigation to fit your “look and feel”. The markup for the script is very simple as below…

<div class="menu">
<ul id="menu">
<li><a href="#">JavaScript</a></li>
<li><a href="#">Graphic Design</a></li>
<li><a href="#">HTML</a></li>
<li value="1"><a href="#">User Interface</a></li>
<li><a href="#">CSS</a></li>
</ul>
<div id="slide"></div>
</div>

In the above code the default menu selection is identified by the value=”1″ attribute on the appropriate list item. To initialize the script simply call the function as below…

menuSlider.init('menu','slide')

The first variable is the id of the unordered list and the second is the id of the slider div. This script has been tested in IE6, IE7, FireFox, Opera and Safari. Please send any bug reports to michael@leigeber.com. This code is free to use for any personal or commercial projects.

Click here to download the script.



1 Digg Spread This

71 Responses


Awesome! Than you so much.

Abhisek on 31 May 2008 at 11:05 pm

Awesome! Realle, really awesome. Good examples of JavaScript Power, thanks!

Rolando Murillo on 31 May 2008 at 11:32 pm

[...] Menú con resalte deslizante. [...]

JavaScript sin frameworks / acualicio.us on 01 Jun 2008 at 1:38 am

Fantastic. I have been making a real mess of a mootools version of this lava lamp effect. This looks much much simpler.
Thank you very much.

Neil on 01 Jun 2008 at 2:03 am

if you change :

g=document.getElementById(k);

by :

g=document.createElement(’div’);
g.setAttribute(’id’,k);
m.parentNode.appendChild(g);

you can remove <div id=”slide”></div> in html

thanks for all your work

christophe on 01 Jun 2008 at 2:12 am

[...] : Quelques liens Menus : http://www.leigeber.com/2008/05/sliding-javascript-menu-highlight-1kb/ Be the first to rate this postCurrently 0/5 Stars.12345 Posted on juin 1, 2008 10:44 by Tech .NET [...]

Javascript : Quelques liens on 01 Jun 2008 at 2:45 am

[...] Красивое слайдинговое JavaScript меню можно от веб девелопера Laigber. Примеры здесь и здесь. Архив с меню здесь. Первоисточник здесь. [...]

Блокнот » Blog Archive » Слайдинговое JavaScript Меню on 01 Jun 2008 at 3:09 am

[...] bu sefer de 1 KB’lık kayan menu yapımı yayınlanmış. Kodu download etmeden önce ilgili yazıya bakarsanız daha bilgi verici [...]

1kb’lık kayan menu « basarozcan on 01 Jun 2008 at 5:06 am

Wow, this is really cool stuff. Have you ever thought of creating your own javascript library? I use jQuery but it contains a lot of functions i would never use. If i had all your functionality in one library it would cover most of my needs for less advanced sites and be a tenth the size!

Si on 01 Jun 2008 at 6:43 am

Nicely done. I like to read your articles.

Janko on 01 Jun 2008 at 8:12 am

Thanks for the suggestions/feedback.

@christophe - I debated whether to add the slider to the DOM with the JavaScript or not. Anyone interested can definitely grab your code. I will consider changing it when I update the script.

@Si - The thought had crossed my mind however I really do not have the time to support something on that scale. Perhaps with enough community support it would be possible. Will keep it in mind.

Michael on 01 Jun 2008 at 8:37 am

Really nice, working fine on Konqueror :)

Ayoze on 01 Jun 2008 at 9:36 am

On mouse-out the default position should be the last item you hovered over.

Alexander on 01 Jun 2008 at 1:14 pm

Very nice!
Congrats! :)

Marco on 01 Jun 2008 at 2:22 pm

It’s broken in Firefox3 RC1

Alex on 01 Jun 2008 at 3:16 pm

@Alex - I developed the script using Firefox 3.0 RC1 and am testing it right now with no problems. Shoot me an email with some more information as to what exactly you are seeing if you don’t mind. Thanks for reporting.

Michael on 01 Jun 2008 at 3:19 pm

[...] Sliding JavaScript Menu Highlight 1kb (tags: javascript menu slider navigation webdesign css menus **) [...]

links for 2008-06-02 « Simply… A User on 01 Jun 2008 at 6:30 pm

Just what I’ve been searching for. Thanks for all the hard work! Camxso

KBG on 01 Jun 2008 at 8:01 pm

Very Nice! (They all are.)

I notice that each of the SliderMenus on this page are in iframes.
Have you tried writing these scripts to allow multiple instances on a single page? I am trying to re-write your Horizontal Accordion as a js class to allow this, will let you know if I succeed.

Keep up the good work :)

nickg on 01 Jun 2008 at 10:22 pm

@nickg - I have had a couple requests for multiple instance support so I probably will be changing them around a bit to accommodate. Thanks for the feedback.

Michael on 01 Jun 2008 at 10:32 pm

[...] Sliding JavaScript Menu Highlight 1kb This sliding hover effect script is an easy method to add some flavor to your navigation. (tags: javascript webdesign) [...]

links for 2008-06-02 « Mandarine on 01 Jun 2008 at 10:32 pm

Good very good sliding effect.

Geshan on 02 Jun 2008 at 1:54 am

Thanks for you people you really make programming brilient.

manoranjan on 02 Jun 2008 at 4:59 am

Excellent improvement.

web design company on 02 Jun 2008 at 6:52 am

[...] sliding hover effect script is an easy method to add some flavor to your navigation.read more | digg [...]

Sliding JavaScript Menu Highlight 1kb | White Sands Digital on 02 Jun 2008 at 8:12 am

[...] Sliding JavaScript Menu Highlight 1kb - Multi-Core Ant Colony Optimization for TSP in Erlang - SQL Server Programming Hacks - Using URL [...]

Katod’un Seyir Defteri - 02.06.2008 at katodivaihe on 02 Jun 2008 at 12:32 pm

[...] Sliding JavaScript Menu Highlight 1kb - Web Development Blog (tags: javascript menu css navigation) [...]

links for 2008-06-02 « toonz on 02 Jun 2008 at 5:31 pm

[...] блог Майкла Лейгебера. Категория: JavaScriptАвтор: Spider Дата: 3 Июнь 2008 Время: 0:14 [...]

Анимированное меню на JavaScript | АяксЛайн.ру on 02 Jun 2008 at 6:15 pm

[...] Sliding JavaScript Menu Highlight 1kb - Web Development Blog (tags: javascript menu css navigation slider) [...]

» links for 2008-06-02 | Paul Cowles on 02 Jun 2008 at 6:18 pm

I’ve used this in my menu here: http://bubblebean.org. Thanks so much! Camxso

KBG on 02 Jun 2008 at 11:43 pm

[...] Sliding JavaScript Menu Highlight 1kb (tags: javascript) [...]

Skylog » Blog Archive » links for 2008-06-03 on 03 Jun 2008 at 12:31 am

Michael,

Your solutions are so light weight - my first reaction to making something like that would be to use jQuery or mootools. To put it in perspective, the smallest file size that jQuery compresses to is 30KB. If you’re not using jQuery for anything else but an effect such as this, use this solution instead!

Keep up the great work and I look forward to your future posts! This blog is on fire!

Jacob Gube on 03 Jun 2008 at 1:10 pm

This inspired me to create a more expandable, vertical menu along the same lines yesterday… see http://fruise.googlepages.com/bookmarks.html

Lee on 04 Jun 2008 at 3:19 am

@Lee - Looks good. Thanks for sharing the link. In IE there does seem to be some jerking as you move through the links. Might want to look into it.

Michael on 04 Jun 2008 at 8:18 am

Yes, thanks… I had to kludge the positioning for IE (at least I can run it in Linux). I still haven’t worked out how you got the slider bar to actually show the text underneath through it - if I fix IE to add the slider to the body then it becomes solid which is useless. If it’s attached to the menu, the ‘absolute’ position becomes relative to the menu… How did you get the CSS to work to show the text through the slider? (-:

Lee on 04 Jun 2008 at 11:50 am

Muito bom garoto!!!

:P

Areta do Bem on 04 Jun 2008 at 11:55 am

@Lee - It is a combination of the z-index on the slide and position:relative set on the parent. If you can’t get it working how you want shoot me an email and I will take a look at your code.

@Areta do Bern - Obrigado :-)

Michael on 04 Jun 2008 at 12:24 pm

This is SUPER-SUPER cool! I’m just wondering, can you do a version with a dropdown menu? ^ ^ hehe..

Thank you!

Amanda on 04 Jun 2008 at 3:45 pm

Very good ;) Thank you.

Deniz AKIN on 04 Jun 2008 at 11:16 pm

Excellent tutorial. Simple and direct.

Tiago Celestino on 05 Jun 2008 at 10:41 am

[...] Sliding JavaScript Menu Highlight - o soluţie foarte elegantă de meniu pe un singur nivel, cu sublinierea opţiunii deasupra căreia este mouse-ul. Nu pot explica: daţi click pe link ca să vedeţi. [...]

Leigeber: Web Development Blog | CNET.ro on 06 Jun 2008 at 12:31 pm

you use :
document.onmousemove = this.pos;

if in the site you have another onmouseover declaration, yours will block the new one.
can you fix this?

gio on 11 Jun 2008 at 5:55 am

@gio - This script down not use onmousemove, perhaps you are referring to the tooltip script? If so shoot me an email with a link so I can take a look at the conflict and find a compromise.

Michael on 11 Jun 2008 at 8:09 am

This is awesome mate, I too want to use multiple menus - so I’m trying to modify your code, I suspect you’ll beat me to it though.
I’m trying to modify your menu to have sub-menus appear horizontally below, it would be the perfect menu on the web then!

Richard on 12 Jun 2008 at 3:30 pm

Bello, semplice, compatto. Molto utile per piccoli e grandi lavori. Complimenti!

Giuseppe on 13 Jun 2008 at 5:28 am

Great work. I just modified a little bit to support multiple menus in a page, also support click to set the “menu selection”:
<pre>
var menuSlider=function(){
// usage: onload=”menuSlider.init(’menu1′,’slide1′);menuSlider.init(’menu2′,’slide2′)” /
var q=8; //delta width
var i=8; //delta time interval (ms)

var sliders=[]; //all share the sam idx;
var ms=[];
var intervals=[];
return{
init:function(menu,slider){
var g=document.getElementById(slider);
var idx = sliders.indexOf(g);
if(idx !==-1) return; //already initialized

sliders.push(g); idx=sliders.length-1;

var menu_items=document.getElementById(menu).getElementsByTagName(’li’);
ms[idx]=menu_items[0];
for(var i=0, L=menu_items.length;i<L;i++){
var c=menu_items[i]; if(c.value==1){ms[idx]=c;}
c.onmouseover=function(){menuSlider.mo(this, idx)}; c.onmouseout=function(){menuSlider.mo(ms[idx], idx)};
c.onmousedown=function(){ms[idx]=this;return false};
}
g.style.width=ms[idx].offsetWidth+’px’;
g.style.left =ms[idx].offsetLeft +’px’;
},
mo:function(d,idx){
clearInterval(intervals[idx]);
var el,ew; el=parseInt(d.offsetLeft); ew=parseInt(d.offsetWidth);
intervals[idx]=setInterval(function(){menuSlider.mv(el,ew,idx)},i);
},
mv:function(el,ew,idx){
var g=sliders[idx];
var l,w; l=parseInt(g.offsetLeft); w=parseInt(g.offsetWidth);
if(l!==el||w!==ew){
if(l!=el){var ld,lr,li; ld=(l>el)?-1:1; lr=Math.abs(el-l); li=(lr<q)?ld*lr:ld*q; g.style.left =(l+li)+’px’}
if(w!=ew){var wd,wr,wi; wd=(w>ew)?-1:1; wr=Math.abs(ew-w); wi=(wr<q)?wd*wr:wd*q; g.style.width=(w+wi)+’px’}
}else{clearInterval(intervals[idx])}
}};}();
</pre>
#note: those who want to support older version of browser, may need this Javascript extension: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf

AC on 14 Jun 2008 at 3:54 am

Thank you, this is an awesome effect and very easy to implement.

I am running into one problem when using it. It seems to prevent .png fixes for transparency in IE6 from working. I’m no javascript coder, so have no idea where to look. I’ve confirmed that when I remove the body onload the png fix works, when it’s there no dice. I’m currently using Supersleight (http://24ways.org/2007/supersleight-transparent-png-in-ie6). I’ve also tried a few of the other fixes and none seem to work with the menueffect enabled. Any ideas?

And thanks again for this and all the other awesome scripts you’ve made available.

Windy Phillips on 16 Jun 2008 at 9:09 am

@AC - Thanks for sharing, I will look over you code later on.

@Windy - You can email me a link to check it out but it is probably due to multiple onload events. You would need to wrap both events in one global onload function. Hope that makes sense.

Michael on 16 Jun 2008 at 10:37 am

Definitely makes sense. I’d puzzled that out as the probable culprit and started hunting for solutions when you responded. I’ve been poking around for some Global Onloaders on the web so I can have one js get called onload that does all the separate functions. I’ve found a couple variations online. Is there any you’d recommend? Thanks again for the great tools and help. You should put a donation link up so we can at least buy you lunch for all the great stuff. :)

Windy Phillips on 17 Jun 2008 at 7:54 am

@Windy - If you only have a couple you could do something like…

window.onload = function(){
function1();
function2();
}

Michael on 17 Jun 2008 at 8:15 am

is there any way to use an id instead of value=”1″ for the active tab? trying to integrate this to joomla menu system, in the active <li> tag i have <li id=”current”>.

And do you have any idea why the slider is moving so slow when integrated in joomla?

Love your site by the way!!

Henrik on 01 Jul 2008 at 4:25 pm

Michael, Thanks for the email reply! I ended up scrapping the original yacht site and now am building one using these sliding menus. They work great for me on all browsers except IE. I know that it works on IE “as-is” but as soon as I wrap it in a containing div and lengthen the menu bar, I end up having problems (on IE). It seems that the text move relative to the slider. It could be something entirely trivial, but I can’t seem to figure it out regardless. Any thoughts? Here is the site: http://www.yachtpartnership.org

Parker on 02 Jul 2008 at 4:33 am

@Parker - Looks like a CSS issue, shoot me a zip of the files and I will look into it. michael@leigeber.com

Michael on 02 Jul 2008 at 8:04 am

I assumed it was a CSS issue as well, but could not find it until about 10 mins ago. I had text-align: center in the Body definition for some reason. This was shifting the list on the sliding Nav. Thank you for the help!

Parker on 03 Jul 2008 at 4:54 pm

Dear Michael,

I have issue that i need load the menu based on user id, so i have 5 different menu category to load based on user. what will be right way to use this sliding down menu. As of now im using some menu its supporting XML as datasource. but im very much impressed with ur menu… plz try to give a solution that how i can go forward using ur menu.

Thanks & Regards

veeru on 06 Jul 2008 at 8:57 pm

[...] Sliding JavaScript Menu Highlight 1kb - Web Development Blog [...]

(依主題重組):電腦技術領域 « eweibookmark on 07 Jul 2008 at 5:45 pm

Henrik, here’s how you can use an id=”current” instead of the value=”1″:

On line 8 change this:
var c,v; c=e[i]; v=c.value; if(v==1){s=c; w=c.offsetWidth; p=c.offsetLeft}

To this:
var c,v; c=e[i]; v=c.id; if(v==’current’){s=c; w=c.offsetWidth; p=c.offsetLeft}

Amazing script Michael. I look forward to your future posts!

Ben on 07 Jul 2008 at 11:12 pm

Is there a way to change the speed of the slide? I’m new to javascript so its tough to rap my head around it.

Thanks in advance.

Ryan on 12 Jul 2008 at 7:48 pm

@Ryan - The q and i variables at the top of the script will effect the speed.

Michael on 15 Jul 2008 at 6:30 pm

Thanks Michael!

Ryan on 16 Jul 2008 at 11:36 am

hello, how you can add the messenger?
I leave you here, I need some help installing it on my blog, maybe you can do for me, and besides you can give me a few more tips: P
Please if you can add! here I leave the msn: mariano.143@hotmail.com
Sorry for my English, I’m argentine , and don’t speak their language, ’cause use a translator
or at least send a message explaining where each item goes on the page, I am novice in this CSS and HTML.
I’ve been looking for a menu like this for months!
please respond!
taquito

taquito on 17 Jul 2008 at 4:17 pm

when used with Dynamic Image Gallery and Slideshow I can’t get the slide effect to work! Can you help? I think it has something to do with the onload event?

maarten on 10 Aug 2008 at 5:09 am

ok, I moved the onload event from the body to the image gallery js script, this works perfectly!

btw awesome scripts you’ve put online for all of us! thnx

maarten on 10 Aug 2008 at 6:07 am

I put it online check result http://www.extrasaus.nl thnx again

maarten on 10 Aug 2008 at 8:05 am

@windy I have the same problem I want to use a trans png and can’t get it to work, i’m using this png hack:http://www.twinhelix.com/css/iepngfix/

@michael: what is this and how to implement it? :
window.onload = function(){
function1();
function2();
}

dreedenator on 13 Aug 2008 at 7:40 am

@windy: I got my trans png working on ie6 with the htc hack i was using. Just had to initialize it by adding the li in the style section. Now however only the left side works the right corner is gone!

dreedenator on 15 Aug 2008 at 9:50 am

Gratefully appreciated.

gratefull on 15 Aug 2008 at 11:53 am

This is a great lil script, but I am having trouble getting it to work in IE (6&7), any suggestions? Here is the site I am trying to get it to work on http://www.back40internetinnovators.com/thepuzzleboy/index3.htm It works fine in FF and Opera but it throws the text and slider to the center of the page, and does not align them properly. I realize that this is probably my fault due to an error I made somewhere, but I have had no luck tracking it down.

Thanks much for any assistance or suggestions you may be able to provide.

surelock on 20 Aug 2008 at 10:39 am

@dreedenator - The function I referenced is an example of wrapping multiple onload events into one global onload function call.

@surelock - Looks the same in IE7 and FF to me so not sure what end result you are looking for. Email me if you like michael@leigeber.com.

Michael on 24 Aug 2008 at 9:09 pm

Awesome job…thanks a lot! It truly helps us lesser mortals with little or no knowledge of javascript :)
But i’m having a problem with aligning the slider and the text in the menu. (in IE 7)
css –>
.menu{
position:relative;
background: url(”yellow.gif”) repeat-x; */yellow.gif is 1×100 px/*
width:100%;
height:100px;
}

.menu ul {list-style:none; z-index:10; position:absolute; z-index:100; padding:7px }
.menu li {float:left}

.menu a{
font-size:12px;
font-family: arial, helvetica, sans-serif;
text-transform: uppercase;
font-weight: bold;
text-align:left;
}

.menu a, .menu a:active, .menu a:visited, .menu a:hover {
text-decoration:none;
color:#FFF;
padding:6px;
}

#slide {position:absolute; height:30px; background:#89957a; opacity:0.4;
filter:alpha(opacity=40); z-index:10}

markup–>
<div class=”menu”>
<ul id=”menu”>
<li id=”current”><a href=”#” >home</a></li>
<li><a href=”#”>pictures</a></li>
<li><a href=”#”>architectures</a></li>
<li><a href=”#”>my account</a></li>
<li><a href=”#”>how to</a></li>
</ul>
<div id=”slide”></div>

</div>

Shalini on 25 Aug 2008 at 8:08 am
Trackback URI | Comments RSS

Leave a Reply

Subscribe to RSS Feed
Powered by FeedBurner
Recent Links
Tag Cloud