简体   繁体   English

CSS下拉菜单在iPad上无法正常运行

[英]CSS dropdown menu not functioning as expected on iPad

I have a CSS-only dropdown menu that functions as expected on desktop. 我有一个仅CSS的下拉菜单,可以在桌面上正常运行。 On mobile Safari on an iPad, when one of the top-level navigation links is clicked, its dropdown menu shows briefly, but the link is followed, bringing the user to a new page. 在iPad上的移动Safari中,单击顶级导航链接之一时,会短暂显示其下拉菜单,但会跟随该链接,使用户进入新页面。

I have been reading everything I can find about this and it seems like the default behavior is supposed to be that the iPad will treat the first tap as a hover, and not actually follow the link until there is a second tap. 我一直在阅读有关此内容的所有资料,似乎默认行为是iPad会将第一次轻按视为悬停,直到第二次轻按才真正跟随链接。 This is because the :hover style alters the display property of its submenu, so the device knows not to follow the link on the first tap. 这是因为:hover样式会更改其子菜单的display属性,因此设备知道在第一次点击时不遵循链接。

I've seen this behavior working appropriately on other sites, and have even seen it working correctly on other parts of my website, but for some reason, it does not behave correctly with my CSS dropdown menu. 我已经看到这种行为在其他站点上可以正常运行,甚至可以在我的网站的其他部分上正常运行,但是由于某些原因,它在CSS下拉菜单中无法正常运行。

The menu can be seen on my site, https://www.storeyourboard.com . 可以在我的网站https://www.storeyourboard.com上看到菜单。 There is a horizontal menu listing category names near the top of the page. 在页面顶部附近有一个水平菜单,列出了类别名称。 If you view this on an iPad, and tap on one of the menu items, for example "Surfboard", you will see that the iPad navigates to the Surfboard page, rather than displaying the dropdown menu on first tap. 如果您在iPad上查看此内容,然后点击一个菜单项,例如“ Surfboard”,您将看到iPad导航到Surfboard页面,而不是在第一次点击时显示下拉菜单。 Strangely, there have been times that my tapping randomly behaves as expected, and the dropdown menu displays without navigating, but that has been rare, and I can't reproduce it consistently. 奇怪的是,有时候我的点击随机地表现出预期的效果,并且下拉菜单显示时没有导航,但这很少见,而且我无法始终如一地再现它。

This is the relevant markup for the menu: 这是菜单的相关标记:

HTML 的HTML

<ul class="mainNav-menu">
    <li>
        <a href="/sup-racks-bags/">SUP</a>
        <div class="mainNav-submenu">
            <ul class="mainNav-submenu-menu">
                <!-- Submenu items here -->
            </ul>
        </div>
    </li>
    <li>
        <a href="/surfboard-racks-bags/">Surfboard</a>
        <div class="mainNav-submenu">
            <ul class="mainNav-submenu-menu">
                <!-- Submenu items here -->
            </ul>
        </div>
    </li>
</ul>

CSS 的CSS

.mainNav-submenu {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: auto;
    background-color: #8a8a8a;
    z-index: 20;
    padding: 1rem;
}

.mainNav-menu > li:hover .mainNav-submenu {
    display: block;
}

I've seen some other posts saying that adding onclick="return true" to the menu links or the menu container will fix it, but this did not have any effect for me. 我看到其他一些帖子说,在菜单链接或菜单容器中添加onclick="return true"可以解决此问题,但这对我没有任何影响。 I should also mention that I'm using fastclick.js on the site. 我还应该提到我在网站上使用fastclick.js I thought this could be causing the issue, but when I remove it, I see the exact same behavior. 我以为这可能是问题的原因,但是当我删除它时,我看到的是完全相同的行为。

I also added the code from this article to my site just to see if it would behave correctly, and it did. 我还将本文中的代码添加到我的站点中,只是为了查看它是否可以正确运行,并且确实可以。 In other words, I added this small snippet of code to my site: 换句话说,我将以下代码片段添加到我的网站中:

<style>
    p {height: 100px;}

    p span {
        display: none;
    }

    p:hover span {
        display: block;
    }
</style>

<p>
    <a href="/">Tap me</a>
    <span>You tapped!</span>
</p>

This worked correctly (even with fastclick active). 这可以正常工作(即使在fastclick处于活动状态时)。 If I tapped on the link, the "You tapped" span showed, and if I tapped again, I navigated away. 如果我点击该链接,则显示“ You tapped”跨度,如果我再次点击,则导航离开。

I can not figure out why this same behavior is not working correctly for my navigation menu. 我无法弄清楚为什么相同的行为无法在我的导航菜单中正常工作。 Is it something in the way I have it marked up or styled? 是否以我标记或设置样式的方式进行? Does anyone have any idea? 有人有什么主意吗? I'm not sure what else to do. 我不确定该怎么办。

UPDATE 更新

Thanks to Doc's answer below, I was able to come up with this solution: 多亏了以下Doc的回答,我才能够提出以下解决方案:

.mainNav-menu > li > a {
  pointer-events: none;
}

.mainNav-menu > li:hover > a {
  pointer-events: all;
}

It's live on the site now and you can see it in action. 现在它已在网站上直播,您可以看到它的运行情况。 I still don't know why this is necessary and why it didn't work originally, so if anyon has any ideas, please let me know. 我仍然不知道为什么这是必要的,为什么它本来就不起作用,所以如果有人有任何想法,请告诉我。

I don't know why tapping the a link causes it to be followed, but this should still solve the problem. 我不知道为什么敲击a链接使其遵循,但还是应该解决这个问题。


First, make it so that there is a regular piece of text on all of these tags, as well as the link. 首先,确保所有这些标签以及链接上都有规则的文本。 So keep 所以保持

<a href="/surfboard-racks-bags/">Surfboard</a>

but also add 还要加上

<p>Surfboard</p>

Make sure that both of these elements have the same content and style 确保这两个元素具有相同的内容和样式

Then, make the link have display: none by default, so its hidden. 然后,使链接display: none默认情况下display: none ,因此将其隐藏。 But when the div is hovered, make the link visible (with something like display: inline ) and give the other non-link display: none . 但是当div悬停时,使链接可见(如display: inline ),并给另一个非链接display: none Ideally the p element should seamlessly be replaced by the a element, which should be styled the exact same, as if the a was there all along 理想情况下,应该将p元素无缝地替换为a元素,其样式应完全相同,就像a一直存在

For the desktop, when the user isn't hovering the div, there's no reason that the "link" part of the link needs to be active, so it's substituted for regular text. 对于台式机,当用户不悬停div时,没有理由需要激活链接的“链接”部分,因此可以用普通文本代替。 When the user is hovering the div, than the link shows up, so he/she can use it. 当用户将鼠标悬停在div上时,链接就会显示出来,因此他/她可以使用它。

However, when an iOS user taps the div, he/she should trigger the :hover effect, but not the link; 但是,当iOS用户点击div时,他/她应触发:hover效果,但触发链接; since the link wasn't visible when he/she tapped, it shouldn't be triggered. 由于当他/她点击链接时不可见,因此不应触发该链接。 If the user still is clicking on the link, than add something like transition: display 0s 0.1s to the a and p elements, since the link might be shown too quickly 如果用户仍在单击链接,则添加诸如transition: display 0s 0.1sap元素上transition: display 0s 0.1s ,因为链接显示可能太快了

The only problem with this approach is that once the iOS user opens the dropdown, he/she can't close it by clicking on it again; 这种方法的唯一问题是,一旦iOS用户打开了下拉菜单,他/她将无法通过再次单击来关闭它。 that would trigger the link. 这将触发链接。 But the user could click somewhere else and it should close 但是用户可以单击其他位置,它应该关闭

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

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