简体   繁体   English

禁用锚菜单点击移动设备

[英]Disable anchor menu click on mobile devices

I'm using a nested list as a menu with sub menu items.我使用嵌套列表作为带有子菜单项的菜单。 I used to do it where if you hovered over main menu item, the sub menu items would appear by changing the display from none to block.我曾经这样做过,如果您将鼠标悬停在主菜单项上,则会通过将显示从无更改为阻止来显示子菜单项。 I decided to make the sub menus looks as if they were dropping down and used CSS transitions.我决定让子菜单看起来像是在下拉并使用 CSS 转换。

The problem I have is that in the first approach if you touched a main menu item on an iPad the sub menu would display and you would touch it again to follow the link.我遇到的问题是,在第一种方法中,如果您在 iPad 上触摸主菜单项,子菜单将显示,您将再次触摸它以点击链接。 With the new approach it follows the link on the iPad and you don't get to see the sub menu.使用新方法,它遵循 iPad 上的链接,而您看不到子菜单。

The menus that have nothing below them (sub menu items and main menu items with no sub menu items) have one class while the main menu items with a sub menu below them have a different class.下面没有任何内容的菜单(子菜单项和没有子菜单项的主菜单项)有一个类,而在它们下面有子菜单的主菜单项有一个不同的类。

Is there any way I can set it so that hovering over the menu would show the sub menu items on a desktop and clicking would follow the link while touching a main menu item on an iPad would follow the link if there was no sub menu items but follow the link on a second touch and show the sub menu on the first touch when there is a sub menu?有什么方法可以设置它,以便将鼠标悬停在菜单上会显示桌面上的子菜单项,单击将跟随链接,而触摸 iPad 上的主菜单项将跟随链接,如果没有子菜单项,但是在第二次触摸时点击链接并在有子菜单时在第一次触摸时显示子菜单?

    <div id="menu">
    <div class="mainmenu">
         <ul>
            <li class='menu_child'>
                <a href=''>Home</a>
            </li>
            <li class='menu_child'>
                <a href=''>Blog</a>
            </li>
            <li class='menu_parent'>
                <a href=''>Contact Us</a>
                <ul>
                    <li>
                        <a href='' >Find Us</a>
                </li>
                <li>
                    <a href='' >About Us</a>
                </li>
                <li>
                    <a href='' >Meet the Team</a>
                </li>
            </ul>
        </li>
        <li class='menu_parent'>
            <a href=''  >Adventure</a>
            <ul>
                <li>
                    <a href='' >Adventurer Grandmaster</a>
                </li>
            </ul>
        </li>
        <li class='menu_child'>
            <a href='' >Guilds</a>
        </li>
        <li class='menu_parent'>
            <a href='' >Trade</a>
            <ul>
                <li>
                    <a href='' >Moonsea</a>
                </li>
                <li>
                    <a href='' >Hillsfar</a>
                </li>
                <li>
                    <a href='' >Femphrey</a>
                </li>
                <li>
                    <a href='' >Anvil</a>
                </li>
                </ul>
            </li>
        </ul>
    </div>
</div>

First approach第一种方法

    .mainmenu ul ul{
float: left;
padding: 0;
position: absolute;
text-align: left;
width: 274px;
z-index: 1001;
display: none;
}

.mainmenu ul li:hover ul, .mainmenu li.over ul {
display: block;
}

Second approach第二种方法

.mainmenu ul ul{
float: left;
padding: 0;
position: absolute;
text-align: left;
width: 274px;
z-index: 1001;
    height: auto;
max-height: 0;
overflow: hidden;
-webkit-transition: max-height 0.3s ease-in;
    -moz-transition: max-height 0.3s ease-in;
    -o-transition: max-height 0.3s ease-in;
    -ms-transition: max-height 0.3s ease-in;
    transition: max-height 0.3s ease-in;
}

.mainmenu ul li:hover ul, .mainmenu li.over ul{
max-height: 999px;
-webkit-transition-duration:1s;
    -moz-transition-duration:1s;
    -o-transition-duration:1s;
    -ms-transition-duration:1s;
    transition-duration:1s;
}

I've adding focus and active pseudo-classes to the hover bit.我已将焦点和活动伪类添加到悬停位。

I'm willing to use JavaScript but I'd rather do it with CSS, preferably without changing the HTML.我愿意使用 JavaScript,但我更愿意使用 CSS,最好不要更改 HTML。 I'm willing to use PHP as a last resort.我愿意使用 PHP 作为最后的手段。

http://jsbin.com/cilapi/2/ http://jsbin.com/cilapi/2/

The trick is to prevent a click on any .menu_parent element's first inner a anchor using CSS pointer-events: none;关键是要防止任何一个点击.menu_parent元素的第一内a使用CSS锚pointer-events: none; on mobile在移动

@media (max-width: 768px)  { /* Small devices */

  li.menu_parent > a {    // If LI element is parent to a submenu
    pointer-events: none; // prevent it's immediate A child to follow HREF
    cursor: default;     
  }

}

:hover will still work as usual on larger screens and a click will follow a link. :hover在更大的屏幕上仍然可以正常工作,点击后会出现一个链接。
On smaller @media devices - any element that is .menu_parent parent will have clicks disabled on it's > immediate a child, allowing the sub menu to open (instead of triggering the HREF follow)在较小的@media设备-这是任何元素.menu_parent家长将已禁用点击它是>直接a孩子,让子菜单打开(而不是触发HREF后续)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM