Realization of accordion Menu _jquery based on jquery code

Source: Internet
Author: User
Tags jquery accordion

First to show you the effect of the picture:

First look at the page code, nested list:

<div id= "Menudiv" > <ul id= "menu" > <li class= "Parentli" > <span>B</span> &L T;ul class= "Childrenul" > <li class= "Childrenli" ><span>C</span></li> <li clas s= "Childrenli" ><span>C</span></li> <li class= "Childrenli" ><span>c</span>&  lt;/li> </ul> </li> <li class= "Parentli" > <span>B</span> <ul class= "Childrenul" > <li class= "Childrenli" ><span>C</span></li> <li class= "C" Hildrenli "><span>C</span></li> <li class=" Childrenli "><span>c</span></ li> </ul> </li> <li class= "Parentli" > <span>B</span> <ul CLA ss= "Childrenul" > <li class= "Childrenli" ><span>C</span></li> <li class= "Child Renli "><span>c</span></li> <li class= "Childrenli" ><span>C</span></li> </ul> </ Li> </ul> </div>

CSS code, mainly set the background color and submenu to the left side of the style, set the suspension and selected style, start to set the submenu does not display (through JS set click after the display):

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

The next step is to click the event code:

$ (". 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 ');
  });

It should be noted that the list nesting, will cause event bubbling, so in the click of the submenu event to organize Bubbles, event.stoppropagation ();

The above code is very simple, the code is a comment, the jquery accordion menu is implemented. Need a friend to quickly refer to the next bar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.