abstract:<!DOCTYPE html><html><head> <title></title> <script type="text/javascript" src="jquery-3.3.1.min.js"></script><style type="text/cs
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<style type="text/css">
*{padding: 0px;margin: 0px;}
ul {list-style: none;z-index: 20;position: relative;font-size: 15px;}
li {float: left;cursor: pointer;width: 100px; height: 30px;text-align: center;line-height: 30px;color: #fff;font-weight: bold;}
.menu {width: 500px;position: relative;margin: 20px auto;height: 32px; box-shadow: 0 2px 20px #000; background: #AF3434;border-radius: 3px;}
</style>
<script type="text/javascript">
$(function(){
$('li').hover(
function(){
$x = parseInt($(this).attr('name'))*100
$('.block').stop().animate({left: $x + 'px'}, 300)
},
function(){
$('.block').stop().animate({left:'0px'},300)
}
)
})
</script>
</head>
<body>
<div class="menu">
<ul>
<li name="0">首页</li>
<li name="1">php中文网</li>
<li name="2">独孤九剑</li>
<li name="3">西门大官人</li>
<li name="4">灭绝师太</li>
</ul>
<div class="block" style="z-index: 10;width: 100px;height: 2px; background: #fff;position: absolute;top: 30px"></div>
</div>
</body>
</html>
<!--
name设定的数字隐含了位置移动的距离,通过parseInt()进行采集并同步放大100在加上px单位,即可通过hover实现下划线的移动
stop()的作用在于一个缓冲,因为有两个动作,一个是移动,另外一个是还原。那么stop()起到了缓冲的作用,是的效果不会冲突
还原的过程就是把初始位移重新设定为0 -->