当前位置:Gxlcms > html代码 > CSS:响应式下的折叠菜单(条纹式)_html/css_WEB-ITnose

CSS:响应式下的折叠菜单(条纹式)_html/css_WEB-ITnose

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

原文: CSS: Responsive Navigation Menu

译文:CSS:响应式导航菜单

译者:dwqs

写在之前,关于如何制作响应式的下拉菜单:响应式下的下拉菜单

之前,我写了一篇关于怎么制作响应式的移动导航的教程。现在,我发现了一个新技巧,不用使用JavaScript就能实现响应式菜单,而是使用HTML 5清晰的语义标记。

这个菜单能居左、居中和居右对齐,不像之前文章提到的菜单需要靠点击在显示和隐藏之间进行切换。它也有一个标记显示激活/当前菜单项,并且它能作用在所有移动平台和包括IE在内的所有桌面浏览器。

首先,看看Demo的效果:Demo

一、 目的

本教程的目的是展示如何将规律的列表菜单变成一个较小的显示下拉菜单。

这个技巧在由许多链接组成的导航上非常有用,例如下面的截图,可以将所有按钮压缩成一个优雅的下拉。

二、Nav HTML标记

  1. <nav class="nav"> <ul> <li class="current">Portfolio</li> <li>Illustration</li> <li>Web Design</li> <li>Print Media</li> <li>Graphic Design</li> </ul></nav>

三、CSS

  1. /* nav */.nav { position: relative; margin: 20px 0;}.nav ul { margin: 0; padding: 0;}.nav li { margin: 0 5px 10px 0; padding: 0; list-style: none; display: inline-block;}.nav a { padding: 3px 12px; text-decoration: none; color: #999; line-height: 100%;}.nav a:hover { color: #000;}.nav .current a { background: #999; color: #fff; border-radius: 5px;}

四、居中和右对齐

正如前面提到的,可以同过text-align属性改变对齐方式。

  1. /* right nav */.nav.right ul { text-align: right;}/* center nav */.nav.center ul { text-align: center;}

五、IE的支持

在IE 8以及更早版本中,是不支持HTML5的nav标签和媒体查询。引入css3-mediaqueries.js(或 respond.js) 和 html5shim.js 提供后备的兼容处理。如果你不想用html5shim.js,可以用div代替nav

  1. <!--[if lt IE 9]> <script src="http://css3-mediaqueries-js.googlecode.com/files/css3-mediaqueries.js"></ script> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></ script><![endif]?></script></pre> </p> <p><strong>六、响应式</strong></p> <p> 现在有趣的部分来了??媒体查询菜单的响应性。如果你对响应式设计不熟悉,可以阅读之前我写的关于responsive design (后续翻译)and media query的文章。</p> <p> 在600 px断点,我把nav元素设置成相对定位,这样,我就能对ul元素进行绝对定位了。通过display:none,我隐藏了所有的li元素,但保持当前的li元素以块元素显示。然后,当鼠标悬浮于nav元素时,将所有li添加display:block.为了标记当前的li元素,我会添加一个图标。对于居中和右对齐,对定位的ul元素使用left和right属性。看看最终的Demo效果:</p> <p class="sycode"> <pre class="brush:php;toolbar:false">@media screen and (max-width: 600px) { .nav { position: relative; min-height: 40px; } .nav ul { width: 180px; padding: 5px 0; position: absolute; top: 0; left: 0; border: solid 1px #aaa; background: #fff url(images/icon-menu.png) no-repeat 10px 11px; border-radius: 5px; box-shadow: 0 1px 2px rgba(0,0,0,.3); } .nav li { display: none; /* hide all <li> items */ margin: 0; } .nav .current { display: block; /* show only current <li> item */ } .nav a { display: block; padding: 5px 5px 5px 32px; text-align: left; } .nav .current a { background: none; color: #666; } /* on nav hover */ .nav ul:hover { background-image: none; } .nav ul:hover li { display: block; margin: 0 0 5px; } .nav ul:hover .current { background: url(images/icon-check.png) no-repeat 10px 7px; } /* right nav */ .nav.right ul { left: auto; right: 0; } /* center nav */ .nav.center ul { left: 50%; margin-left: -90px; } }</script></pre> </p> <p> 相关博文推荐:</p> <h3> 10个响应式设计的导航菜单源码:http://www.shejidaren.com/10-responsive-navigation-tutorials.html</h3> <h3>Creating a CSS3 Responsive Menu:http://www.onextrapixel.com/2012/08/27/creating-a-css3-responsive-menu/</h3> Animenu响应式导航设计教程: http://red-team-design.com/animenu-a-responsive-dropdown-navigation-made-with-sass/ <p> Demo:http://red-team-design.com/wp-content/uploads/2013/02/animenu-demo.html</p> <p><br /> </p> <p>原文首发: </div>
  2. <div class="">
  3. <ul class="m-news-opt fix">
  4. <li class="opt-item">
  5. <a href='/htmldaima-272179.html' target='_blank'><p>< 上一篇</p><p class="ellipsis">大家都用什么html编辑器_html/css_WEB-ITnose</p></a>
  6. </li>
  7. <li class="opt-item ta-r">
  8. <a href='/htmldaima-272181.html' target='_blank'><p>下一篇 ></p><p class="ellipsis">IE无法调整那些使用px作为单位的字体大小_html/css_WEB-ITnose</p></a>
  9. </li>
  10. </ul>
  11. </div>
  12. </div>
  13. </div>
  14. <div class="g-title fix">
  15. <h2 class="title-txt">人气教程排行</h2>
  16. </div>
  17. <div class="m-rank u-dashed mb40">
  18. <ul>
  19. <li class="rank-item">
  20. <a href="/htmldaima-274978.html" title='如何生成一个调查问卷_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  21. <span class="g-art-count fr">230次</span>
  22. <span class="g-sort-num top">1</span>
  23. 如何生成一个调查问卷_html/css_WEB-ITnose </a>
  24. </li>
  25. <li class="rank-item">
  26. <a href="/htmldaima-274080.html" title='在页面直接显示日历_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  27. <span class="g-art-count fr">230次</span>
  28. <span class="g-sort-num second">2</span>
  29. 在页面直接显示日历_html/css_WEB-ITnose </a>
  30. </li>
  31. <li class="rank-item">
  32. <a href="/htmldaima-262106.html" title='如何点击a标签实现弹出inputfile上传文件对话框_HTML/Xhtml_网页制作' class="item-name ellipsis" target="_blank">
  33. <span class="g-art-count fr">230次</span>
  34. <span class="g-sort-num third">3</span>
  35. 如何点击a标签实现弹出inputfile上传文件对话框_HTML/Xhtml_网页制作 </a>
  36. </li>
  37. <li class="rank-item">
  38. <a href="/htmldaima-278863.html" title='关于列表标记的详细介绍' class="item-name ellipsis" target="_blank">
  39. <span class="g-art-count fr">229次</span>
  40. <span class="g-sort-num ">4</span>
  41. 关于列表标记的详细介绍 </a>
  42. </li>
  43. <li class="rank-item">
  44. <a href="/htmldaima-277331.html" title='cssborder-bottom(指定下边线的样式、宽度及颜色)' class="item-name ellipsis" target="_blank">
  45. <span class="g-art-count fr">229次</span>
  46. <span class="g-sort-num ">5</span>
  47. cssborder-bottom(指定下边线的样式、宽度及颜色) </a>
  48. </li>
  49. <li class="rank-item">
  50. <a href="/htmldaima-270548.html" title='html新闻详情页_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  51. <span class="g-art-count fr">229次</span>
  52. <span class="g-sort-num ">6</span>
  53. html新闻详情页_html/css_WEB-ITnose </a>
  54. </li>
  55. <li class="rank-item">
  56. <a href="/htmldaima-268070.html" title='为何页面内容和网页边缘有空隙_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  57. <span class="g-art-count fr">229次</span>
  58. <span class="g-sort-num ">7</span>
  59. 为何页面内容和网页边缘有空隙_html/css_WEB-ITnose </a>
  60. </li>
  61. <li class="rank-item">
  62. <a href="/htmldaima-269054.html" title='position:fixed定位时“高度坍塌”问题的解决_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  63. <span class="g-art-count fr">228次</span>
  64. <span class="g-sort-num ">8</span>
  65. position:fixed定位时“高度坍塌”问题的解决_html/css_WEB-ITnose </a>
  66. </li>
  67. <li class="rank-item">
  68. <a href="/htmldaima-280863.html" title='htmlp标签怎么换行?htmlp标签添加br换行标签的应用' class="item-name ellipsis" target="_blank">
  69. <span class="g-art-count fr">227次</span>
  70. <span class="g-sort-num ">9</span>
  71. htmlp标签怎么换行?htmlp标签添加br换行标签的应用 </a>
  72. </li>
  73. <li class="rank-item">
  74. <a href="/htmldaima-281519.html" title='HTML的<!DOCTYPE>标签' class="item-name ellipsis" target="_blank">
  75. <span class="g-art-count fr">226次</span>
  76. <span class="g-sort-num ">10</span>
  77. HTML的<!DOCTYPE>标签 </a>
  78. </li>
  79. <li class="rank-item">
  80. <a href="/htmldaima-280725.html" title='html页面中友情链接怎么进行添加设置?(代码示例)' class="item-name ellipsis" target="_blank">
  81. <span class="g-art-count fr">226次</span>
  82. <span class="g-sort-num ">11</span>
  83. html页面中友情链接怎么进行添加设置?(代码示例) </a>
  84. </li>
  85. <li class="rank-item">
  86. <a href="/htmldaima-278651.html" title='form表单中属性及功能应用介绍汇总' class="item-name ellipsis" target="_blank">
  87. <span class="g-art-count fr">226次</span>
  88. <span class="g-sort-num ">12</span>
  89. form表单中属性及功能应用介绍汇总 </a>
  90. </li>
  91. <li class="rank-item">
  92. <a href="/htmldaima-278586.html" title='详解form标签中的method属性' class="item-name ellipsis" target="_blank">
  93. <span class="g-art-count fr">226次</span>
  94. <span class="g-sort-num ">13</span>
  95. 详解form标签中的method属性 </a>
  96. </li>
  97. <li class="rank-item">
  98. <a href="/htmldaima-277758.html" title='HTML5Canvas逼真烟雾效果js插件' class="item-name ellipsis" target="_blank">
  99. <span class="g-art-count fr">226次</span>
  100. <span class="g-sort-num ">14</span>
  101. HTML5Canvas逼真烟雾效果js插件 </a>
  102. </li>
  103. <li class="rank-item">
  104. <a href="/htmldaima-275669.html" title='页面跳转特效_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  105. <span class="g-art-count fr">226次</span>
  106. <span class="g-sort-num ">15</span>
  107. 页面跳转特效_html/css_WEB-ITnose </a>
  108. </li>
  109. <li class="rank-item">
  110. <a href="/htmldaima-271209.html" title='改变鼠标选中区域的样式。_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  111. <span class="g-art-count fr">226次</span>
  112. <span class="g-sort-num ">16</span>
  113. 改变鼠标选中区域的样式。_html/css_WEB-ITnose </a>
  114. </li>
  115. <li class="rank-item">
  116. <a href="/htmldaima-273428.html" title='关于$("body").append()一段html代码,在页面写能触发事件,写在js文件里写就没有_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  117. <span class="g-art-count fr">225次</span>
  118. <span class="g-sort-num ">17</span>
  119. 关于$("body").append()一段html代码,在页面写能触发事件,写在js文件里写就没有_html/css_WEB-ITnose </a>
  120. </li>
  121. <li class="rank-item">
  122. <a href="/htmldaima-271182.html" title='CSS3悬浮动画效果_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  123. <span class="g-art-count fr">225次</span>
  124. <span class="g-sort-num ">18</span>
  125. CSS3悬浮动画效果_html/css_WEB-ITnose </a>
  126. </li>
  127. <li class="rank-item">
  128. <a href="/htmldaima-271093.html" title='纯C语言实现的CSS解析器:katana_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  129. <span class="g-art-count fr">225次</span>
  130. <span class="g-sort-num ">19</span>
  131. 纯C语言实现的CSS解析器:katana_html/css_WEB-ITnose </a>
  132. </li>
  133. <li class="rank-item">
  134. <a href="/htmldaima-267953.html" title='body在默认情况下是具有margin外边距的_html/css_WEB-ITnose' class="item-name ellipsis" target="_blank">
  135. <span class="g-art-count fr">225次</span>
  136. <span class="g-sort-num ">20</span>
  137. body在默认情况下是具有margin外边距的_html/css_WEB-ITnose </a>
  138. </li>
  139. </ul>
  140. </div>
  141. </div>
  142. </div>
  143. <!-- / 教程内容页 -->