JQuery uses CSS to create a dynamic drop-down menu, jquerycss

Source: Internet
Author: User

JQuery uses CSS to create a dynamic drop-down menu, jquerycss

When you want to place a large sub-menu in a limited navigation menu space, we usually use a drop-down menu to make up for the lack of space. This article uses jQuery and CSS to create a dynamic drop-down menu with the minimum time needed.

XHTML
First, introduce the jquery library in the head part of the page. This is required.

<script type="text/javascript" src="js/jquery.js"></script> 

Then I use an unordered list to create a menu.

<Ul class = "menu"> <li> <a href = "#"> homepage </a> </li> <a href = "#"> Service </a> </li> <a href = "#"> case </a> </li> <a href = "#"> about </a> </li> <a href = "#"> BLOG </a> </li> </ul>

It looks very simple at a Glance. Since the drop-down menu is closed at the beginning, I also need to create a visual trigger drop-down button. This article uses an image as the trigger button for convenience. Place the image on the top of the menu list

 

CSS
Create CSS styles for the menu separately. See the Code:

.menu_head{border:1px solid #998675; background:#f30} .menu{display:none; width:184px; border:1px solid #998675; border-top:none} .menu li{list-style:none; background:#493e3b} .menu li a{padding:10px; display:block;color:#fff; text-decoration:none;} .menu li a:hover{font-weight:bold;} .menu li.alt{background:#362f2d;} 

It is worth noting that the. menu li. alt style is used to distinguish between line breaks of parity rows, which will be reflected in the jquery code below.
JQuery
In jQuery code, I first need to differentiate the number of lines in a drop-down menu, and give the even row a style: alt. Next, add a trigger event for the image button. When you click the button, you can switch to the drop-down menu.

$(function(){   $(".menu li:even").addClass("alt");   $("img.menu_head").click(function(){     $(".menu").slideToggle("fast");   }); }); 

Note:,I used jQuery's slideToggle method to display and hide menus in a slide manner. The effect is very smooth.

The above is the share of jQuery combined with CSS to create a dynamic drop-down menu, hope to help you learn.

Related Article

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.