Sunday, 6 August 2017

Simple Drop down Menu using jQuery

Simple Drop down Menu using jQuery

Hello friends, this tutorial will help you to make the simple drop down menu using jQuery very easily. So you do not need to more plugins for this simple task.

Step 1 :

Here is our HTML markup
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Simple Dropdown using jQuery</title>
  5. </head>
  6. <body>
  7. <div class="container">
  8. <ul class="menu">
  9. <li><a href="#">Home</a>
  10. <ul>
  11. <li><a href="#">Link One</a></li>
  12. <li><a href="#">Link Two</a></li>
  13. <li><a href="#">Link Three</a></li>
  14. <li><a href="#">Link Four</a></li>
  15. </ul>
  16. </li>
  17. <li><a href="#">About us</a></li>
  18. <li><a href="#">Contact us</a>
  19. <ul>
  20. <li><a href="#">Link Another One</a></li>
  21. <li><a href="#">Link Another Two</a></li>
  22. <li><a href="#">Link Another Three</a></li>
  23. <li><a href="#">Link Another Four</a></li>
  24. </ul>
  25. </li>
  26. <li><a href="#">Services</a></li>
  27. <li><a href="#">Our Clients</a></li>
  28. </ul>
  29. </div>
  30. <footer>
  31. &copy; NareshOnLine.com | Naresh Pal
  32. </footer>
  33. </div>
  34. </body>
  35. </html>

Step 2 :

Here is our css styling
  1. <style type="text/css">
  2. *{
  3. margin: 0;
  4. padding:0;
  5. font-family: arial;
  6. }
  7. .container{
  8. width:100%;
  9. height: 50px;
  10. }
  11. .container ul{
  12. list-style-type:none;
  13. text-transform: uppercase;
  14. }
  15. .container ul li{
  16. background: #236090;
  17. float: left;
  18. width: 20%;
  19. height: 50px;
  20. line-height: 50px;
  21. text-align: center;
  22. position: relative;
  23. }
  24. .container ul li:hover{
  25. background: #023972;
  26. }
  27. .container ul li>a{
  28. display: block;
  29. text-decoration: none;
  30. color: #fff;
  31. }
  32. .container ul li ul{
  33. position: absolute;
  34. width: 100%;
  35. }
  36. .container ul li li{
  37. width: 100% !important;
  38. display: none;
  39. }
  40. footer{
  41. clear: both;
  42. margin-top:20px;
  43. padding: 20px;
  44. color: #888;
  45. border-top: 2px solid #ccc;
  46. text-align: center;
  47. }
  48. </style>

Step 3 :

Lets add some jQuery to make this task so easy.
First add jQeury liberary using CDN Link
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
Now add your custom script
  1. <script type="text/javascript">
  2. $(function(){
  3. // function on hover on main li
  4. $('li').hover(function(){
  5. // enabaling function to find child li to show
  6. $(this).find('ul>li').stop().show(200);
  7. }, function(){
  8. //enabling call back function to hide the child li
  9. $(this).find('ul>li').hide();
  10. });
  11. });
  12. </script>
 Author : Naresh Pal
Website  : http://nareshonline.com/

No comments:

Post a Comment

Upload file using PHP

Upload file using PHP This is a simple tutorial to learn how to upload file using PHP with the help of HTML form. PHP makes it very...