2016-04-01 125 views
1

我創建了一個嵌套列表,下拉菜單。下拉菜單的動態縮進

問題: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; 
} 

回答

1

我只是改變了一些規則,在你的CSS:

ul li:hover > ul { /* I just added ">" so it affect's only next ul. */ 
    display: block; 
    opacity: 1; 
    visibility: visible; 
} 

ul ul li > ul { /* This will fix the position for children uls */ 
    top: 0; 
    left: 100%; 
} 

活生生的例子:https://jsfiddle.net/xwazzo/pk7kcqb9/

+0

這是一個聰明的想法來定位孩子。 – sabithpocker

+0

謝謝@sabithpocker!我將離開此鏈接到[垂直主菜單帖子的水平子菜單](http://stackoverflow.com/questions/36349002/horizo​​ntal-submenu-for-a-vertical-main-menu-using-bootstrap/36349902#36349902 )在這裏,因爲我認爲相關,它很好解釋。 – xWaZzo

+0

這對我來說好處多多。好的解決方案 – Batman

2

嘗試使用的child selectora > b代替descendant selectora b

你可能到處都不需要子選擇器,因爲你可以利用後代選擇器本身的通用樣式,我只是用它無處不在,它可能不是你的正確方法!

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') 
 
})();
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; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="container"></div>

0

不要直接覆蓋標籤或這將是更加複雜你擴大。創建CSS類來節省時間。如果你寫這樣的CSS,例如:「ul li ul li」,將來會變得更加複雜。並且還嘗試使用「ul> li」選擇器在子節點上應用樣式。不是孩子的孩子。