美文网首页
下滑动画菜单Demo

下滑动画菜单Demo

作者: 小y哥哥 | 来源:发表于2016-07-06 17:26 被阅读91次
    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>动画菜单Demo</title>
        <style type="text/css">
    * { margin: 0; padding: 0; font-size: 14px; }
    a { color: #333; text-decoration: none }
    ul{ list-style: none; }
    .nav {height: 30px; border-bottom: 5px solid #F60; margin-left:50px; width:600px;}
    .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:0px; overflow:hidden}
    .subNav li a{ background:#ddd }
    .subNav li a:hover{ background:#efefef}
    </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">
            <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>
        <li><a href="#">一级菜单</a></li>
        <li><a href="#">一级菜单</a></li>
        <li><a href="#">一级菜单</a></li>
    </ul>
    </body>
    </html>
    

    特别鸣谢 imooc

    相关文章

      网友评论

          本文标题:下滑动画菜单Demo

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