动态缩进下拉菜单

时间:2016-04-01 06:58:31

标签: javascript jquery html css drop-down-menu

我已经创建了一个嵌套列表,下拉菜单。

问题:CSS只处理1级嵌套菜单。我无法弄清楚如何设置嵌套项的位置,以便它可以处理多个级别,而无需在CSS文件中设置这些规则。

这是fiddle我一直在使用

CSS:

ul {
  text-align: left;
  display: inline;
  margin: 0;
  padding: 15px 4px 17px 0;
  list-style: none;
  -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
}
ul li {
  font: bold 12px/18px sans-serif;
  display: inline-block;
  margin-right: -4px;
  position: relative;
  padding: 15px 20px;
  background: #fff;
  cursor: pointer;
  -webkit-transition: all 0.2s;
  -moz-transition: all 0.2s;
  -ms-transition: all 0.2s;
  -o-transition: all 0.2s;
  transition: all 0.2s;
}
ul li:hover {
  background: #555;
  color: #fff;
}
ul li ul {
  padding: 0;
  position: absolute;
  top: 48px;
  left: 0;
  width: 150px;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  display: none;
  opacity: 0;
  visibility: hidden;
  -webkit-transiton: opacity 0.2s;
  -moz-transition: opacity 0.2s;
  -ms-transition: opacity 0.2s;
  -o-transition: opacity 0.2s;
  -transition: opacity 0.2s;
}
ul li ul li { 
  background: #555; 
  display: block; 
  color: #fff;
  text-shadow: 0 -1px 0 #000;
}
ul li ul li:hover { background: #666; }
ul li:hover ul {
  display: block;
  opacity: 1;
  visibility: visible;
}

3 个答案:

答案 0 :(得分:2)

尝试使用child selector a > b代替descendant selector a b

您可能不需要在任何地方使用子选择器,因为您可以使用后代选择器本身的常见样式,我只是使用它无处不在,它可能不适合您! / p>



var items = [{
  "id": "1",
  "title": "Menu A",
  "link": "http: //www.google.com"
}, {
  "id": "2",
  "title": "Menu B",
  "link": "http://www.google.com"
}, {
  "id": "3",
  "title": "Menu C",
  "link": "http://www.google.com"
}, {
  "id": "4",
  "title": "Menu D",
  "link": "http://www.google.com"
}, {
  "id": "5",
  "title": "Menu E",
  "link": "http://www.google.com"
}, {
  "id": "6",
  "title": "Menu 1A",
  "link": "http://www.google.com",
  "parent": "1;#Menu A"
}, {
  "id": "7",
  "title": "Menu 2A",
  "link": "http://www.google.com",
  "parent": "1;#Menu A"
}, {
  "id": "8",
  "title": "Menu 3A",
  "link": "http://www.google.com",
  "parent": "1;#Menu A"
}, {
  "id": "9",
  "title": "Menu 1B",
  "link": "http://www.google.com",
  "parent": "2;#Menu B"
}, {
  "id": "10",
  "title": "Menu 2B",
  "link": "http://www.google.com",
  "parent": "2;#Menu B"
}, {
  "id": "11",
  "title": "Menu 1C",
  "link": "http://www.google.com",
  "parent": "3;#Menu C"
}, {
  "id": "12",
  "title": "Menu 2C",
  "link": "http://www.google.com",
  "parent": "3;#Menu C"
}, {
  "id": "13",
  "title": "Menu 3C",
  "link": "http://www.google.com",
  "parent": "3;#Menu C"
}, {
  "id": "14",
  "title": "Menu 1D",
  "link": "http://www.google.com",
  "parent": "4;#Menu D"
}, {
  "id": "15",
  "title": "Menu 2D",
  "link": "http://www.google.com",
  "parent": "14;#Menu 1D"
}, {
  "id": "16",
  "title": "Menu 3D",
  "link": "http://www.google.com",
  "parent": "15;#Menu 2D"
}, {
  "id": "17",
  "title": "Menu 1E",
  "link": "http://www.google.com",
  "parent": "5;#Menu E"
}, {
  "id": "18",
  "title": "Menu 2E",
  "link": "http://www.google.com",
  "parent": "17;#Menu 1E"
}]

items = $.map(items, function(item) {
  return {
    id: item.id,
    title: item.title,
    link: item.link,
    parent: (item.parent) ? item.parent.split(";")[0] : false
  }
})
console.log(items)


var container = $("#container");

(function createMenu() {
  $.each(items, function(i, v) {
    //Check if item has parent
    if (v.parent == false) {
        // Check if container has first level ul
        if (container.find("ul#topLevel").length == 0) {
          container.append($("<ul>").attr("id","topLevel"))
        } else {
          console.log('firstlevel exist')
        }

      //Add items to first level
      container
      .find('ul')
      .append($("<li>")
        .text(v.title)
        .attr('id', v.id)
        )
    }else{
      var parent = v.parent;
      var parentElement = $("#"+parent);

      if( parentElement.find('ul').length == false ) {
        
        parentElement.append($("<ul>").attr({
          id: 'subLevelFor_'+ parent
        }))
      }

      $("ul#subLevelFor_"+ parent)
      .append($("<li>")
        .text(v.title)
        .attr('id', v.id)
        )
    }
  })
  console.log('done')
})();
&#13;
body {
  font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  padding: 20px 50px 150px;
  font-size: 13px;
  text-align: center;
  background: #E3CAA1;
}

ul {
  text-align: left;
  display: inline;
  margin: 0;
  padding: 15px 4px 17px 0;
  list-style: none;
  -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
}
ul > li {
  font: bold 12px/18px sans-serif;
  display: inline-block;
  margin-right: -4px;
  position: relative;
  padding: 15px 20px;
  background: #fff;
  cursor: pointer;
  -webkit-transition: all 0.2s;
  -moz-transition: all 0.2s;
  -ms-transition: all 0.2s;
  -o-transition: all 0.2s;
  transition: all 0.2s;
}
ul > li:hover {
  background: #555;
  color: #fff;
}
ul > li > ul {
  padding: 0;
  position: absolute;
  top: 48px;
  left: 0;
  width: 150px;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  display: none;
  opacity: 0;
  visibility: hidden;
  -webkit-transiton: opacity 0.2s;
  -moz-transition: opacity 0.2s;
  -ms-transition: opacity 0.2s;
  -o-transition: opacity 0.2s;
  -transition: opacity 0.2s;
}
ul > li > ul > li { 
  background: #555; 
  display: block; 
  color: #fff;
  text-shadow: 0 -1px 0 #000;
}
ul > li > ul > li:hover { background: #666; }
ul > li:hover > ul {
  display: block;
  opacity: 1;
  visibility: visible;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我刚刚在你的css中改变了一些规则:

int i = 3;
int step = 1;

while (list.size()  > i) {
    list.remove(i);
    step++;
    i = 3 * step - step + 1; 
}

实例:https://jsfiddle.net/xwazzo/pk7kcqb9/

答案 2 :(得分:0)

不要直接覆盖标签,否则扩展会更复杂。创建CSS类以节省您的时间。如果你写这样的CSS,例如:&#34; ul li ul li&#34;它将来会变得更加复杂。并尝试使用&#34; ul&gt;李&#34;选择器以在子项上应用样式。不是孩子的孩子。