Home >Web Front-end >JS Tutorial >jQuery implements accordion side menu effect code
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('.childrenUl').slideToggle(); }); $(".childrenLi").click(function(event) { event.stopPropagation(); $(".childrenLi").removeClass('selectedChildrenMenu'); $(".parentLi").removeClass('selectedParentMenu'); $(this).parents(".parentLi").addClass('selectedParentMenu'); $(this).addClass('selectedChildrenMenu'); });
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!