支持IE8的纯css3开发的响应式设计动画菜单好代码教程

全世界只有一个自己,所以没必要去当别人生命的插曲。烈日当空,道路两旁,成熟的谷物在热得弯下腰,低着头。蚱蜢多得像。

  这是一个响应式设计的菜单。单击列表图标,当你显示屏大小可以完全水平放下所有菜单项时,菜单水平显示(如图1)。当你的显示屏不能水平放置所有菜单项时,菜单垂直显示(如图2)。 而且显示的时候是以动画的型式显示。效果相当的好。

图1

图2

  下面是实现的代码。

  html代码:

XML/HTML Code复制内容到剪贴板
  1. <divclass="container">
  2. <!--[iflteIE8]>
  3. <style>
  4. .iconicmenu>label{
  5. border-width:7px;
  6. background:#eee;
  7. }
  8. .iconicmenu:hoverul{
  9. left:8px;/*showmenuonmouseoverinIE8andbelow*/
  10. }
  11. </style>
  12. <![endif]-->
  13. <divclass="iconicmenu">
  14. <inputtype="checkbox"id="togglebox"/>
  15. <ul>
  16. <li><atarge="_blank"rel="nofollow noopener noreferrer" href="https://www.haodaima.com/Shili/css3%E8%8F%9C%E5%8D%95">Home</a></li>
  17. <li><atarge="_blank"rel="nofollow noopener noreferrer" href="https://www.haodaima.com/Shili/css3%E8%8F%9C%E5%8D%95">DHTML</a></li>
  18. <li><atarge="_blank"rel="nofollow noopener noreferrer" href="https://www.haodaima.com/Shili/css3%E8%8F%9C%E5%8D%95">CSSLibrary</a></li>
  19. <li><atarge="_blank"rel="nofollow noopener noreferrer" href="https://www.haodaima.com/Shili/css3%E8%8F%9C%E5%8D%95">CSSGallery</a></li>
  20. <li><atarge="_blank"rel="nofollow noopener noreferrer" href="https://www.haodaima.com/Shili/css3%E8%8F%9C%E5%8D%95">JavaScript</a></li>
  21. <li>
  22. <labelfor="togglebox">
  23. </label>
  24. </li>
  25. </ul>
  26. <labelclass="toggler"for="togglebox">
  27. Menu</label>
  28. </div>
  29. </div>

  这里加入了兼容ie8的hack 。

  css代码:

CSS Code复制内容到剪贴板
  1. body
  2. {
  3. padding:0;margin:0;
  4. }
  5. .container
  6. {
  7. width:600px;margin:auto;
  8. }
  9. .iconicmenu{
  10. position:relative;
  11. height:45px;
  12. overflow:hidden;
  13. }
  14. .iconicmenu,.iconicmenu*{
  15. -moz-box-sizing:border-box;
  16. box-sizing:border-box;
  17. }
  18. .iconicmenuinput[type="checkbox"]{/*checkboxusedtotogglemenustate*/
  19. position:absolute;
  20. left:0;
  21. top:0;
  22. opacity:0;
  23. }
  24. .iconicmenu>label{/*Mainlabelicontotogglemenustate*/
  25. z-index:1000;
  26. display:block;
  27. position:absolute;
  28. width:40px;
  29. height:40px;
  30. float:left;
  31. top:0;
  32. left:0;
  33. background:white;
  34. text-indent:-1000000px;
  35. border:6pxsolidblack;/*bordercolor*/
  36. border-width:6px0;
  37. cursor:pointer;
  38. -moz-transition:all0.3sease-in;
  39. -webkit-transition:all0.3sease-in;
  40. transition:all0.3sease-in;/*transitionforflippinglabel*/
  41. }
  42. .iconicmenu>label::after{/*innerstripesinsidelabel*/
  43. content:"";
  44. display:block;
  45. position:absolute;
  46. width:100%;
  47. height:18%;
  48. top:19%;
  49. left:0;
  50. border:6pxsolidblack;/*bordercolor*/
  51. border-width:6px0;
  52. -moz-transition:all0.3sease-in;
  53. -webkit-transition:all0.3sease-in;
  54. transition:all0.3sease-in;/*transitionforflippinglabel*/
  55. }
  56. .iconicmenuul{/*ULmenuinsidecontainer*/
  57. margin:0;
  58. padding:0;
  59. position:absolute;
  60. margin-left:40px;
  61. background:#eee;
  62. left:-100%;/*hidemenuintially*/
  63. height:40px;/*heightofmenu*/
  64. font:bold14pxVerdana;
  65. text-align:center;
  66. list-style:none;
  67. opacity:0;
  68. -moz-border-radius:05px5px0;
  69. -webkit-border-radius:05px5px0;
  70. border-radius:05px5px0;
  71. -moz-perspective:10000px;
  72. perspective:10000px;
  73. -moz-transition:all0.5sease-in;
  74. -webkit-transition:all0.5sease-in;
  75. transition:all0.5sease-in;/*transitionforanimatingULinandout*/
  76. }
  77. .iconicmenuli{
  78. display:inline;
  79. margin:0;
  80. padding:0;
  81. }
  82. .iconicmenuullabel{/*labelbuttoninsideULtoclosemenu*/
  83. cursor:pointer;
  84. position:relative;
  85. height:100%;
  86. text-align:center;
  87. }
  88. .iconicmenuullabel::after{/*labelbuttonx*/
  89. content:"x";
  90. display:inline-block;
  91. line-height:14px;
  92. color:white;
  93. -moz-border-radius:50px;
  94. -webkit-border-radius:50px;
  95. border-radius:50px;
  96. width:20px;
  97. height:20px;
  98. background:black;
  99. font-size:18px;
  100. margin:5px;
  101. margin-top:10px;
  102. -moz-transition:all0.3sease-in;
  103. -webkit-transition:all0.3sease-in;
  104. transition:all0.3sease-in;
  105. }
  106. .iconicmenuinput[type="checkbox"]:checked~label,.iconicmenuullabel:hover::after{
  107. -moz-transform:rotatey(180deg);
  108. -ms-transform:rotatey(180deg);
  109. -webkit-transform:rotatey(180deg);
  110. transform:rotatey(180deg);/*fliplabelsverticallyonMouseover*/
  111. }
  112. .iconicmenu>label:hover,.iconicmenu>label:hover::after,.iconicmenuinput[type="checkbox"]:checked~label,.iconicmenuinput[type="checkbox"]:checked~label::after{
  113. border-color:darkred;/*highlightcolorofmainmenulabelonMouseover*/
  114. }
  115. .iconicmenuinput[type="checkbox"]:checked~ul{
  116. left:8px;/*Animatemenuintoview*/
  117. opacity:1;
  118. -moz-box-shadow:1px1px5pxgray;
  119. -webkit-box-shadow:1px1px5pxgray;
  120. box-shadow:1px1px5pxgray;
  121. }
  122. .iconicmenulia{
  123. display:block;
  124. float:left;
  125. text-align:center;
  126. text-decoration:none;
  127. color:black;
  128. margin:0;
  129. padding:10px;
  130. padding-right:15px;
  131. height:100%;
  132. }
  133. .iconicmenulia:hover{
  134. background:black;
  135. color:white;
  136. }
  137. /*-----------------------------CSSMediaQueries-----------------------------*/
  138. /*
  139. Theserulescontrolwhichportionsofthemenugetsshownwhenthescreensizeisbelowacertainwidth.
  140. Bydefault2stagesaredefineddependingonbrowserscreenwidth.
  141. */
  142. @mediascreenand(max-width:580px){/*Hidetoggleiconwhenmenuisalreadyopen(increasesusablemenuspaceby40px)*/
  143. .iconicmenuinput[type="checkbox"]:checked~label{
  144. display:none;
  145. }
  146. .iconicmenuinput[type="checkbox"]:checked~ul{
  147. margin-left:0;
  148. }
  149. }
  150. @mediascreenand(max-width:560px){/*Converthorizontalmenutoverticaldropdowninstead(friendlyacrossallscreensizes)*/
  151. .iconicmenu{
  152. overflow:visible;
  153. }
  154. .iconicmenuul{
  155. height:auto;
  156. }
  157. .iconicmenuulli{
  158. min-width:200px;;
  159. display:block;
  160. }
  161. .iconicmenuullia{
  162. float:none;;
  163. text-align:left;
  164. }
  165. }

 以上就是一个纯用css3实现的菜单,是不是很简单呢,谢谢阅读,希望能帮到大家,请继续关注,我们会努力分享更多优秀的文章。