手记

QQ空间为例 | 制作下拉式导航条菜单+改变高度伸缩菜单

自学web前段两个月,这是今天所完成的
链接描述

QQ空间日志,可以复制,粘贴

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动画菜单</title>
<style type="text/css">
* { margin: 0; padding: 0; font-size: 14px; }
a { color: #533; text-decoration: none }
ul{ list-style: none; }
.nav ul {float:left;}

/*只改变一级菜单*/
.on:hover{ color:#fff;background-color:#F60;height:40px; line-height:40px;margin-top:-10px;}

.nav {height: 30px; border-bottom: 5px solid #F60; margin-left:50px; width:600px;margin-top:20px;}
.nav li { float: left; position:relative; height:30px; width:120px }
.nav li a { display: block; height: 30px; text-align: center; line-height: 30px; width:120px; background: #efefef; margin-left: 1px; }
.subNav{ position:absolute; top:30px; left:0; width:120px; height:0; overflow:hidden}
.subNav li a{ background:#ddd }
.subNav li a:hover{ background:#F54921}
</style>
<script>
window.onload=function(){
    var aLi=document.getElementsByTagName('li');
    for(var i=0; i<aLi.length; i++){
        //鼠标经过一级菜单,二级菜单动画下拉显示出来
        aLi[i].onmouseover=function(){
            var oSubNav=this.getElementsByTagName('ul')[0];
            if(oSubNav){
            var This=oSubNav;
            clearInterval(This.time);
            This.time=setInterval(function(){
                    This.style.height=This.offsetHeight+16+"px";
                    if(This.offsetHeight>=120)
                    clearInterval(This.time);
                },30)
             }
          }
    //鼠标离开菜单,二级菜单动画收缩起来。    
     aLi[i].onmouseout=function(){
            var oSubNav=this.getElementsByTagName('ul')[0];
            if(oSubNav){
            var This=oSubNav;
            clearInterval(This.time);
            This.time=setInterval(function(){
                    This.style.height=This.offsetHeight-16+"px";
                    if(This.offsetHeight<=0)
                    clearInterval(This.time);
                },30)
             }
          }

    }
}
</script>
</head>

<body>

<ul class="nav">

    <!-- class="on"确保只改变一级菜单 -->
    <li><a class="on" href="#">一级菜单</a>
        <ul class="subNav">
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
        </ul>
    </li>
    <li><a class="on" href="#">一级菜单</a>
        <ul class="subNav">
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
        </ul>
    </li>
    <li><a class="on" href="#">一级菜单</a>
        <ul class="subNav">
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
        </ul>
    </li>
    <li><a href="#">一级菜单</a>
        <ul class="subNav">
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
        </ul>
    </li>
    <li><a href="#">一级菜单</a>
        <ul class="subNav">
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
            <li><a href="#">二级菜单</a></li>
        </ul>
    </li>
</ul>
</body>
</html>
12人推荐
随时随地看视频
慕课网APP

热门评论

我也是初步学习前端,正打算试试呢,这是很好的参考,谢谢

查看全部评论