Home  >  Article  >  Web Front-end  >  jQuery implements accordion side menu effect code

jQuery implements accordion side menu effect code

零下一度
零下一度Original
2017-04-19 15:57:451092browse

This article mainly introduces the accordion side menu effect implemented by jQuery, involving implementation techniques related to jQuery event response and dynamic operation of element attributes. Friends in need can refer to it

The example of this article describes the implementation of jQuery Accordion side menu effect. Share it with everyone for your reference, the details are as follows:

I made a simple accordion menu, as shown above:

Click B to shrink the C list, click C changes the style of itself and parent node B, and both have different style changes when suspended.

Look at the page code first, the nesting of the list:


<p id="menup">
<ul id="menu">
<li class="parentLi">
<span>B</span>
<ul class="childrenUl">
<li class="childrenLi"><span>C</span></li>
<li class="childrenLi"><span>C</span></li>
<li class="childrenLi"><span>C</span></li>
</ul>
</li>
<li class="parentLi">
<span>B</span>
<ul class="childrenUl">
<li class="childrenLi"><span>C</span></li>
<li class="childrenLi"><span>C</span></li>
<li class="childrenLi"><span>C</span></li>
</ul>
</li>
<li class="parentLi">
<span>B</span>
<ul class="childrenUl">
<li class="childrenLi"><span>C</span></li>
<li class="childrenLi"><span>C</span></li>
<li class="childrenLi"><span>C</span></li>
</ul>
</li>
</ul>
</p>

css code, mainly sets the background color and the style of the left border of the submenu, and sets the suspension and selection Style, start setting the submenu not to display (display after clicking through js setting):


#menup{
  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 the code for the click event:


$(".parentLi").click(function(event) {
    $(this).children(&#39;.childrenUl&#39;).slideToggle();
});
$(".childrenLi").click(function(event) {
    event.stopPropagation();
    $(".childrenLi").removeClass(&#39;selectedChildrenMenu&#39;);
    $(".parentLi").removeClass(&#39;selectedParentMenu&#39;);
    $(this).parents(".parentLi").addClass(&#39;selectedParentMenu&#39;);
    $(this).addClass(&#39;selectedChildrenMenu&#39;);
});

The above is the detailed content of jQuery implements accordion side menu effect code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn