当前位置:Gxlcms > css > CSS3实现鼠标悬停显示扩展内容

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

时间:2021-07-01 10:21:17 帮助过:149人阅读

本文给大家分享css3代码实现鼠标悬停显示要扩展的内容,在空间过于拥挤时需要隐藏部分内容使用此功能比较好,下面小编给带来了具体实现代码,一起看看吧

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

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

HTML代码:

  1. <p id="nav">
  2. <a id="nav-main"><span><i class="icon-home"></i> 主界面</span></a>
  3. <a id="nav-sum"><span><i class="icon-laptop"></i> 统计界面</span></a>
  4. </p>
  5. CSS代码:
  6. /*******************************************************************************/
  7. /*********************************** nav **************************************/
  8. /*******************************************************************************/
  9. #nav{
  10. box-sizing:border-box;
  11. width:200px;
  12. height:100%;
  13. position:fixed;
  14. padding-top:80px;
  15. }
  16. #nav a{
  17. display:block;
  18. width:30px;
  19. height:52px;
  20. position:relative;
  21. margin-top:50px;
  22. }
  23. #nav a span{
  24. display:inline-block;
  25. width:46px;
  26. height:50px;
  27. font-size:1em;
  28. font-weight:600;
  29. color:rgba(255,255,255,0.9);
  30. text-indent:3px;
  31. line-height:52px;
  32. cursor:pointer;
  33. overflow:hidden;
  34. }
  35. #nav a span i{
  36. font-size:1.3em;
  37. }
  38. #nav a::after{
  39. content:'';
  40. display:block;
  41. width:0;
  42. height:0;
  43. position:absolute;
  44. rightright:-32px;
  45. bottombottom:0;
  46. border-top:26px solid transparent;
  47. border-right:16px solid transparent;
  48. border-bottom:26px solid transparent;
  49. }
  50. #nav-main{
  51. background-color:rgb(211,83,80);
  52. }
  53. #nav-sum{
  54. background-color:rgb(0,158,163);
  55. }
  56. #nav-main::after{
  57. border-left:16px solid rgb(211,83,80);
  58. }
  59. #nav-sum::after{
  60. border-left:16px solid rgb(0,158,163);
  61. }
  62. #nav a:hover{
  63. -webkit-animation:extend-a 0.5s;
  64. -moz-animation:extend-a 0.5s;
  65. animation:extend-a 0.5s;
  66. width:100px;
  67. }
  68. #nav a span:hover{
  69. -webkit-animation:extend-span 0.5s;
  70. -moz-animation:extend-span 0.5s;
  71. animation:extend-span 0.5s;
  72. width:116px;
  73. }
  74. /******************* a扩展效果 ******************/
  75. @-webkit-keyframes extend-a{
  76. 0% {
  77. width:30px;
  78. }
  79. 100% {
  80. width:100px;
  81. }
  82. }
  83. @-moz-keyframes extend-a{
  84. 0% {
  85. width:30px;
  86. }
  87. 100% {
  88. width:100px;
  89. }
  90. }
  91. @keyframes extend-a{
  92. 0% {
  93. width:30px;
  94. }
  95. 100% {
  96. width:100px;
  97. }
  98. }
  99. /******************* span扩展效果 ******************/
  100. @-webkit-keyframes extend-span{
  101. 0% {
  102. width:46px;
  103. }
  104. 100% {
  105. width:116px;
  106. }
  107. }
  108. @-moz-keyframes extend-span{
  109. 0% {
  110. width:46px;
  111. }
  112. 100% {
  113. width:116px;
  114. }
  115. }
  116. @keyframes extend-span{
  117. 0% {
  118. width:46px;
  119. }
  120. 100% {
  121. width:116px;
  122. }
  123. }

其中图标使用的是 font-awesome 提供的API,使用时引入它的css文件即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

CSS制作图形变形弹出的效果

CSS3和jQuery实现跟随鼠标方位的Hover特效

CSS实现自适应宽度的菜单按钮效果

以上就是CSS3实现鼠标悬停显示扩展内容的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行