是否有可能创建漂亮的多级菜单,该菜单适用于desctop和触摸屏浏览器?没有任何js。
由于触摸屏没有:hover
属性,因此为两种变体创建一个样式表让我发疯。
在桌面上的行为应该是:在悬停 - 打开子菜单,点击 - 转到链接。
在iPad上:在第一次点击 - 打开子菜单,第二次点击 - 转到链接。
感谢您的任何建议。
答案 0 :(得分:3)
使用纯CSS ,定位:hover
和:active
即可完成您的目标。
在iPhone / iPad上试用此网站:www.thepricklepatch.com。它并不完美,但我正在做的是创造我想要的效果。
修改强> 对不起,我花了很长时间才回复 - 本周末露营,不允许电脑! :)这是该网站的CSS,被剥离以获得您想要的位数。我已经为你添加了评论。这里有一些空的规则,我用它来设置菜单的样式,我删除了任何处理纯粹外观的CSS,并专注于元素的定位。
/* NAVIGATION ************************************************************************/
nav > ul > li
{
display: inline-block; /* sets elems to be inline but retain block-like properties, you may not need this */
position: relative; /* resets the positioning context */
cursor: pointer; /* sets the mouse cursor to look like a link for menus that do not have an A element */
}
nav > ul > li:hover
{
position: relative; /* reset positioning context */
z-index: 2000; /* bring element top-most */
}
nav > ul > li:hover,
nav > ul > li:hover > a /* HOVERED list elems and links in the main menu */
{
}
nav > ul > li,
nav > ul > li > a /* list elems and links in the menu */
{
}
nav > ul > li > div.submenu /* only first level of submenus */
{
display: none; /* hide the submenus by default */
}
nav > ul > li:hover > div.submenu /* Only HOVERED OVER first level of submenus */
{
display: block; /* display the submenus when the LI is hovered over */
position: absolute;
top: 100%; /* set to the bottom of the menu */
left: -1px; /* move left 1px from left of LI element */
z-index: 1000; /* top most */
}
nav > ul div.submenu /* All submenus */
{
}
nav > ul div.submenu > ul /* All lists of links in a submenu */
{
background: #fff;
padding: 2px;
border: solid 2px #f89d57;
}
nav > ul div.submenu > ul > li, /* All list elements in any submenu */
nav > ul div.submenu > ul > li > a /* All list elements containing links in any submenu */
{
}
nav > ul div.submenu > ul > li
{
}
nav > ul div.submenu > ul > li > a
{
display: block;
}
nav > ul div.submenu > ul > li,
nav > ul div.submenu > ul > li > a /* All links in any submenu */
{
}
nav > ul div.submenu > ul > li:hover /* All HOVERED li containing links in any submenu */
{
}
nav > ul div.submenu > ul > li:hover > a /* All links HOVERED in any submenu */
{
}
nav div.submenu > ul > li > div.submenu,
nav div.submenu > ul > li > a + div.submenu /* 2nd level of submenus and on */
{
display: none; /* Hide by default submenus */
}
nav div.submenu > ul > li:hover div.submenu,
nav div.submenu > ul > li:hover a + div.submenu /* 2nd level submenus and on (if even used) */
{
display: block; /* Show submenus when parent hovered */
position: absolute;
top: -2px;
left: 75%;
z-index: 1000;
}
nav li:hover
{
position: relative;
z-index: 2000;
}
供参考,这是我使用的HTML:
<nav>
<ul>
<li><a href="/index.aspx">Home</a></li>
<li><a href="/shop-by.aspx">Shop By…</a>
<div class="submenu">
<ul>
<li>Shop By Product
<div class="submenu">
<ul>
<li><a href="/products.aspx?category=furniture">Furniture</a></li><li><a href="/products.aspx?category=bed-bath">Bed & Bath</a></li><li><a href="/products.aspx?category=decor-accents">Décor Accents</a></li><li><a href="/products.aspx?category=tabletops">Tabletops</a></li><li><a href="/products.aspx?category=rugs-curtains">Rugs & Curtains</a></li><li><a href="/products.aspx?category=home-office">Home Office</a></li>
</ul>
</div>
</li>
<li>Shop By Room
<div class="submenu">
<ul>
<li><a href="/products.aspx?room=bathroom">Bathroom</a></li><li><a href="/products.aspx?room=bedroom">Bedroom</a></li><li><a href="/products.aspx?room=dining">Dining</a></li><li><a href="/products.aspx?room=kitchen">Kitchen</a></li><li><a href="/products.aspx?room=living-room">Living Room</a></li><li><a href="/products.aspx?room=media-office">Media & Office</a></li>
</ul>
</div>
</li>
<li>Shop By Color
<div class="submenu">
<ul>
<li><a href="/products.aspx?color=black">Black</a></li><li><a href="/products.aspx?color=blue">Blue</a></li><li><a href="/products.aspx?color=brown">Brown</a></li><li><a href="/products.aspx?color=green">Green</a></li><li><a href="/products.aspx?color=orange">Orange</a></li><li><a href="/products.aspx?color=purple">Purple</a></li><li><a href="/products.aspx?color=red">Red</a></li><li><a href="/products.aspx?color=white">White</a></li><li><a href="/products.aspx?color=yellow">Yellow</a></li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li><a href="/services.aspx">Services</a>
<div class="submenu">
<ul>
<li><a href="/">Home Consultation</a></li>
<li><a href="/">Interior Design</a></li>
<li><a href="/">Floral Design</a></li>
<li><a href="/">Event Planning</a></li>
</ul>
</div>
</li>
<li><a href="/galleries.aspx">Gallery</a>
<div class="submenu">
<ul>
<li><a href="/">Rooms</a></li>
<li><a href="/">Floral</a></li>
<li><a href="/">Christmas</a></li>
<li><a href="/">Other Seasons</a></li>
</ul>
</div>
</li>
<li><a href="/promotions.aspx">Promotions</a>
<div class="submenu">
<ul>
<li><a href="/">Semi-Annual</a></li>
<li><a href="/">Sale</a></li>
<li><a href="/">Sale by Season</a></li>
</ul>
</div>
</li>
<li><a href="/contact-us.aspx">Contact Us</a>
<div class="submenu">
<ul>
<li><a href="/">Contact Info</a></li>
<li><a href="/">Comments</a></li>
<li><a href="/">Inquiries</a></li>
</ul>
</div>
</li>
<li class="navWishList"><a href="/wish-list.aspx">Wish List</a></li>
</ul>
</nav>
答案 1 :(得分:1)
如果您的主菜单节点无法链接到任何地方,您可以使用JavaScript执行此操作而不会有太多麻烦。您可以将其添加为点击功能,这可以在触摸设备上使用。
您也可以使用:focus和:与兄弟选择器结合使用的活动状态。这将允许菜单显示键盘导航,这是一个加号,但你必须添加一些JavaScript,以便当你选择子菜单链接时它不会隐藏。
<ul>
<li>
<a class="menuLink" href="javascript:void(0);">Menu 1</a>
<ul class="submenu">
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
</ul>
</li>
<li>
<a class="menuLink" href="javascript:void(0);">Menu 2</a>
<ul class="submenu">
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
</ul>
</li>
<li>
<a class="menuLink" href="javascript:void(0);">Menu 3</a>
<ul class="submenu">
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
</ul>
</li>
<li>
<a class="menuLink" href="javascript:void(0);">Menu 4</a>
<ul class="submenu">
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
<li><a href="page">Submenu Link</a></li>
</ul>
</li>
<ul>
使用这样的CSS:
.submenu { display:none; }
.menuLink:hover + .submenu,
.menuLink:focus + .submenu,
.menuLink:active + .submenu {
display:block;
}