当前位置:首页 > 建站知识

中英文双语导航菜单

更新时间:2009.06.22 浏览次数:

作者的blog :http://www.planabc.net/

老甘的《完全用CSS实现的中英文双语导航菜单》一文中使用“position: absolute;left: -999em;”将其左缩进N远,并通过“visibility: hidden;”将其物理隐藏(实际是占位的),此时显示英文导航。当hover触发时,z-index定义在上,并将其绝对定位位置设置为左上,设置visibility: visible;显示,此时span层覆盖在a层上,显示中文。

我们还可以还一种思维使用hover触发display属性实现:

CSS代码修改如下

#nav li a,#nav li a:hover span {
  line-height: 20px;
  text-decoration: none;
  background: #DDDDDD;
  color: #666666;
  display: block;
  width: 80px;
  text-align: center;
  overflow:hidden;
}

#nav li a span {
  display:none;
}

#nav a:hover {
  position: relative;
}

#nav a:hover span {
  display:block;
  position:absolute;
  top: 0;
  left: 0;
  cursor: pointer;
}

#nav a:hover span {
  padding-top:2px;
}

#nav li a:hover,#nav li a:hover span {
  color: #FFFFFF;
  background: #DC4E1B;
}

运行代码

<!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=utf-8" />
<title>中英文双语导航菜单</title>
<style type="text/css">
* {margin:0;padding:0;}  
#nav{
 padding: 10px 10px 0;
 font-size: 12px;
 font-weight: bold;
 font-family:Arial, Helvetica, sans-serif,宋体;
 margin: 1em 0 0;
 list-style:none;
}

#nav li{
 float: left;
 margin-right: 1px;
}

#nav li a,#nav li a:hover span{
 line-height: 20px;
 text-decoration: none;
 background: #DDDDDD;
 color: #666666;
 display: block;
 width: 80px;
 text-align: center;
 overflow:hidden;
}

#nav li a span{display:none;}

#nav a:hover{
 position: relative;
}

#nav a:hover span{
 display:block;
 position:absolute;
 top: 0;
 left: 0;
 cursor: pointer;
}

#nav a:hover span{
    padding-top:2px;
}

#nav li a:hover,#nav li a:hover span{
 color: #FFFFFF;
 background: #DC4E1B;
}

#navbar{
 background: #DC4E1B;
 height: 8px;
 overflow: hidden;
 clear: both;
}

</style>
</head>

<body>
    <ul id="nav">
  <li><a href="index.html">Home<span>首 页</span></a></li>
  <li><a href="about.html">About us<span>关于我们</span></a></li>
  <li><a href="products.html">Products<span>产品展示</span></a></li>
  <li><a href="services.html">Services<span>售后服务</span></a></li>
  <li><a href="contact.html">Contact<span>联系我们</span></a></li>
 </ul>
 <div id="navbar"></div>
</body>
</html>