JavaScript implements a simple instance of a secondary navigation menu

  • 2020-05-30 19:21:59
  • OfStack

This article illustrates how JavaScript implements a simple level 2 navigation menu. Share with you for your reference. The details are as follows:


<!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>css Menu demo </title>
<style type="text/css">
<!--
*{margin:0;padding:0;border:0;}
body {
font-family: arial,  Song typeface , serif;
     font-size:12px;
}
#nav {
  line-height: 24px; list-style-type: none; background:#666;
}
#nav a {
display: block; width: 80px; text-align:center;
}
#nav a:link  {
color:#666; text-decoration:none;
}
#nav a:visited  {
color:#666;text-decoration:none;
}
#nav a:hover  {
color:#FFF;text-decoration:none;font-weight:bold;
}
#nav li {
float: left; width: 80px; background:#CCC;
}
#nav li a:hover{
background:#999;
}
#nav li ul {
line-height: 27px; list-style-type: none;text-align:left;
left: -999em; width: 180px; position: absolute;
}
#nav li ul li{
float: left; width: 180px;
background: #F6F6F6;
}
#nav li ul a{
display: block; width: 180px;width: 156px;
text-align:left;padding-left:24px;
}
#nav li ul a:link  {
color:#666; text-decoration:none;
}
#nav li ul a:visited  {
color:#666;text-decoration:none;
}
#nav li ul a:hover  {
color:#F3F3F3;text-decoration:none;font-weight:normal;
background:#C00;
}
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}
#content {
clear: left;
}
-->
</style>
<script type=text/javascript>
<!--//--><![CDATA[//><!--
function menuFix() {
var sfEls = document.getElementById("nav").getElementsByTagName("li");
for (var i=0; i<sfEls.length; i++) {
  sfEls[i].onmouseover=function() {
  this.className+=(this.className.length>0? " ": "") + "sfhover";
  }
  sfEls[i].onMouseDown=function() {
  this.className+=(this.className.length>0? " ": "") + "sfhover";
  }
  sfEls[i].onMouseUp=function() {
  this.className+=(this.className.length>0? " ": "") + "sfhover";
  }
  sfEls[i].onmouseout=function() {
  this.className=this.className.replace(new RegExp("( ?|^)sfhover\b"),
"");
  }
}
}
window.onload=menuFix;
//--><!]]>
</script>
</head>
<body>
<ul id="nav">
<li><a href="#"> Product introduction </a>
<ul>
<li><a href="#"> product 1</a></li>
<li><a href="#"> product 1</a></li>
<li><a href="#"> product 1</a></li>
<li><a href="#"> product 1</a></li>
<li><a href="#"> product 1</a></li>
<li><a href="#"> product 1</a></li>
</ul>
</li>
<li><a href="#"> The service is introduced </a>
<ul>
<li><a href="#"> service 2</a></li>
<li><a href="#"> service 2</a></li>
<li><a href="#"> service 2</a></li>
<li><a href="#"> service 2 service 2</a></li>
<li><a href="#"> service 2 service 2 service 2</a></li>
<li><a href="#"> service 2</a></li>
</ul>
</li>
<li><a href="#"> Successful cases </a>
<ul>
<li><a href="#"> case 3</a></li>
<li><a href="#"> case </a></li>
<li><a href="#"> case 3 case 3</a></li>
<li><a href="#"> case 3 case 3 case 3</a></li>
</ul>
</li>
<li><a href="#"> About us </a>
<ul>
<li><a href="#"> we 4</a></li>
<li><a href="#"> we 4</a></li>
<li><a href="#"> we 4</a></li>
<li><a href="#"> we 4111</a></li>
</ul>
</li>
<li><a href="#"> The online demo </a>
<ul>
<li><a href="#"> demo </a></li>
<li><a href="#"> demo </a></li>
<li><a href="#"> demo </a></li>
<li><a href="#"> Demo demo demo </a></li>
<li><a href="#"> Demo demo demo </a></li>
<li><a href="#"> Demo presentation </a></li>
<li><a href="#"> Demo demo demo </a></li>
<li><a href="#"> Demo demo demo demo demo demo </a></li>
</ul>
</li>
<li><a href="#"> Contact us </a>
<ul>
<li><a href="#"> Contact contact contact contact contact </a></li>
<li><a href="#"> Contact contact contact </a></li>
<li><a href="#"> contact </a></li>
<li><a href="#"> Contact contact </a></li>
<li><a href="#"> Contact contact </a></li>
<li><a href="#"> Contact contact contact </a></li>
<li><a href="#"> Contact contact contact </a></li>
</ul>
</li>
</ul>
</body>
</html>

I hope this article has been helpful to your javascript programming.


Related articles: