Quickly realize jQuery multi level menu effect

  • 2021-07-15 07:58:03
  • OfStack

Use js or jquery to realize the multi-level menu effect of navigation bar most quickly.

I use jquery 1.9. 1 for this code, and the compatibility below ie8 needs to be considered, mainly for the introduction of the code, write less, do more.


<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<head>
  <title></title>
  <style type="text/css">
    .top-nav
    {
      font-size: 12px;
      font-weight: bold;
      list-style: none;
    }
    .top-nav li
    {
      float: left;
      list-style: none;
      margin-right: 1px;
    }
    .top-nav li a
    {
      line-height: 20px;
      text-decoration: none;
      background: #DDDDDD;
      color: #666666;
      display: block;
      width: 80px;
      text-align: center;
    }
    .top-nav li a:hover
    {
      background: #900;
      color: #FFF;
    }
    .top-nav ul
    {
      list-style: none;
      display: none;
      width: 80px;
      padding: 0;
      position: relative;
    }
    .top-nav li ul li ul
    {
      position: absolute;
      top: 0;
      left: 80px;
    }
  </style>
</head>
<body>
  <ul class="top-nav">
    <li><a href="#"> Home page content </a>
      <ul>
        <li><a href="#"> Front-end courses  +</a>
          <ul>
            <li><a href="#">javascript</a></li>
            <li><a href="#">css</a></li>
            <li><a href="#">jquery</a></li>
          </ul>
        </li>
        <li><a href="#"> Mobile phone development </a>
          <ul>
            <li><a href="#">ios Development </a></li>
            <li><a href="#">android Development </a></li>
            <li><a href="#">WP Development </a></li>
          </ul>
        </li>
        <li><a href="#"> Background programming </a></li>
      </ul>
    </li>
    <li><a href="#"> Course hall </a> </li>
    <li><a href="#"> Learning center  +</a>
      <ul>
        <li><a href="#"> Front-end courses  +</a>
          <ul>
            <li><a href="#">javascript</a></li>
            <li><a href="#">css</a></li>
            <li><a href="#">jquery</a></li>
          </ul>
        </li>
        <li><a href="#"> Mobile phone development </a>
          <ul>
            <li><a href="#">ios Development </a></li>
            <li><a href="#">android Development </a></li>
            <li><a href="#">WP Development </a></li>
          </ul>
        </li>
        <li><a href="#"> Background programming </a></li>
      </ul>
    </li>
    <li><a href="#"> About us </a></li>
  </ul>
    </script>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
   $(function(){
    $(".top-nav li").hover(function(){
      $(this).has("ul").children("ul").fadeIn();
    },function(){
      $(this).has("ul").children("ul").hide();
    });
   }) 
</script>
</body>
</html>

Related articles: