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

所属分类: 网页制作 / CSS 阅读数: 283
收藏 0 赞 0 分享

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

图1

图2

  下面是实现的代码。

  html代码:

XML/HTML Code复制内容到剪贴板
  1. <div class="container">  
  2.         <!--[if lte IE 8]>  
  3. <style>  
  4.   
  5.         .iconicmenu > label{   
  6.         border-width: 7px;   
  7.         background: #eee;   
  8.         }   
  9.            
  10.         .iconicmenu:hover ul{   
  11.             left: 8px; /* show menu onmouseover in IE8 and below */   
  12.         }   
  13.   
  14. </style>  
  15. <![endif]-->  
  16.         <div class="iconicmenu">  
  17.             <input type="checkbox" id="togglebox" />  
  18.             <ul>  
  19.                 <li><a targe="_blank" href="https://www.jb51.net/Shili/css3%E8%8F%9C%E5%8D%95">Home</a></li>  
  20.                 <li><a targe="_blank" href="https://www.jb51.net/Shili/css3%E8%8F%9C%E5%8D%95">DHTML</a></li>  
  21.                 <li><a targe="_blank" href="https://www.jb51.net/Shili/css3%E8%8F%9C%E5%8D%95">CSS Library</a></li>  
  22.                 <li><a targe="_blank" href="https://www.jb51.net/Shili/css3%E8%8F%9C%E5%8D%95">CSS Gallery</a></li>  
  23.                 <li><a targe="_blank" href="https://www.jb51.net/Shili/css3%E8%8F%9C%E5%8D%95">JavaScript</a></li>  
  24.                 <li>  
  25.                     <label for="togglebox">  
  26.                     </label>  
  27.                 </li>  
  28.             </ul>  
  29.             <label class="toggler" for="togglebox">  
  30.                 Menu</label>  
  31.         </div>  
  32.     </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.     positionrelative;   
  11.     height45px;   
  12.     overflowhidden;   
  13.     }   
  14.   
  15. .iconicmenu, .iconicmenu * {   
  16.     -moz-box-sizing: border-box;   
  17.     box-sizing: border-box;   
  18.     }   
  19.   
  20. .iconicmenu input[type="checkbox"] { /* checkbox used to toggle menu state */  
  21.     positionabsolute;   
  22.     left: 0;   
  23.     top: 0;   
  24.     opacity: 0;   
  25.     }   
  26.   
  27. .iconicmenu > label { /* Main label icon to toggle menu state */  
  28.     z-index: 1000;   
  29.     displayblock;   
  30.     positionabsolute;   
  31.     width40px;   
  32.     height40px;   
  33.     floatleft;   
  34.     top: 0;   
  35.     left: 0;   
  36.     backgroundwhite;   
  37.     text-indent: -1000000px;   
  38.     border6px solid black/* border color */  
  39.     border-width6px 0;   
  40.     cursorpointer;   
  41.     -moz-transition: all 0.3s ease-in;   
  42.     -webkit-transition: all 0.3s ease-in;   
  43.     transition: all 0.3s ease-in; /* transition for flipping label */  
  44.     }   
  45.   
  46. .iconicmenu > label::after { /* inner stripes inside label */  
  47.     content"";   
  48.     displayblock;   
  49.     positionabsolute;   
  50.     width: 100%;   
  51.     height: 18%;   
  52.     top: 19%;   
  53.     left: 0;   
  54.     border6px solid black/* border color */  
  55.     border-width6px 0;   
  56.     -moz-transition: all 0.3s ease-in;   
  57.     -webkit-transition: all 0.3s ease-in;   
  58.     transition: all 0.3s ease-in; /* transition for flipping label */  
  59.     }   
  60.   
  61. .iconicmenu ul { /* UL menu inside container */  
  62.     margin: 0;   
  63.     padding: 0;   
  64.     positionabsolute;   
  65.     margin-left40px;   
  66.     background#eee;   
  67.     left: -100%; /* hide menu intially */  
  68.     height40px/* height of menu */  
  69.     fontbold 14px Verdana;   
  70.     text-aligncenter;   
  71.     list-stylenone;   
  72.     opacity: 0;   
  73.     -moz-border-radius: 0 5px 5px 0;   
  74.     -webkit-border-radius: 0 5px 5px 0;   
  75.     border-radius: 0 5px 5px 0;   
  76.     -moz-perspective: 10000px;   
  77.     perspective: 10000px;   
  78.     -moz-transition: all 0.5s ease-in;   
  79.     -webkit-transition: all 0.5s ease-in;   
  80.     transition: all 0.5s ease-in; /* transition for animating UL in and out */  
  81.     }   
  82.   
  83. .iconicmenu li {   
  84.     displayinline;   
  85.     margin: 0;   
  86.     padding: 0;   
  87.     }   
  88.   
  89. .iconicmenu ul label { /* label button inside UL to close menu */  
  90.     cursorpointer;   
  91.     positionrelative;   
  92.     height: 100%;   
  93.     text-aligncenter;   
  94.     }   
  95.   
  96. .iconicmenu ul label::after { /* label button x */  
  97.     content"x";   
  98.     displayinline-block;   
  99.     line-height14px;   
  100.     colorwhite;   
  101.     -moz-border-radius: 50px;   
  102.     -webkit-border-radius: 50px;   
  103.     border-radius: 50px;   
  104.     width20px;   
  105.     height20px;   
  106.     backgroundblack;   
  107.     font-size18px;   
  108.     margin5px;   
  109.     margin-top10px;   
  110.     -moz-transition: all 0.3s ease-in;   
  111.     -webkit-transition: all 0.3s ease-in;   
  112.     transition: all 0.3s ease-in;   
  113.     }   
  114.   
  115. .iconicmenu input[type="checkbox"]:checked ~ label, .iconicmenu ul label:hover::after {   
  116.     -moz-transform: rotatey(180deg);   
  117.     -ms-transform: rotatey(180deg);   
  118.     -webkit-transform: rotatey(180deg);   
  119.     transform: rotatey(180deg); /* flip labels vertically onMouseover */  
  120.     }   
  121.   
  122. .iconicmenu > label:hover, .iconicmenu > label:hover::after, .iconicmenu input[type="checkbox"]:checked ~ label, .iconicmenu input[type="checkbox"]:checked ~ label::after {   
  123.     border-color: darkred; /* highlight color of main menu label onMouseover */  
  124.     }   
  125.   
  126. .iconicmenu input[type="checkbox"]:checked ~ ul {   
  127.     left8px/* Animate menu into view */  
  128.     opacity: 1;   
  129.     -moz-box-shadow: 1px 1px 5px gray;   
  130.     -webkit-box-shadow: 1px 1px 5px gray;   
  131.     box-shadow: 1px 1px 5px gray;   
  132.     }   
  133.   
  134. .iconicmenu li a {   
  135.     displayblock;   
  136.     floatleft;   
  137.     text-aligncenter;   
  138.     text-decorationnone;   
  139.     colorblack;   
  140.     margin: 0;   
  141.     padding10px;   
  142.     padding-right15px;   
  143.     height: 100%;   
  144.     }   
  145.   
  146. .iconicmenu li a:hover {   
  147.     backgroundblack;   
  148.     colorwhite;   
  149.     }   
  150.   
  151. /* ----------------------------- CSS Media Queries ----------------------------- */  
  152.   
  153. /*  
  154. These rules control which portions of the menu gets shown when the screen size is below a certain width.  
  155. By default 2 stages are defined depending on browser screen width.  
  156. */  
  157.   
  158. @media screen and (max-width580px) { /* Hide toggle icon when menu is already open (increases usable menu space by 40px) */  
  159.     .iconicmenu input[type="checkbox"]:checked ~ label {   
  160.         displaynone;   
  161.         }   
  162.     .iconicmenu input[type="checkbox"]:checked ~ ul {   
  163.         margin-left: 0;   
  164.         }   
  165.     }   
  166.     
  167. @media screen and (max-width560px) { /* Convert horizontal menu to vertical drop down instead (friendly across all screen sizes) */  
  168.     .iconicmenu {   
  169.         overflowvisible;   
  170.         }   
  171.     .iconicmenu ul {   
  172.         heightauto;   
  173.         }   
  174.     .iconicmenu ul li {   
  175.         min-width200px;;   
  176.         displayblock;   
  177.         }   
  178.     .iconicmenu ul li a {   
  179.         floatnone;;   
  180.         text-alignleft;   
  181.         }   
  182.     }  

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

更多精彩内容其他人还在看

CSS入门教程:计算CSS盒模型宽和高

 出处:当我们布局一个网页的时候,经常会遇到这样的一种情况,那就是最终网页成型的宽度或是高度会超出我们预先的计算,其实就就是所谓的CSS的盒模型造成的。 #test{margin:10px;padding:10px;width:100px;height:100px;}
收藏 0 赞 0 分享

在IE流览器中正确显示PNG透明图片

  png图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!   但目前IE中对于插入
收藏 0 赞 0 分享

CSS教程:DIV底部放置文字

  css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近的距离还可以精确到像素,自己可以调节,是不是很不错呢?
收藏 0 赞 0 分享

如何用CSS让文字居于div的底部

  这个问题是别人提出的,因为css对文字的布局上没有靠容器底部对齐的参数,(或许有但是我没有发现)不过目前我使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,我用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近
收藏 0 赞 0 分享

从A页面连接到B页面后并直接把B页面的隐藏层显示

  这个效果实现的是,在B页面里有两个层,一个显示层,我们暂定名c层,一个是隐藏层,我们暂定名d层,单独进B页面的时候,c层显示,d层隐藏,然而从A页面连接到B页面的时候,则是让d层显示,c层隐藏,我觉得这个效果对网页设计者以后会有很大帮助,现在把代码发出来,
收藏 0 赞 0 分享

CSS样式表定义标签li前面样式

定义LI前面的小点样式 view plaincopy to clipboardprint? 语法: list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lowe
收藏 0 赞 0 分享

符合标准的div css制作的弹出菜单

本文介绍了五款符合标准的div css制作的弹出菜单,而且不含有js的. NO.1最基本的:二级dropdown弹出菜单 <!DOCTYPE html PUB
收藏 0 赞 0 分享

CSS实现在文章每段后面加入带连接的隐藏文字

代码主要理解3个参数:createElement、createTextNode、appendChild。这3个js参数分别是创建元素、创建字符、追加节点。代码原理:循环页面段落标签<p>,创建连接元素<a>,创建要显示的连接字符,用SetAttribute
收藏 0 赞 0 分享

CSS:浏览器特定选择器介绍

当你想在一个浏览器里改变样式而不像在其他浏览器中改变时,这些选择器很有用。 IE6以下 *html{} IE 7 以下 *:first-child html {} * html {} 只对IE 7 *:first-child html {} 只对IE 7
收藏 0 赞 0 分享

WEB标准学习,认识两种网页声明的含义

即网页标准推出来以后,我们时常会看到两种不同的网页的声明,一个是Dhtml,一个是Xhtml。如下所示: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht
收藏 0 赞 0 分享
查看更多