猿问

如何实现导航菜单分割线的动态显示

http://www.imooc.com/code/1881

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>CSS制作立体导航</title>
        <link rel="stylesheet" href="http://www.w3cplus.com/demo/css3/base.css">
        <style>
            body{
              background: #ebebeb;
            }
            .nav{
              width:560px;
              height: 50px;
              font:bold 0/50px Arial;
              text-align:center;
              margin:40px auto 0;
              background: #f65f57;
             /*制作导航圆角*/
              border-radius: 8px;
             /*制作导航立体效果*/
              box-shadow: 0 7px 0 #ba4a45;
            }
            .nav a{
              display: inline-block;
                -webkit-transition: all 0.2s ease-in;
                -moz-transition: all 0.2s ease-in;
               -o-transition: all 0.2s ease-in;
               -ms-transition: all 0.2s ease-in;
               transition: all 0.2s ease-in;
            }
            .nav a:hover{
              -webkit-transform:rotate(10deg);
              -moz-transform:rotate(10deg);
              -o-transform:rotate(10deg);
              -ms-transform:rotate(10deg);
              transform:rotate(10deg);
            }

            .nav li{
              position:relative;
              display:inline-block;
              padding:0 16px;
              font-size: 13px;
              text-shadow:1px 2px 4px rgba(0,0,0,.5);
              list-style: none outside none;
            }
            /*制作导航分隔线效果*/
            .nav li::before,
            .nav li::after{
              content:"";
              position:absolute;
              top:14px;
              height: 25px;
              width: 1px;
            }
            .nav li::after{
              right: 0;
              background: -moz-linear-gradient(top, rgba(255,255,255,0), rgba(255,255,255,.2) 50%, rgba(255,255,255,0));
              background: -webkit-linear-gradient(top, rgba(255,255,255,0), rgba(255,255,255,.2) 50%, rgba(255,255,255,0));
              background: -o-linear-gradient(top, rgba(255,255,255,0), rgba(255,255,255,.2) 50%, rgba(255,255,255,0));
              background: -ms-linear-gradient(top, rgba(255,255,255,0), rgba(255,255,255,.2) 50%, rgba(255,255,255,0));
              background: linear-gradient(top, rgba(255,255,255,0), rgba(255,255,255,.2) 50%, rgba(255,255,255,0));
            }
            .nav li::before{
              left: 0;
              background: -moz-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);
              background: -webkit-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);
              background: -o-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);
              background: -ms-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);
              background: linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);
            }
            /*删除导航第一个导航项左边的分隔线*/
            .nav a,
            .nav a:hover{
              color:#fff;
              text-decoration: none;
            }
        </style>
    </head>
    <body>
        <ul>
        <li><a href="">Home</a></li>
        <li><a href="">About Me</a></li>
        <li><a href="">Portfolio</a></li>
        <li><a href="">Blog</a></li>
        <li><a href="">Resources</a></li>
        <li><a href="">Contact Me</a></li>
        </ul>
    </body>
</html>

这是所给的源代码

代码中可见,before与after两个伪元素颜色不一样,前面一个li的after与下一个li的before组合成为一条分割线。

如果用li:hover伪类实现的话,只能显示当前li的after和before伪元素

如果我要实现两边都有分割线出现的效果,该怎样?

难点:

之前做过分割线效果利用空元素的border来实现,但是无法实现渐变效果

如果通过建立伪元素进行background渐变的方式,无法实现两条线同时显示。

FreeGloye
浏览 2085回答 2
2回答

penglijuan

你这种情况就是属于本来只有6个导航项 但是要显示7条分割线 所以可以在nav后面加个伪类.nav:after{  right:27px;                  background: -moz-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);                  background: -webkit-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);                  background: -o-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);                  background: -ms-linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a);                  background: linear-gradient(top, #ff625a, #9e3e3a 50%, #ff625a); }

小宇没宙了

.nav li:first-child::before{              display: none;             } .nav li:last-child::after{               display: none;             }去掉两边的也挺好,反正没东西~~已参考下
随时随地看视频慕课网APP
我要回答