下划线跟随导航

Original 2018-11-29 18:43:40 185
abstract:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>下划线跟随导航</title> <script type="text/javascript" src="jque
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>下划线跟随导航</title>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<style>
*{margin:0px;padding:0px;}
.menu{
width:500px;
height:32px;
background: black;
margin:20px auto;
box-shadow: 0px 2px 20px;
border-radius: 3px;
position: relative;
}

ul li{

float:left;
width:100px;
height:30px;
line-height: 30px;
color:#ccc;
font-weight: bold;
text-align: center;
cursor: pointer;
list-style: none;
font-size:15px;
}

.block{
width:100px;
height:2px;
background: #fff;
top:30px;
position: absolute;
z-index: 10;

}

</style>

<script type="text/javascript">
$(function(){
$('li').hover(function(){
$x=parseInt($(this).attr('name'))*100;
$('.block').stop().animate({left:$x+'px'},500)
},
function(){$('.block').stop().animate({left:'0px'},500)
})
})


</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"></div>
</div>
</body>
</html>

注意相对定位和绝对定位的使用:
    使用绝对定位的时候,定位的参照元素是包含定位属性的父元素,
    如果没有父元素,则元素按照<body>元素的位置,确定显示位置。
z-index属性的使用:
    z-index 仅能在定位元素上奏效
 
hover(over,out)
    over:鼠标移上元素上要触发的一个函数
    out:鼠标移出元素上要触发的一个函数
   语法:
   $(document).ready(function(){
       $("selector").hover(function(){},function(){})
       })


Correcting teacher:灭绝师太Correction time:2018-11-30 09:16:05
Teacher's summary:很简单的动画效果,思路清楚以后很容易就完成了,要自己试着找点案例把知识点运用起来

Release Notes

Popular Entries