您好,欢迎访问一九零五行业门户网

基于Jquery代码实现手风琴菜单_jquery

先给大家展示效果图:
先看页面代码,列表的嵌套:
bcccbcccbccc

css 代码,主要设置背景色和子菜单左边框的样式,设置悬浮和选中的样式,开始设置子菜单不显示(通过 js 设置点击之后再显示):
#menudiv{ width: 200px; background-color: #029fd4;}.parentli{ width: 100%; line-height: 40px; margin-top: 1px; background: #1c73ba; color: #fff; cursor: pointer; font-weight:bolder;}.parentli span{ padding: 10px;}.parentli:hover, .selectedparentmenu{ background: #0033cc;}.childrenul{ background-color: #ffffff; display: none;}.childrenli{ width: 100%; line-height: 30px; font-size: .9em; margin-top: 1px; background: #63b8ff; color: #000000; padding-left: 15px; cursor: pointer;}.childrenli:hover, .selectedchildrenmenu{ border-left: 5px #0033cc solid; background: #0099cc; padding-left: 15px;}
接下来就是点击事件的代码:
$(.parentli).click(function(event) { $(this).children('.childrenul').slidetoggle(); }); $(.childrenli).click(function(event) { event.stoppropagation(); $(.childrenli).removeclass('selectedchildrenmenu'); $(.parentli).removeclass('selectedparentmenu'); $(this).parents(.parentli).addclass('selectedparentmenu'); $(this).addclass('selectedchildrenmenu'); });
需要注意的是列表嵌套,会导致事件冒泡,所以在子菜单的点击事件里面要组织冒泡,event.stoppropagation();
以上代码很简单,代码就是注释,jquery手风琴菜单就实现了。需要的朋友快来参考下吧。
其它类似信息

推荐信息