css导航栏

几个导航栏也算对学过知识的回顾,总有新的收获,下面是学习过程中敲的代码:

 <!DOCTYPE HTML>  
<html>  
<head>  
    <title></title>  
    <meta charset="utf-8"/>  
    <style>
   

    ul{
        list-style-type: none;
        width: 100px;
    }
    a{
        display: block;     /*设为block修改其样式,而不是修改li标签*/
        text-decoration:none;
        background-color:#ccc;
        height: 30px;
        line-height: 30px;
        width: 100px;
        margin-bottom: 1px;
        text-indent:20px;
    }
    li{

    }
    a:hover{
        background-color:#f60;
        color:#fff; 
    }


    </style>  
</head>  
<body>  
  <ul>
    <li><a href='#'>首页</a></li>
    <li><a href='#'>新闻</a></li>
    <li><a href='#'>咨询</a></li>
    <li><a href='#'>服务</a></li>
    <li><a href='#'>关于</a></li>
   </ul>
</body>  
</html> 
下面css代码使其水平导航:

ul{
        list-style-type: none;
        
    }
    a{
        display: block;/*设为block修改其样式,而不是修改li标签*/
        text-decoration:none;
        background-color:#ccc;
        height: 30px;
        line-height: 30px;
        width: 100px;
        margin-bottom: 1px;
        text-align:center;
    }
    li{
        float:left;
    }
    a:hover{
        background-color:#f60;
        color:#fff; 
    }

下面再次改进为伸缩效果:

ul{
        list-style-type: none;
        height:30px;
        border-bottom:3px solid #f60;
        margin:0 auto;
        width: 800px;
        padding-left: 200px;
    }
    a{
        display: block;/*设为block修改其样式,而不是修改li标签*/
        text-decoration:none;
        background-color:#ccc;
        height: 30px;
        line-height: 30px;
        width: 100px;
        margin-bottom: 1px;
        text-align:center;
        border-radius:10px 10px 0 0; 

    }
    li{
        float:left;
       width: 100px;
        margin: 0 auto;
    }
    a:hover{
        background-color:#f60;
        color:#fff; 
        height:40px;
        margin-top:-10px;
        line-height: 40px;
    }
改进后效果如下:

下面用js改进,实现鼠标放上去变宽效果:

 <!DOCTYPE HTML>  
<html>  
<head>  
    <title></title>  
    <meta charset="utf-8"/>  
    <style>
   

    ul{
        list-style-type: none;
        height:30px;
        border-bottom:3px solid #f60;
        margin:0 auto;
        width: 800px;
        padding-left: 200px;
    }
    a{
        display: block;/*设为block修改其样式,而不是修改li标签*/
        text-decoration:none;
        background-color:#ccc;
        height: 30px;
        line-height: 30px;
        width: 100px;
        margin-bottom: 1px;
        text-align:center;
        border-radius:10px 10px 0 0; 

    }
    li{
        float:left;
       width: 100px;
        margin: 0 auto;
    }
    a:hover{
        background-color:#f60;
        color:#fff; 
        height:40px;
        margin-top:-10px;
        line-height: 40px;
    }


    </style>  
    <script>
    window.οnlοad=function(){
        var oa=document.getElementsByTagName('a');
        for(var i=0;i<oa.length;i++)
        {
            
            oa[i].οnmοuseοver=function(){
                var that=this;
                that.time=setInterval(function(){

                    that.style.width=that.offsetWidth+8+'px';
                    if(that.offsetWidth>=120)
                    {
                        clearInterval(that.time);
                    }
                },30);
            }

            oa[i].οnmοuseοut=function(){
                var that=this;
                that.time=setInterval(function(){

                    that.style.width=that.offsetWidth-8+'px';
                    if(that.offsetWidth<=120)
                    {
                        that.style=width='120px';
                        clearInterval(that.time);
                    }
                },30);
            }
        }
    }
    </script>
</head>  
<body>  
  <ul>
    <li><a href='#'>首页</a></li>
    <li><a href='#'>新闻</a></li>
    <li><a href='#'>咨询</a></li>
    <li><a href='#'>服务</a></li>
    <li><a href='#'>关于</a></li>
   </ul>
</body>  
</html> 

效果:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值