本章节分享一段代码实例,它实现了垂直可展开和折叠的导航菜单效果。
代码实例如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.gaodaima.com/" /> <title>搞代码</title> <style type="text/css"> * { margin: 0; padding: 0; } .menu_list { width: 268px; margin: 0 auto; } .menu_head { height: 47px; line-height: 47px; padding-left: 38px; font-size: 14px; color: #525252; cursor: pointer; border-left: 1px solid #e1e1e1; border-right: 1px solid #e1e1e1; border-bottom: 1px solid #e1e1e1; border-top: 1px solid #F1F1F1; position: relative; margin: 0px; font-weight: bold; background: #f1f1f1 url(demo/jQuery/img/jia.png) center right no-repeat; } .menu_list .current { background: #f1f1f1 url(demo/jQuery/img/jian.png) center right no-repeat; } .menu_body { line-height: 38px; border-left: 1px solid #e1e1e1; background: #fff; border-right: 1px solid #e1e1e1; } .menu_body a { display: block; height: 38px; line-height: 38px; padding-left: 38px; color: #777777; background: #fff; text-decoration: none; border-bottom: 1px solid #e1e1e1; } .menu_body a:hover { text-decoration: none; } </style> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script> $(document).ready(function () { $("#firstpane h3.menu_head").click(function () { $(this).addClass("current") .next("div.menu_body") .slideToggle(300) .siblings("div.menu_body") .slideUp("slow"); $(this).siblings().removeClass("current"); }); }); </script> </head> <body> <div id="firstpane" class="menu_list"> <h3 class="menu_head">搞代码</h3> <div style="display:none" class="menu_body"> <a href="#">div教程</a> <a href="#">css教程</a> <a href="#">js教程</a> <a href="#">css3教程</a> <a href="#">json教程</a> <a href="#">ajax教程</a> <a href="#">es6教程</a> <a href="#">jquery教程</a> </div> <h3 class="menu_head">资源下载</h3> <div style="display:none" class="menu_body"> <a href="#">特效下载</a> <a href="#">移动端下载</a> <a href="#">模板下载</a> <a href="#">源码下载</a> <a href="#">图片下载</a> </div> <h3 class="menu_head">前端网站</h3> <div style="display:none" class="menu_body"> <a href="#">搞代码</a> <a href="#">gaodaima.com</a> <a href="#">前端专区</a> <a href="#">资源专区</a> </div> </div> </body> </html>