触摸屏(iPad)的纯CSS分层菜单 [英] Pure CSS hierarchical menu for touchscreens(iPad)

查看:191
本文介绍了触摸屏(iPad)的纯CSS分层菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建漂亮的多层次菜单,将在桌面和触摸屏浏览器上工作?没有任何js。
由于触摸屏没有:hover 属性,我为这两个变体创建一个样式表变得疯狂。
在桌面上的行为应该是:on hover - 打开子菜单,点击 - 转到链接。
在iPad上:首次点按 - 打开子菜单,第二次点击 - 转到链接。

Is it possible to create nice looking multi-level menu which will work on desctop and touchscreen browsers? Without any js. Since touchscreen haven't :hover property it is getting me crazy to create one stylesheet for both variants. On desktop behavior should be: on hover - open submenu, on click - go to link. On iPad: on first tap - open submenu, on second tap - go to link.

感谢任何建议。

推荐答案

对于纯CSS ,目标:hover

With pure CSS, target :hover AND :active to accomplish what you want.

在您的iPhone / iPad上试用这个网站:www。 .thepricklepatch.com。这不是完美的,但这是我正在做的,以创造我想要的效果。

Try this site out on your iPhone/iPad: www.thepricklepatch.com . It's not perfect but that's what I am doing to create the effect I want.

编辑:
对不起,我回复 - 是露营这个周末,没有电脑允许! :)这里是该网站的CSS,剥离下来得到你想要的位。我已经为您添加了评论。在这里有一些空的规则,我用来对菜单进行样式化,我删除了处理纯粹外观并专注于元素定位的任何CSS。

Sorry it took so long for me to reply - was camping this weekend, no computer allowed! :) Here's the CSS for that site, stripped down to get the bits you want. I've added comments for you. There are empty rules in here that I used to stylize the menus, and I removed any CSS that dealt with pure looks and focused on the positioning of elements.

/* 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:

For reference, here is the HTML I used:

    <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>

这篇关于触摸屏(iPad)的纯CSS分层菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆