CSS响应式下拉菜单-分为2列 [英] CSS Responsive Dropdown Menu - Split into 2 Columns
问题描述
我已经仔细研究了这里的所有支持,并尝试了许多不同的方法将此CSS Dropdown菜单分为2列,因为我有很多条目将导航列表扩展到折叠以下.
I've looked through all the support here and have tried many different ways to split this CSS Dropdown menu into 2 columns, as I have many entries that stretch the nav list down below the fold.
即使菜单是响应式的,我只需要两列在展开时即可工作,而不是在移动浏览缩小时就可以工作(那方面很好).我也在使用jQuery脚本.
Even though the menu is responsive, I just need the two columns to work when its expanded, not when it shrinks for mobile browsing (that aspect is fine). I'm using jQuery script as well.
我尚未成功对代码进行任何调整.有人对我如何将该下拉菜单分为2个并排的列有建议吗?
I have not yet been successful using any adjustments to my code. Anyone have advice on how I can split this drop-down into 2 side-by-side columns?
这是HTML:
<div id='cssmenu' class='align-center'>
<ul>
<li><a href='index.html'>Home</a></li>
<li class='has-sub'><a href='equipment.html'>Equipment</a>
<ul>
<li><a href='#'>Automotive</a></li>
<li><a href='#'>Building Equip.</a></li>
<li><a href='#'>Concrete Tools</a></li>
<li><a href='#'>Drills</a></li>
<li><a href='#'>Generators</a></li>
<li><a href='#'>Heaters</a></li>
<li><a href='#'>Household Equip.</a></li>
<li><a href='#'>Ladders</a></li>
<li><a href='#'>Painting</a></li>
<li><a href='#'>Party / Canopy</a></li>
<li><a href='#'>Plumbing</a></li>
<li><a href='#'>Pumps</a></li>
<li><a href='#'>Sanders</a></li>
<li><a href='#'>Saws</a></li>
<li><a href='#'>Stage</a></li>
<li><a href='#'>Table & Chairs</a></li>
<li><a href='#'>Trailers</a></li>
<li><a href='#'>Trenchers</a></li>
<li><a href='#'>Garden & Lawn Care</a></li>
<li><a href='#'>Miscellaneous</a></li>
</ul>
</li>
<li><a href='concrete.html'>Concrete</a></li>
<li><a href='gravel.html'>Gravel</a></li>
<li><a href='propane.html'>Propane</a></li>
</ul>
</div>
这是CSS:
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700);
#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a,
#cssmenu #menu-button {
margin: 0;
padding: 0;
border: 0;
list-style: none;
line-height: 1;
display: block;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#cssmenu:after,
#cssmenu > ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
#cssmenu #menu-button {
display: none;
}
#cssmenu {
z-index: 100;
width: auto;
font-family: 'Open Sans', Helvetica, sans-serif;
background: #468cc6;
background: -moz-linear-gradient(top, #5d9bcd 0%, #3677ae 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #5d9bcd), color-stop(100%, #3677ae));
background: -webkit-linear-gradient(top, #5d9bcd 0%, #3677ae 100%);
background: -o-linear-gradient(top, #5d9bcd 0%, #3677ae 100%);
background: -ms-linear-gradient(top, #5d9bcd 0%, #3677ae 100%);
background: linear-gradient(to bottom, #5d9bcd 0%, #3677ae 100%);
}
#cssmenu > ul {
background: url('images/bg.png');
box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.05);
}
#cssmenu.align-right > ul > li {
float: right;
}
#cssmenu > ul > li {
float: left;
display: inline-block;
}
#cssmenu.align-center > ul {
float: none;
text-align: center;
font-size: 0;
}
#cssmenu.align-center > ul > li {
float: none;
}
#cssmenu.align-center ul ul {
text-align: left;
}
#cssmenu > ul > li > a {
padding: 18px 25px 21px 25px;
border-right: 1px solid rgba(80, 80, 80, 0.12);
text-decoration: none;
font-size: 14px;
font-weight: 700;
color: #FFFFFF;
text-transform: uppercase;
letter-spacing: 1px;
}
#cssmenu > ul > li:hover > a,
#cssmenu > ul > li > a:hover,
#cssmenu > ul > li.active > a {
color: #ffffff;
background: #3c85c1;
background: rgba(0, 0, 0, 0.1);
}
#cssmenu > ul > li.has-sub > a {
padding-right: 45px;
}
#cssmenu > ul > li.has-sub > a::after {
content: "";
position: absolute;
width: 0;
height: 0;
border: 6px solid transparent;
border-top-color: #c7def0;
right: 17px;
top: 22.5px;
}
#cssmenu > ul > li.has-sub.active > a::after,
#cssmenu > ul > li.has-sub:hover > a {
border-top-color: #ffffff;
}
#cssmenu ul ul {
position: absolute;
left: -9999px;
top: 60px;
z-index: 9999;
padding-top: 6px;
font-size: 14px;
opacity: 0;
-webkit-transition: top 0.2s ease, opacity 0.2s ease-in;
-moz-transition: top 0.2s ease, opacity 0.2s ease-in;
-ms-transition: top 0.2s ease, opacity 0.2s ease-in;
-o-transition: top 0.2s ease, opacity 0.2s ease-in;
transition: top 0.2s ease, opacity 0.2s ease-in;
}
#cssmenu.align-right ul ul {
text-align: right;
}
#cssmenu > ul > li > ul::after {
content: "";
position: absolute;
width: 0;
height: 0;
border: 5px solid transparent;
border-bottom-color: #468cc6;
top: -4px;
left: 20px;
}
#cssmenu.align-right > ul > li > ul::after {
left: auto;
right: 20px;
}
#cssmenu ul ul ul::after {
content: "";
position: absolute;
width: 0;
height: 0;
border: 5px solid transparent;
border-right-color: #468cc6;
top: 11px;
left: -4px;
}
#cssmenu.align-right ul ul ul::after {
border-right-color: transparent;
border-left-color: #468cc6;
left: auto;
right: -4px;
}
#cssmenu > ul > li > ul {
top: 120px;
}
#cssmenu > ul > li:hover > ul {
top: 53px;
left: 0;
opacity: 1;
}
#cssmenu.align-right > ul > li:hover > ul {
left: auto;
right: 0;
}
#cssmenu ul ul ul {
padding-top: 0;
padding-left: 6px;
}
#cssmenu.align-right ul ul ul {
padding-right: 6px;
}
#cssmenu ul ul > li:hover > ul {
left: 180px;
top: 0;
opacity: 1;
}
#cssmenu.align-right ul ul > li:hover > ul {
left: auto;
right: 100%;
opacity: 1;
}
#cssmenu ul ul li a {
text-decoration: none;
font-weight: 400;
padding: 11px 25px;
width: 180px;
color: #ffffff;
background: #468cc6;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1), 1px 1px 1px rgba(0, 0, 0, 0.1), -1px 1px 1px rgba(0, 0, 0, 0.1);
}
#cssmenu ul ul li:hover > a,
#cssmenu ul ul li.active > a {
color: #333333;
}
#cssmenu ul ul li:first-child > a {
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
#cssmenu ul ul li:last-child > a {
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
}
#cssmenu > ul > li > ul::after {
position: absolute;
display: block;
}
#cssmenu ul ul li.has-sub > a::after {
content: "";
position: absolute;
width: 0;
height: 0;
border: 4px solid transparent;
border-left-color: #ffffff;
right: 17px;
top: 14px;
top: 14.5px;
}
#cssmenu.align-right ul ul li.has-sub > a::after {
border-left-color: transparent;
border-right-color: #ffffff;
right: auto;
left: 17px;
}
#cssmenu ul ul li.has-sub.active > a::after,
#cssmenu ul ul li.has-sub:hover > a::after {
border-left-color: #333333;
}
#cssmenu.align-right ul ul li.has-sub.active > a::after,
#cssmenu.align-right ul ul li.has-sub:hover > a::after {
border-right-color: #333333;
border-left-color: transparent;
}
@media all and (max-width: 725px), only screen and (-webkit-min-device-pixel-ratio: 2) and (max-width: 1024px), only screen and (min--moz-device-pixel-ratio: 2) and (max-width: 1024px), only screen and (-o-min-device-pixel-ratio: 2/1) and (max-width: 1024px), only screen and (min-device-pixel-ratio: 2) and (max-width: 1024px), only screen and (min-resolution: 192dpi) and (max-width: 1024px), only screen and (min-resolution: 2dppx) and (max-width: 1024px) {
#cssmenu {
background: #468cc6;
}
#cssmenu > ul {
display: none;
}
#cssmenu > ul.open {
display: block;
border-top: 1px solid rgba(0, 0, 0, 0.1);
}
#cssmenu.align-right > ul {
float: none;
}
#cssmenu.align-center > ul {
text-align: left;
}
#cssmenu > ul > li,
#cssmenu.align-right > ul > li {
float: none;
display: block;
}
#cssmenu > ul > li > a {
padding: 18px 25px 18px 25px;
border-right: 0;
}
#cssmenu > ul > li:hover > a,
#cssmenu > ul > li.active > a {
background: rgba(0, 0, 0, 0.1);
}
#cssmenu #menu-button {
display: block;
text-decoration: none;
font-size: 13px;
font-weight: 700;
color: #FFFFFF;
padding: 18px 25px 18px 25px;
text-transform: uppercase;
letter-spacing: 1px;
background: url('images/bg.png');
cursor: pointer;
}
#cssmenu ul ul,
#cssmenu ul li:hover > ul,
#cssmenu > ul > li > ul,
#cssmenu ul ul ul,
#cssmenu ul ul li:hover > ul,
#cssmenu.align-right ul ul,
#cssmenu.align-right ul li:hover > ul,
#cssmenu.align-right > ul > li > ul,
#cssmenu.align-right ul ul ul,
#cssmenu.align-right ul ul li:hover > ul {
left: 0;
right: auto;
top: auto;
opacity: 1;
width: 100%;
padding: 0;
position: relative;
text-align: left;
}
#cssmenu ul ul li {
width: 100%;
}
#cssmenu ul ul li a {
width: 100%;
box-shadow: none;
padding-left: 35px;
}
#cssmenu ul ul ul li a {
padding-left: 45px;
}
#cssmenu ul ul li:first-child > a,
#cssmenu ul ul li:last-child > a {
border-radius: 0;
}
#cssmenu #menu-button::after {
display: block;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
content: '';
position: absolute;
height: 3px;
width: 22px;
border-top: 2px solid #c7def0;
border-bottom: 2px solid #c7def0;
right: 25px;
top: 18px;
}
#cssmenu #menu-button::before {
display: block;
content: '';
position: absolute;
height: 3px;
width: 22px;
border-top: 2px solid #c7def0;
right: 25px;
top: 28px;
}
#cssmenu > ul > li.has-sub > a::after,
#cssmenu ul ul li.has-sub > a::after {
display: none;
}
}
这是jQuery:
(function($){
$(document).ready(function(){
$('#cssmenu').prepend('<div id="menu-button">Menu</div>');
$('#cssmenu #menu-button').on('click', function(){
var menu = $(this).next('ul');
if (menu.hasClass('open')) {
menu.removeClass('open');
} else {
menu.addClass('open');
}
});
});
})(jQuery);
推荐答案
这不是一个优雅的解决方案,但是我将其分成两列用于480像素宽的屏幕.
Not an elegant solution but I got this working in two columns for screen sizes above 480px wide.
@media only screen and (min-width: 480px) {
#cssmenu > ul > li > ul > div {
width: 200%;
}
#cssmenu > ul > li > ul > div > li {
display: inline-block;
width: 49%;
white-space: nowrap;
}
}
这篇关于CSS响应式下拉菜单-分为2列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!