HTML, CSS and jQuery: Create a navigation menu drop-down animation effect
In web development, an attractive page effect can increase the user's favorability of the website , improve user experience. Among them, menu navigation is one of the common elements in websites. We can add animation effects to increase the interactive effect of menus and make the website more vivid.
In this article, we will use HTML, CSS and jQuery to create a navigation menu with drop-down animation effects. Through the following steps, you will learn how to use these techniques to create a gorgeous navigation menu.
Step One: HTML Structure
First, we need to create a basic HTML structure. Inside the body tag, add a nav element and set a ul element with a class attribute as our menu container, and then add a few li elements inside it as menu options.
<!DOCTYPE html> <html> <head> <title>导航菜单下拉动画效果</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <nav> <ul class="menu"> <li><a href="#">首页</a></li> <li><a href="#">关于</a></li> <li><a href="#">服务</a></li> <li><a href="#">产品</a> <ul class="submenu"> <li><a href="#">产品1</a></li> <li><a href="#">产品2</a></li> <li><a href="#">产品3</a></li> </ul> </li> <li><a href="#">联系</a></li> </ul> </nav> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="script.js"></script> </body> </html>
Step 2: CSS Style
Next, let’s add CSS styles to set the appearance and animation effects of the menu. First, we style ul.menu
so that it has horizontally arranged menu items, and set its sub-element ul.submenu
to a hidden state. Then, set the drop-down animation effect when the mouse hovers by adding the pseudo-class selector:hover.
ul.menu { list-style: none; padding: 0; margin: 0; display: flex; } ul.menu li { position: relative; padding: 10px; background-color: #f7f7f7; } ul.menu li:hover ul.submenu { display: block; } ul.menu li ul.submenu { display: none; list-style: none; padding: 0; position: absolute; top: 100%; left: 0; background-color: #f7f7f7; } ul.menu li ul.submenu li { padding: 10px; } ul.menu li ul.submenu li:hover { background-color: #e1e1e1; }
Step 3: jQuery interaction
Finally, we use jQuery to set the drop-down animation effect. By using the .slideToggle() method, when the mouse is hovering over a menu item, the corresponding submenu is expanded or collapsed.
$(document).ready(function(){ $(".menu li").hover(function(){ $(this).find("ul.submenu").slideToggle("fast"); }); });
The above code uses jQuery's .hover() method to execute the corresponding function when the mouse hovers over the .menu li element. Within the function, use the .find() method to find the corresponding ul.submenu element, and use the .slideToggle() method to expand or collapse the submenu.
So far, we have completed the production of the navigation menu drop-down animation effect. You can modify the menu style according to your needs and add more submenu items and styles.
Summary
By using HTML, CSS and jQuery, we created a navigation menu with a drop-down animation effect. This animation effect can bring users a richer interactive experience and enhance the quality and appeal of the website.
Through this example, you can have a deep understanding of the cooperation between HTML, CSS and jQuery, and how to use these technologies to create cool page effects. I hope this article will be helpful to you in your learning and practice of web development.
The above is the detailed content of HTML, CSS and jQuery: Create a navigation menu drop-down animation effect. For more information, please follow other related articles on the PHP Chinese website!