gpt4 book ai didi

css - 用于触摸屏的纯 CSS 分层菜单(iPad)

转载 作者:行者123 更新时间:2023-11-28 10:55:55 25 4
gpt4 key购买 nike

是否可以创建适用于桌面和触摸屏浏览器的漂亮的多级菜单?没有任何js。由于触摸屏没有 :hover 属性,因此为这两种变体创建一个样式表让我发疯。在桌面上的行为应该是:悬停 - 打开子菜单,点击 - 转到链接。在 iPad 上:第一次点击 - 打开子菜单,第二次点击 - 转到链接。

感谢任何建议。

最佳答案

使用纯 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&hellip;</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>

关于css - 用于触摸屏的纯 CSS 分层菜单(iPad),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10247336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com