console
$(".demo-nav1-items li").hover(function() {
var index = $(this).data("index");
var left = index * 110 + 5;
$(".demo-nav1-line").css({
"margin-left": left + "px",
"transition": "all .3s"
});
},
function() {
var active_index = $(".demo-nav1-items").find(".active").data("index");
var left = active_index * 110 + 5;
$(".demo-nav1-line").css({
"margin-left": left + "px",
"transition": "all .3s"
});
})
$(".demo-nav1-items").on("click", "li", function() {
$(this).siblings().removeClass("active");
$(this).addClass("active");
})
<div class="demo-nav1">
<h3>
导航下划线随鼠标移动
</h3>
<ul class="demo-nav1-items">
<li data-index="0" class="active">
<a> 导航一</a>
</li>
<li data-index="1">
<a>导航二</a>
</li>
<li data-index="2">
导航三
</li>
<li data-index="3">
导航四
</li>
<li data-index="4">
导航五
</li>
<span class="demo-nav1-line">
</span>
</ul>
</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js">
</script>
html,
body,
div,
ul,
li {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
ul {
list-style: none;
}
.demo-nav1 {
text-align: center;
position: relative;
margin-top: 5px;
width: 100%;
height: 150px;
border: 1px solid#5FB878;
}
.demo-nav1-items {
width: 550px;
height: 53px;
position: absolute;
left: 50%;
margin-left: -225px;
overflow: hidden;
}
.demo-nav1-items li {
float: left;
width: 100px;
text-align: center;
position: relative;
cursor: pointer;
transition: all .2s;
height: 50px;
line-height: 50px;
margin: 0 5px;
}
.active {
border-bottom: 3px solid#5FB878;
color: #5FB878;
}
.demo-nav1-line {
position: absolute;
display: inline-block;
width: 100px;
height: 3px;
bottom: 0;
left: 0;
margin-left: -100px;
background-color: #5FB878;
}