javascript - 如何在 css 的弹出菜单上添加 Accordion 菜单

标签 javascript jquery html css

我正在制作以下多级悬停菜单:

http://cssmenumaker.com/menu/simple-black-flyout

代码如下:

<!doctype html>
<html lang=''>
<head>
   <meta charset='utf-8'>
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">

<style type="text/css">

@charset "UTF-8";
/* Some stylesheet reset */
#cssmenu > ul {
  list-style: none;
  margin: 0;
  padding: 0;
  vertical-align: baseline;
  line-height: 1;
}
/* The container */
#cssmenu > ul {
  display: block;
  position: relative;
  width: 150px;
  /* The list elements which contain the links */

}
#cssmenu > ul li {
  display: block;
  position: relative;
  margin: 0;
  padding: 0;
  width: 100%;
}
/* General link styling */
#cssmenu > ul li a {
  /* Layout */

  display: block;
  position: relative;
  margin: 0;
  border-top: 1px dotted #3a3a3a;
  border-bottom: 1px dotted #1b1b1b;
  padding: 11px 20px;
  /* Typography */

  font-family: Helvetica, Arial, sans-serif;
  color: #d8d8d8;
  text-decoration: none;
  text-transform: uppercase;
  text-shadow: 0 1px 1px #000;
  font-size: 13px;
  font-weight: 300;
  /* Background & effects */

  background: #282828;
}
/* Rounded corners for the first link of the menu/submenus */
#cssmenu > ul li:first-child > a {
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  border-top: 0;
}
/* Rounded corners for the last link of the menu/submenus */
#cssmenu > ul li:last-child > a {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  border-bottom: 0;
}
/* The hover state of the menu/submenu links */
#cssmenu > ul li > a:hover,
#cssmenu > ul li:hover > a {
  color: #ffffff;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
  background: #54cdf1;
  background: -webkit-linear-gradient(#72d4f2, #54cdf1);
  background: -moz-linear-gradient(#72d4f2, #54cdf1);
  background: linear-gradient(#72d4f2, #54cdf1);
  border-color: transparent;
}
/* The arrow indicating a submenu */
#cssmenu > ul .has-sub > a::after {
  content: "";
  position: absolute;
  top: 16px;
  right: 10px;
  width: 0px;
  height: 0px;
  /* Creating the arrow using borders */

  border: 4px solid transparent;
  border-left: 4px solid #d8d8d8;
}
/* The same arrow, but with a darker color, to create the shadow effect */
#cssmenu > ul .has-sub > a::before {
  content: "";
  position: absolute;
  top: 17px;
  right: 10px;
  width: 0px;
  height: 0px;
  /* Creating the arrow using borders */

  border: 4px solid transparent;
  border-left: 4px solid #000;
}
/* Changing the color of the arrow on hover */
#cssmenu > ul li > a:hover::after,
#cssmenu > ul li:hover > a::after {
  border-left: 4px solid #ffffff;
}
#cssmenu > ul li > a:hover::before,
#cssmenu > ul li:hover > a::before {
  border-left: 4px solid rgba(0, 0, 0, 0.3);
}
/* THE SUBMENUS */
#cssmenu > ul ul {
  position: absolute;
  left: 100%;
  top: -9999px;
  padding-left: 5px;
  opacity: 0;
  width: 140px;
  /* The fade effect, created using an opacity transition */

  -webkit-transition: opacity 0.3s ease-in;
  -moz-transition: opacity 0.3s ease-in;
  transition: opacity 0.3s ease-in;
}
/* Showing the submenu when the user is hovering the parent link */
#cssmenu > ul li:hover > ul {
  top: 0px;
  opacity: 1;
}





</style>
   <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
   <script src="script.js"></script>

<title>CSS MenuMaker</title>
</head>
<body>

<div id='cssmenu'>
<ul>
   <li><a href='#'><span>Home</span></a></li>
   <li class='active has-sub'><a href='#'><span>Products</span></a>
      <ul>
         <li class='has-sub'><a href='#'><span>Product 1</span></a>
    <div id="accordian">
            <ul>
               <li>
          <a href='#'><span>Sub Product</span></a>


          </li>
               <li>
        <a href='#'><span>Sub Product</span></a>


          </li>
            </ul>
         </li>
         <li class='has-sub'><a href='#'><span>Product 2</span></a>
            <ul>
               <li><a href='#'><span>Sub Product</span></a></li>
               <li class='last'><a href='#'><span>Sub Product</span></a></li>
            </ul>
         </li>
      </ul>
   </li>
   <li><a href='#'><span>About</span></a></li>
   <li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</div>

</body>
<html>

我希望“子产品”列表项作为 Accordion 菜单打开。我怎样才能在这个菜单代码中做到这一点?我对此很陌生。

最佳答案

这将需要一些 css 修复,我没有这样做,但它显示了基本概念:

商品1部分代码:

      <li class='has-sub'><a href='#'><span>Product 1</span></a>
          <ul><li>  
          <div id="accordion">
            <h3>Section 1</h3>
              <div>
                Section 1 Content
              </div>
              <h3>Section 2</h3>
              <div>
                Section 2 Content
              </div>
           </div>
           </ul></li> 
     </li>

完整代码和工作演示: Here

关于javascript - 如何在 css 的弹出菜单上添加 Accordion 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706857/

相关文章:

html - 如何将 NSAttributedString 转换为 HTML 字符串?

javascript - JQuery 类添加/删除不起作用(使用未知的委托(delegate))

jquery - 如何限制jquery全日历中的事件数量?

基于下拉列表选项加载内容的 PHP 表单

jquery - 将光标更改为悬停纯文本上的指针,例如 <a>

javascript - 从每个循环中选取的 div 移动第 N 个元素

html - Angular JS - 有一个空数组

javascript - 模块内部的简单函数调用,得到 NaN,是吗?

javascript - 在 CoffeeScript 中加载图像?

javascript - 无法在 IE 9 中加载谷歌地图 Geojson 数据