当前位置导航:炫浪网>>网络学院>>网页制作>>HTML与CSS教程

完全用CSS实现的中英文双语导航菜单

这是一个完全用CSS实现的中英文双语导航菜单,无脚本和图片,初始时,菜单语言是英文,当鼠标悬停在菜单上时,其变成中文,当然你也可以反过来做。

实现原理:

   XHTML

<ul id="nav">
<li><a class="bi" href="index.html">Home<span>首 页</span></a></li>
</ul>

从上面代码可以看出,中文放在 span 标签里,关键是需要在初始时,使其的属性不可见。

   CSS

.bi{
position: relative;
z-index: 0;
}

.bi:hover{
z-index: 99;
}

.bi:hover span{
visibility: visible;
top: 0;
left: 0;
cursor: pointer;
}

.bi span{
position: absolute;
left: -999em;
visibility: hidden;
}

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

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

.bi:hover span{
padding-top: 2px;
}

链接的属性是相对定位,那在此标签里的元素可将其作为参照点。 在 span 元素里的中文因其设定了不可见,所以在初始状态时,只能显示英文。而当鼠标悬停在菜单上时,

span 元素里的设定变为可见,Z轴为99 ,覆盖在英文上面,从而实现了语言文字的转换。缺点:因为中英文字的长度关系,不能实现自适应宽度,只能固定宽度。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Keywords" content="bilingual menu,中英文双语菜单" />
<meta http-equiv="Description" content="完全用CSS实现的中英文双语导航菜单" />
<meta content="all" name="robots" />
<meta http-equiv="pragma" content="no-cache" />
<meta name="author" content="forestgan" />
<meta name="copyright" content="forestgan" />
<title>完全用CSS实现的中英文双语导航菜单</title>
<style type="text/css">
a{
 color: #FFFF99;
 text-decoration: none;
}

a:hover{
 color: #FFFFFF;
 text-decoration: underline;
}
  
#nav{
 padding: 10px 10px 0;
 font-size: 12px;
 font-weight: bold;
 margin: 1em 0 0;
 list-style:none;
}

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

.bi{
 position: relative;
 z-index: 0;
}

.bi:hover{
 z-index: 99;
}

.bi:hover span{
 visibility: visible;
 top: 0;
 left: 0;
 cursor: pointer;
}

.bi span{
 position: absolute;
 left: -999em;
 visibility: hidden;
}

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

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

.bi:hover span{
 padding-top: 2px;
}

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

</style>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top">
  <ul id="nav">
  <li><a class="bi" href="index.html">Home<span>首 页</span></a></li>
  <li><a class="bi" href="about.html">About us<span>关于我们</span></a></li>
  <li><a class="bi" href="products.html">Products<span>产品展示</span></a></li>
  <li><a class="bi" href="services.html">Services<span>售后服务</span></a></li>
  <li><a class="bi" href="contact.html">Contact<span>联系我们</span></a></li>
 </ul>
 <div id="navbar"></div>
</div>
<div id="footer">
  <address>
  Design by <a href="http://www.forest53.com">forestgan</a> 本演示采用<a href="http://www.creativecommons.cn/">创作共用授权</a>--署名和非商业用途。&nbsp;&nbsp;<a href="javascript:history.back()">Back</a>
  </address>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-152060-1";
urchinTracker();
</script>
<noscript>google-analytics</noscript>
<script src="http://www.forest53.com/stat/mystat.asp?siteid=1" type="text/javascript"></script>
<noscript>stat.</noscript>
</body>
</html>
<script language="Javascript">
var now = new Date();
document.write("<img src='http://counter.yesky.com/counter.shtml?CID=54197&AID=-1&refer="+escape(document.referrer)+"&rand="+ now.getTime()  + "&cur="+escape(document.URL)+"' border='0' alt='' width='0' height='0'>");
</script>
<noscript>
<img src="http://counter.yesky.com/counter.shtml?CID=54197&AID=-1&refer=noscriptcounter&cur=noscriptcounter" border='0' width='0' height='0'/>
</noscript>

相关内容
赞助商链接