当前位置:编程学习 > html/css >>

关于导航菜单鼠标悬停,图片切换css代码

问题描述: 导航大致效果,进入网站时,<li>首页</li>要求背景是红色的,其他<li>是白色的,当鼠标移到其他的<li>时是蓝色的(hover),关键当点击其他<li> 是怎么把"首页"的红色背景移到已经点击的其他<li>上且固定在此,直到点击下一<li>再移动红色背景。如果仅用css不需要js能实现吗,或者尽量不需要太多的js代码。不要求什么渐变,漂浮效果。
答案:<html>
<style>
li {float:left;margin-right:25px;padding:5px}
.active {background-color:red}
.hover {background-color:blue;color:white}
.normal {background-color:white;color:black}
</style>
<body>
<ul id="navbar">
<li class="active" id="first">首页</li>
<li id="second">二页</li>
<li id="third">三页</li>
</ul>
<script type="text/javascript">
var navbar=document.getElementById("navbar");
var items=navbar.getElementsByTagName("li");
var curactiveitem="first";


for(i=0;i<items.length;i++){
items[i].onmouseover=onhover;
items[i].onclick=onactive;
}

function onhover(){
for(i=0;i<items.length;i++){
if(items[i].id!=curactiveitem){
items[i].className="normal";
}

}
if(this.id!=curactiveitem){this.className="hover";}
}

function onactive(){
document.getElementById(curactiveitem).className="normal";
this.className="active";
curactiveitem=this.id;
}
</script>
</body>
</html>
其他:<!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" />
<style type="text/css">
body { font-family: Verdana; font-size: 12px; line-height: 1.5; }
a { color: #000; text-decoration: none; }
a:hover { color: #F00; }
#menu { width:500px; height:28px; margin:0 auto; border-bottom:3px solid #E10001;}
#menu ul { list-style: none; margin: 0px; padding: 0px; }
#menu ul li { float:left; margin-left:2px;}
#menu ul li a { display:block; width:87px; height:28px; line-height:28px; text-align:center; background:url(http://www.aa25.cn/upload/2010-06/27/nav_bg2.gif) 0 0 no-repeat; font-size:14px;}
#menu ul li a:hover { background:url(http://www.aa25.cn/upload/2010-06/27/nav_bg3.gif) 0 0 no-repeat;}
#menu ul li a#current { background:url(http://www.aa25.cn/upload/2010-06/27/nav_bg1.gif) 0 0 no-repeat; font-weight:bold; color:#fff;}
#menu ul li ul { border:1px solid #ccc; display:none; position:absolute;}
#menu ul li ul li { float:none; width:87px; background:#eee; margin:0;}
#menu ul li ul li a { background:none;}
#menu ul li ul li a:hover { background:#333; color:#fff;}
#menu ul li:hover ul { display:block;}
#menu ul li.sfhover ul { display:block;}
</style>
</head>
<body>
<div id="menu">
<ul>
<li><a id="current" href="#">首页</a></li>
<li><a href="#">网页版式</a></li>
<li><a href="#">web教程</a></li>
<li><a href="#">web实例</a></li>
<li><a href="#">常用代码</a></li>
</ul>
</div>
</body>
</html> 

上一个:哪位有 CSS新手完全自学教程<彼岸苏安>这本书的代码
下一个:div+css鼠标放上去显示的div,跟选中滴div不在同一行排列;哪位好心的大哥大姐教教我吧。

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,