不错的左纵向二级菜单(JS)

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>下拉效果</title>
<script type=text/javascript>
function $(d){
 return document.getElementById(d);
}
// set or get the current display style of the div
function dsp(d,v){
 if(v==undefined){
  return d.style.display;
 }else{
  d.style.display=v;
 }
}
// set or get the height of a div.
function sh(d,v){
 // if you are getting the height then display must be block to return the absolute height
 if(v==undefined){
  if(dsp(d)!='none'&& dsp(d)!=''){
   return d.offsetHeight;
  }
  viz = d.style.visibility;
  d.style.visibility = 'hidden';
  o = dsp(d);
  dsp(d,'block');
  r = parseInt(d.offsetHeight);
  dsp(d,o);
  d.style.visibility = viz;
  return r;
 }else{
  d.style.height=v;
 }
}
/*
* Variable 'S' defines the speed of the accordian
* Variable 'T' defines the refresh rate of the accordian
*/
s=7;
t=10;
//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(d){
 d = $(d);
 if(sh(d)>0){
  v = Math.round(sh(d)/d.s);
  v = (v<1) ? 1 :v ;
  v = (sh(d)-v);
  sh(d,v+'px');
  d.style.opacity = (v/d.maxh);
  d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
 }else{
  sh(d,0);
  dsp(d,'none');
  clearInterval(d.t);
 }
}
//Expand Timer is triggered as a setInterval to increase the height of the div exponentially.
function et(d){
 d = $(d);
 if(sh(d)<d.maxh){
  v = Math.round((d.maxh-sh(d))/d.s);
  v = (v<1) ? 1 :v ;
  v = (sh(d)+v);
  sh(d,v+'px');
  d.style.opacity = (v/d.maxh);
  d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
 }else{
  sh(d,d.maxh);
  clearInterval(d.t);
 }
}
// Collapse Initializer
function cl(d){
 if(dsp(d)=='block'){
  clearInterval(d.t);
  d.t=setInterval('ct("'+d.id+'")',t);
 }
}
//Expand Initializer
function ex(d){
 if(dsp(d)=='none'){
  dsp(d,'block');
  d.style.height='0px';
  clearInterval(d.t);
  d.t=setInterval('et("'+d.id+'")',t);
 }
}
// Removes Classname from the given div.
function cc(n,v){
 s=n.className.split(/\s+/);
 for(p=0;p<s.length;p++){
  if(s[p]==v+n.tc){
   s.splice(p,1);
   n.className=s.join(' ');
   break;
  }
 }
}
//Accordian Initializer
function Accordian(d,s,tc){
 // get all the elements that have id as content
 l=$(d).getElementsByTagName('div');
 c=[];
 for(i=0;i<l.length;i++){
  h=l[i].id;
  if(h.substr(h.indexOf('-')+1,h.length)=='content'){c.push(h);}
 }
 sel=null;
 //then search through headers
 for(i=0;i<l.length;i++){
  h=l[i].id;
  if(h.substr(h.indexOf('-')+1,h.length)=='header'){
   d=$(h.substr(0,h.indexOf('-'))+'-content');
   d.style.display='none';
   d.style.overflow='hidden';
   d.maxh =sh(d);
   d.s=(s==undefined)? 7 : s;
   h=$(h);
   h.tc=tc;
   h.c=c;
   // set the onclick function for each header.
   h.onclick = function(){
    for(i=0;i<this.c.length;i++){
     cn=this.c[i];
     n=cn.substr(0,cn.indexOf('-'));
     if((n+'-header')==this.id){
      ex($(n+'-content'));
      n=$(n+'-header');
      cc(n,'__');
      n.className=n.className+' '+n.tc;
     }else{
      cl($(n+'-content'));
      cc($(n+'-header'),'');
     }
    }
   }
   if(h.className.match(/selected+/)!=undefined){ sel=h;}
  }
 }
 if(sel!=undefined){sel.onClick();}
}
</script>
<style type="text/css">
<!--
body{
 font-size: 12px;
 color: #999999;
 background-color: #000000;
 font-family: Arial, Helvetica, sans-serif;
  text-transform: capitalize;
}
* {
 margin: 0px;
 padding: 0px;
 border: 0px;list-style:none;}
/*菜单样式*/
#basic-accordian{
 width:20%;
 z-index:2;
 margin-top: 2%;
 border-top-style: solid;
 border-top-color: #999999;
 border-bottom-style: solid;
 border-bottom-color: #666666;
 padding-top: 15px;
 padding-bottom: 15px;
 margin-bottom: 2%;
}/*菜单背景*/
.accordion_headings{
 padding:2px;
 color:#FFFFFF;
 cursor:pointer;
 font-weight:bold;
 font-size: 14px;
 line-height: 25px;
 letter-spacing: 1px;
}/*菜单分类行*/
.accordion_headings:hover{
}
.accordion_child{
 padding-left: 20px;
 padding-bottom: 8px;
}
.accordion_child ul{}
.accordion_child ul li{
 font-size: 12px;
 display: block;
 line-height: 20px;
 display:block;
}
.accordion_child ul li a{
 text-decoration: none;
 color: #666666;
}
.accordion_child ul li a:hover{
 color: #FFFFFF;
}
.header_highlight{
 background-color: #000000;
 color: #FF0099;
}/*当前显示菜单分类样式*/
 
/*菜单样式结束*/
.main {
 margin-top: 2%;
 width: 96%;
 margin-right: auto;
 margin-left: auto;
}
/*特殊样式-------------------------*/
-->
</style>
<script type="text/javascript" src="js/menu.js"></script>
</head>
<body οnlοad="new Accordian('basic-accordian',5,'header_highlight');">
<div class="main">
<div id="basic-accordian" ><!--菜单开始-->
<div id="test-header" class="accordion_headings" >新闻资讯</div><!--1新闻资讯-->
<div id="test-content">
    <div class="accordion_child">
 <ul><li><a href="#">学院通知</a></li>
 <li><a href="#">新闻资讯</a></li>
</ul>  
</div>   
  </div>
 
  <div id="test1-header" class="accordion_headings" >学院概况</div><!--2学院概况-->
  <div id="test1-content">
    <div class="accordion_child">
<ul><li><a href="#">学院简介</a></li>
<li><a href="#">蒙妮坦文化历史</a></li>
<li><a href="#">教学设施</a></li>
<li><a href="#">刘晓阳校长介绍</a></li>
<li><a href="#">优良师资</a></li>
<li><a href="#">资质认证与荣誉</a></li>
</ul>
</div></div>
  <div id="test2-header" class="accordion_headings" >专业设置</div> <!--3课程设置-->
<div id="test2-content">
    <div class="accordion_child">
<ul>
  <li><a href="#">形象设计 (大学专科)</a></li>
  <li><a href="#">化妆造型</a></li>
<li><a href="#">美容</a></li>
<li><a href="#">美发</a></li>
<li><a href="#">美甲</a></li>
</ul></div></div>
  <div id="test3-header" class="accordion_headings" >国际文凭课程</div><!--4国际文凭课程-->
<div id="test3-content">
    <div class="accordion_child">
<ul><li><a href="#">cidesco圣迪斯哥</a></li>
<li><a href="#">city & gulids国际美容师</a></li>
<li><a href="#">cibtac国际美容师</a></li>
<li><a href="#">srh国际发型师</a></li>
<li><a href="#">itec国际专业时装、舞台、媒体化妆师</a></li>
</ul></div></div>
       
          <div id="test4-header" class="accordion_headings" >师生经典作品</div>
          <!--5作品赏析-->
<div id="test4-content">
    <div class="accordion_child">
<ul>
  <li><a href="#">形象设计</a></li>
  <li><a href="#">化妆造型</a></li>
<li><a href="#">美容</a></li>
<li><a href="#">美发</a></li>
<li><a href="#">美甲</a></li>
</ul>
</div></div>
          <div id="test5-header" class="accordion_headings" >在线视频</div><!--6学校视频-->
<div id="test5-content">
    <div class="accordion_child">
<ul>
  <li><a href="#">学院介绍视频</a></li>
  <li><a href="#">活动视频</a></li>
  <li><a href="#">考试现场视频</a></li>
</ul>
</div></div>
    <div id="test6-header" class="accordion_headings" >蒙妮坦精英学员</div><!--7招生信息-->
<div id="test6-content">
    <div class="accordion_child">
<ul>
    <li><a href="#">各专业优秀学员</a></li>
    <li><a href="#">形象设计大学专科毕业生</a></li>
<li><a href="#">cidesco美容博士人才</a></li>
<li><a href="#">作育英才时代见证(1981-2000年)</a></li>
</ul>
</div></div>
          <div id="test7-header" class="accordion_headings" >招生信息</div><!--8招生信息-->
<div id="test7-content">
    <div class="accordion_child">
<ul>
  <li><a href="#">2008年招生简章</a></li>
  <li><a href="#">报名方式</a></li>
<li><a href="#">报名须知</a></li>
<li><a href="#">在线报名</a></li>
</ul>
</div></div>
          <div id="test8-header" class="accordion_headings" >咨询留言</div><!--8在线咨询-->
<div id="test8-content">
    <div class="accordion_child">
<ul>
  <li><a href="#">常见问题</a></li>
  <li><a href="#">在线留言</a></li>
<li><a href="#">联系我们</a></li>
</ul>
</div></div>
       
</div><!--菜单结束-->
</div><!--main结束-->
</body>
</html>

转载于:https://www.cnblogs.com/hanwater/archive/2009/09/22/1571515.html

### 回答1: 用纯CSS实现简单的二级菜单可以通过以下步骤: 1. 使用HTML创建菜单栏和子菜单的结构。 2. 使用CSS隐藏子菜单,当鼠标悬停在父菜单上时显示子菜单。 3. 使用CSS对菜单项的样式进行修饰,例如更改字体颜色、背景颜色等。 以下是一个实现二级菜单的简单HTML和CSS示例: HTML代码: ``` <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a> <ul> <li><a href="#">Our Team</a></li> <li><a href="#">Our History</a></li> <li><a href="#">Our Values</a></li> </ul> </li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> ``` CSS代码: ``` nav ul { list-style: none; margin: 0; padding: 0; } nav ul li { display: inline-block; } nav ul li a { display: block; padding: 10px; text-decoration: none; color: #333; } nav ul li:hover > ul { display: block; } nav ul ul { display: none; position: absolute; background-color: #fff; padding: 0; } nav ul ul li { display: block; } nav ul ul li a { padding: 5px 10px; } nav ul ul li a:hover { background-color: #f5f5f5; } ``` 以上代码会实现一个具有简单二级菜单的导航栏。当鼠标悬停在“About”菜单上时,将显示三个子菜单项(Our Team,Our History和Our Values)。 ### 回答2: 要实现一个简单的纵向二级菜单,需要使用HTML和CSS。首先,我们需要为每个菜单链接创建一个列表项。用HTML的<ul>标签来创建一个无序列表,每个菜单项定义为<li>标签,则每 个次级菜单都是第一级菜单项的子级列表。接下来,我们用CSS来控制菜单项的样式和位置。 首先,我们需要增加一些样式来改变默认的无序列表样式。可以使用CSS中的list-style属性来删除点号或数字,并使用“none”值来禁用其显示。将“margin”属性设置为0可使列表项之间的间距为0。 接下来,我们需要创建菜单项的hover样式。鼠标悬停在菜单项上时,需要修改其颜色、背景色或其他样式。可以使用:hover伪类来完成这个效果。 最后,我们需要确定菜单项的位置。使用CSS中的position属性来定义元素的定位方式。可以将菜单项的定位方式设置为absolute,这样就可以将其相对于最近的已定位父元素进行定位。例如,可以将最外层的<ul>元素设置为已定位,以便在其内部菜单项定位。对于子级菜单,我们可以设置其父级菜单项的position属性为relative,并将子级菜单的top属性设置为父级菜单项的高度,这样子级菜单就会出现在其父级菜单项的下方。 通过上述步骤,我们就可以实现一个简单的纵向二级菜单。如下面的代码: HTML: <ul> <li><a href="#">菜单1</a> <ul> <li><a href="#">子菜单1.1</a></li> <li><a href="#">子菜单1.2</a></li> <li><a href="#">子菜单1.3</a></li> </ul> </li> <li><a href="#">菜单2</a> <ul> <li><a href="#">子菜单2.1</a></li> <li><a href="#">子菜单2.2</a></li> <li><a href="#">子菜单2.3</a></li> </ul> </li> </ul> CSS: ul { margin: 0; padding: 0; list-style: none; position: relative; /*设置最外层列表的定位属性*/ } li { display: block; position: relative; float: left; } ul ul { position: absolute; top: 100%; /*设置子菜单的top属性*/ left: 0; display: none; z-index: 999; } ul li:hover > ul { display:inherit; } ul li a { display:block; padding:0 10px; line-height: 30px; text-decoration:none; } ul li:hover a { color: #fff; background: #000; } ul ul li { width:100%; float:none; display:list-item; position: relative; } ul ul ul li { position:relative; top:-60px; left:100%; } ### 回答3: div css实现纵向简单的二级菜单是基于CSS的菜单设计。如果你想要增加网站的可导航性,那么使用CSS菜单设计将会非常方便。 CSS菜单设计中最常见的就是横向菜单,但是如果你想实现纵向简单的二级菜单也是十分容易的。 首先,你需要创建两个具有子标签的div元素,一个作为导航栏的容器,一个作为菜单项的容器,并将它们都设置为显示为block元素。 ``` html <div class="nav-container"> <div class="menu-item"> <a href="#">菜单1</a> <ul> <li><a href="#">子菜单1-1</a></li> <li><a href="#">子菜单1-2</a></li> <li><a href="#">子菜单1-3</a></li> </ul> </div> <div class="menu-item"> <a href="#">菜单2</a> <ul> <li><a href="#">子菜单2-1</a></li> <li><a href="#">子菜单2-2</a></li> <li><a href="#">子菜单2-3</a></li> </ul> </div> </div> ``` 接下来,在CSS文件中,你需要设置.nav-container元素以及.menu-item元素的样式,以确保它们垂直排列,并使用一些CSS属性来隐藏其子元素的菜单项,直到鼠标悬停在它们上面的时候才显示。 ``` css .nav-container { display: block; } .menu-item { display: block; position: relative; padding: 10px; } ul { display: none; position: absolute; top: 100%; left: 0; margin: 0; padding: 0; } .menu-item:hover > ul { display: block; } ``` 这里我们设置子菜单的display为none,并设置它的position为absolute,使它在父元素.menu-item中完全脱离文档流,接下来是它的left和top值,我们设置成100%和0,使它能够完全垂直地展开。 最后,我们使用:hover伪类选择器来控制子菜单的显示和隐藏,这使得只有在鼠标悬停在菜单项上时才显示子菜单。 使用上面的CSS样式,你就可以设计出一个简单的纵向二级菜单。这个菜单设计在许多不同的网站中都是很常见的,因为它既简单易用,同时还为网站提供了更好的导航体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值