怎样实现这个导航菜单?
功能:点击导航其中一个栏目变色,再点击页面其他地方,导航没有变化
点击导航另一个栏目变色,并且之前的栏目恢复到未点击之前的状态
h5:
<html>
<head>
<link href="navigation.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function(){
$(".menu a").focus(function(){
$(this).addClass("cur").siblings().removeClass("cur");
})
})
</script>
</head>
<body>
<div class="menu">
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<del></del>
</div>
</body>
</html>
css:
.menu a{
display:block;
float:left;
}
.menu
{
font-size:14px;
line-height:14px;
background:url("image/navigation/bg1.jpg") repeat-x left bottom;
background-color:#f7f7f7;
margin-bottom:24px;
font-family:"Arial","Tahoma","微软雅黑","黑体";
line-height:18px;
padding:0px;
margin:0px;
text-align:center;
}
.menu a:link,.menu a:visited
{
color:#383737;
background-color:#f7f7f7;
border-bottom:solid 2px #e6e6e6;
text-decoration:none;
padding:10px 16px 10px 16px;
}
.menu a:hover,.menu a:focus
{
background-color:#646464;
border-bottom:solid 2px #2e2e2e;
color:#fff;
}
.cur
{
background-color:#646464;
border-bottom:solid 2px #2e2e2e;
color:#fff;
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<link href="navigation.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
$(".menu a").click(function(event){
var target = event.target;
$(target).css("background-color","#646464").siblings().css("background-color","#ffffff");
})
})
</script>
</head>
<body>
<div class="menu">
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<a href="#">导航一</a>
<del></del>
</div>
</body>
</html>
$(".menu a").bind({
focus:function(){$(this).toggleClass("red")},
blur:function(){$(this).removeClass('red')}
});
$(function() {
var events = {
change: function(event) {
var that = $(this),
cur = event.data.current;
that.addClass("cur").siblings().removeClass("cur");
}
}
$(".menu").on("click", "a", {current: 'cur'}, events.change);
})
上面漏替换变量了,这个是正确的。
$(function() {
var events = {
change: function(event) {
var that = $(this),
cur = event.data.current;
that.addClass(cur).siblings().removeClass(cur);
}
}
$(".menu").on("click", "a", {current: 'cur'}, events.change);
})
另一种组件化写法:
var changeColor = function(options) {
var config = {
trigger: '.menu',
target: 'a',
current: 'cur',
event: 'click'
};
var opts = $.extend({}, config, options);
$(opts.trigger).on(opts.event, opts.target, function(event) {
var that = $(this);
that.addClass(opts.current).siblings().removeClass(opts.current);
});
};
//实例化
new changeColor({
trigger: '.menu',
target: 'a',
current: 'cur',
event: 'click'
})
ok了
$(.menu"a").bind({
click:function(){
$(.menu a').removeClass("cur")
$(this).addClass("cur")
}
});
楼主你的连接href是有地址的吧,而不是死链接,发生了跳转刷新的话这个代码没用,可以看这个:网站导航栏目焦点设置
把css属性转化为你想要的class,class样式自己定义,我想这难不住你吧