首页 > 开发 > CSS > 正文

CSS3实现鼠标悬停显示扩展内容

2024-07-11 08:32:31
字体:
来源:转载
供稿:网友

我们在做导航标签的时候,有时会出现空间过于拥挤需要隐藏部分内容的情况,所以在这里我自己写了一个鼠标悬停显示扩展内容的效果,如下图所示。
 


总的来说效果还是比较好实现,但是比较头疼的是三角部分使用了伪元素::after,而对父元素设置 over-flow:hidden 时也会把伪元素给隐藏掉。最后想的办法是把文字和图标用一个 <span> 包裹住然后对其设置over-flow属性。

HTML代码:

CSS Code复制内容到剪贴板
  1.     <div id="nav">           <a id="nav-main"><span><i class="icon-home"></i> 主界面</span></a>   
  2.          <a id="nav-sum"><span><i class="icon-laptop"></i> 统计界面</span></a>       </div>    
  3.    CSS代码:   
  4.    /*******************************************************************************/  
  5. /*********************************** nav **************************************/   /*******************************************************************************/  
  6. #nav{        box-sizing:border-box;   
  7.     width:200px;        height:100%;   
  8.     position:fixed;        padding-top:80px;   
  9. }    #nav a{   
  10.     display:block;        width:30px;   
  11.     height:52px;        position:relative;   
  12.     margin-top:50px;    }   
  13. #nav a span{        display:inline-block;   
  14.     width:46px;        height:50px;   
  15.     font-size:1em;        font-weight:600;   
  16.     color:rgba(255,255,255,0.9);        text-indent:3px;   
  17.     line-height:52px;        cursor:pointer;   
  18.     overflow:hidden;    }   
  19. #nav a span i{        font-size:1.3em;   
  20. }    #nav a::after{   
  21.     content:'';        display:block;   
  22.     width:0;        height:0;   
  23.     position:absolute;        rightright:-32px;   
  24.     bottombottom:0;        border-top:26px solid transparent;   
  25.     border-right:16px solid transparent;        border-bottom:26px solid transparent;   
  26. }    #nav-main{   
  27.     background-color:rgb(211,83,80);    }   
  28. #nav-sum{        background-color:rgb(0,158,163);   
  29. }    #nav-main::after{   
  30.     border-left:16px solid rgb(211,83,80);    }   
  31. #nav-sum::after{        border-left:16px solid rgb(0,158,163);   
  32. }    #nav a:hover{   
  33.     -webkit-animation:extend-a 0.5s;        -moz-animation:extend-a 0.5s;   
  34.     animation:extend-a 0.5s;        width:100px;   
  35. }    #nav a span:hover{   
  36.     -webkit-animation:extend-span 0.5s;        -moz-animation:extend-span 0.5s;   
  37.     animation:extend-span 0.5s;        width:116px;   
  38. }    /******************* a扩展效果 ******************/  
  39. @-webkit-keyframes extend-a{        0% {   
  40.         width:30px;        }   
  41.     100% {            width:100px;   
  42.     }    }   
  43. @-moz-keyframes extend-a{        0% {   
  44.         width:30px;        }   
  45.     100% {            width:100px;   
  46.     }    }   
  47. @keyframes extend-a{        0% {   
  48.         width:30px;        }   
  49.     100% {            width:100px;   
  50.     }    }   
  51. /******************* span扩展效果 ******************/   @-webkit-keyframes extend-span{   
  52.     0% {            width:46px;   
  53.     }        100% {   
  54.         width:116px;        }   
  55. }    @-moz-keyframes extend-span{   
  56.     0% {            width:46px;   
  57.     }        100% {   
  58.         width:116px;        }   
  59. }    @keyframes extend-span{   
  60.     0% {            width:46px;   
  61.     }        100% {   
  62.         width:116px;        }   
  63. }  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表