美文网首页
单页面导航

单页面导航

作者: 嘬烟盒的程序员 | 来源:发表于2016-12-24 17:06 被阅读0次
<style>
*{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
.nav{
width:100%;
height:40px;
background: #222;
}
.last{
width:1000px;
height:40px;
margin: 0 auto;
}
.last li{
float: left;
}
.last li a {
color: #aaa;
padding: 0 30px;
line-height: 40px;
text-decoration: none;
display: block;
}
.last li a:hover{
background: #333;
color: #fff;
}
.last li a.on{
background: #333;
color: #fff;
}
</style>
<script src="jquery-1.7.2.js"></script>
<script src="style.js"></script>
</head>
<body>
<div class="nav">
<ul class="last">
<li><a href="index.html">1</a></li>
<li><a href="index2.html">2</a></li>
<li><a href="index3.html">3</a></li>
<li><a href="index4.html">4</a></li>
</ul>
</div>
<h1>1</h1>
</body>
</html>

index2-4的布局都是一样的。
style.js

$(function(){    
        var index=window.location.href.split('/').length-1;    
        var href=window.location.href.split('/')[index];    
        $('.last li a[href="'+href+'"]').addClass('on');
});

相关文章

网友评论

      本文标题:单页面导航

      本文链接:https://www.haomeiwen.com/subject/gnzlvttx.html