Here are the codes of a drop-down menu with javascript and css:
Html:
<head>
<title>A DOM drop-down menu</title>
<link rel="stylesheet" type="text/css" href="dropdown2.css">
<script language="javascript" type="text/javascript"
src="dropdown.js">
</script>
</head>
<body>
<h1>Menu Test</h1>
<ul id="menu">
<li class="menu"><a href="#">Home</a></li>
<li class="menu"><a href="#">Upper Body</a>
<ul>
<li><a href="#">Chest</a></li>
<li><a href="#">Back</a></li>
<li><a href="#">Shoulders</a></li>
</ul>
</li>
<li class="menu"><a href="#">Arms</a>
<ul>
<li><a href="#">Biceps</a></li>
<li><a href="#">Triceps</a></li>
<li><a href="#">Forearms</a></li>
</ul>
</li>
<li class="menu"><a href="#">Legs</a>
<ul>
<li><a href="#">Quads</a></li>
<li><a href="#">Hams</a></li>
<li><a href="#">Calves</a></li>
</ul>
</li>
<li class="menu"><a href="#">Tips</a>
<ul>
<li><a href="#">Weight Training</a></li>
<li><a href="#">Aerobics</a></li>
</ul>
</li>
</ul>
</body>
</html>
JS:
var t=false,current;
function SetupMenu() {
if (!document.getElementsByTagName) return;
items=document.getElementsByTagName("li");
for (i=0; i<items.length; i++) {
if (items[i].className != "menu") continue;
thelink=findChild(items[i],"A");
thelink.onmouseover=ShowMenu;
thelink.onmouseout=StartTimer;
if (ul=findChild(items[i],"UL")) {
ul.style.display="none";
for (j=0; j<ul.childNodes.length; j++) {
ul.childNodes[j].onmouseover=ResetTimer;
ul.childNodes[j].onmouseout=StartTimer;
}
}
}
}
function findChild(obj,tag) {
cn = obj.childNodes;
for (k=0; k<cn.length; k++) {
if (cn[k].nodeName==tag) return cn[k];
}
return false;
}
function ShowMenu(e) {
if (!e) var e = window.event;
thislink = (e.target) ? e.target: e.srcElement;
ResetTimer();
if (current) HideMenu(current);
thislink = thislink.parentNode;
current=thislink;
ul = findChild(thislink,"UL");
if (!ul) return;
ul.style.display="block";
}
function HideMenu(thelink) {
ul = findChild(thelink,"UL");
if (!ul) return;
ul.style.display="none";
}
function ResetTimer() {
if (t) window.clearTimeout(t);
}
function StartTimer() {
t = window.setTimeout("HideMenu(current)",200);
}
window.onload=SetupMenu;
CSS:
#menu {
position:absolute;
font-family: sans-serif;
font-size: 100%;
}
#menu li {
list-style-type: none;
background-color: silver;
border: 1px solid black;
text-indent: 0px;
margin-left: 3px;
}
#menu li a {
color: black;
text-decoration: none;
width: 100%;
display: block;
}
#menu li a:hover {
color:white;
}
#menu li ul {
background-color: silver;
margin: 0px;
padding: 0px;
margin-left: 84px;
margin-top: -20px;
float:left;
}
#menu li ul li {
padding: 0px;
margin: 0px;
list-style-type: none;
text-indent: 0px;
border: 1px solid black;
float:left;
}
#menu li ul li a {
color: black;
text-decoration: none;
}
#menu li ul li a:hover {
color:black;
background-color: aqua;
}
Can anyone tell me what i have to change in the CSS file so the sub-menus appear normally when i click on the menus without the menus width expanding?I would like have the sub-menus appear on the right and out of the main menus if possible.