Black and White.

Sunday, May 27, 2007

A simple CSS Menu with slider

Found this nice piece of code while browsing . few lines of code to create a nice menu slider.

Example

Part 1 - HTML

<div id="menu">
<div id="m-top">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">F.A.Q.</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<div id="m-slider">
<div id="slider"></div>
</div>
</div>

Part 2 - Javascript

Now we add Javascript event that listen for mousemove on div that holds menu.

window.onload=function(){

var offsetLeft=$('menu').offsetLeft;

Event.observe('menu', 'mousemove', function(event){

coordinateX=Event.pointerX(event)-offsetLeft;
$('slider').style.marginLeft=coordinateX-20+'px';

});

}

No comments: