jQuery垂直可折叠展开导航菜单代码实例详解

快乐打工仔 分类:实例代码

jQuery垂直可折叠展开导航菜单代码实例详解属于前端实例代码,有关更多实例代码大家可以查看

本章节分享一段代码实例,它实现了垂直可展开和折叠的导航菜单效果。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<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="#">pipipi.net</a>
      <a href="#">前端专区</a>
      <a href="#">资源专区</a>
    </div>
  </div>
</body>
</html>

上面的代码实现了我们的要求,下面介绍一下它的实现过程。

一.代码注释:

.menu_list {
  width: 268px;
  margin: 0 auto;
}

定义菜单容器的css代码,将菜单设置于网页水平居中位置。

.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;
}

设置菜单的标题样式,右侧默认是一个加号图标。

<div style="display:none" class="menu_body">

默认情况下二级导航菜单是处于隐藏状态的。

$(document).ready(function () {})

规定当文档结构完全加载完毕再去执行函数中的代码。

$("#firstpane h3.menu_head").click(function () {})

为标题注册click事件处理函数。

$(this).addClass("current")
.next("div.menu_body")
.slideToggle(300)
.siblings("div.menu_body")
.slideUp("slow");

上面的代码是一个链式调用。

当鼠标点击标题之后,就会为当前标题元素添加current样式类,这样右侧就会出现减号图标。

标题的下一个二级菜单就会展开。

其实的兄弟菜单就会收缩。

$(this).siblings().removeClass("current");

删除其他兄弟元素的current样式类。

二.相关阅读:

(1).addClass()可以参阅jQuery addClass()一章节。

(2).next()可以参阅jQuery next()一章节。

(3).slideToggle()可以参阅jQuery slideToggle()一章节。

(4).slideUp()可以参阅jQuery slideUp()一章节。

(5).removeClass()可以参阅jQuery removeClass()一章节。

jQuery垂直可折叠展开导航菜单代码实例详解,这样的场景在实际项目中还是用的比较多的,关于jQuery垂直可折叠展开导航菜单代码实例详解就介绍到这了。

回复

我来回复
  • 暂无回复内容