Safari Mobile:触摸时切换内容

发布于 2024-12-08 04:24:24 字数 578 浏览 1 评论 0原文

我正在改编一个网站,以便让它在 iPad 上具有原生的感觉。

该网站的导航显示一个下拉菜单,悬停时有子导航。 这在 iPad 上就像一个魅力。当您触摸子导航时,它会在您单击/触摸其他地方时再次打开和关闭。

现在我需要在再次触摸导航点时使其再次关闭。

我在想,我可以在悬停时设置 pointer-events:none 。对于 iPad 有效,但这会使子导航闪烁并且不起作用...

我还尝试使用之前选择器设置的元素覆盖导航点并将其指针事件设置为无,但这不起作用...

任何想法,我如何仅使用 CSS 解决这个问题。 (我无法修改 HTML 或 JS)

PS:您可以在 www.macprime.ch 上重现此内容例如...(单击顶部的主导航,然后尝试再次关闭下拉菜单)

编辑 好的,我几乎尝试了仅使用 CSS 所能实现的一切。我认为这是不可能的。如果有人能告诉我原因,他/她将获得赏金奖励。

I am adapting a website in order to make it feel native on the iPad.

This website has navigation that shows a drop-down with the sub-navigation on hover.
This works like a charm on the iPad. When you touch it the subnav, it opens and closes again when you click/touch somewhere else.

Now i have the requirement to make it close again when the navigation point is touched again.

I was thinking, i could just set the pointer-events:none on hover & active for the iPad, but this makes the sub-navigation flicker and it does not work...

i have also tried to cover the navigation point with element set with the before selector and setting its pointer events to none, but this does not work...

Any idea, how i could solve this problem using CSS only. (I can not modify the HTML nor the JS)

PS: you can reproduce this on www.macprime.ch for example... (click the main navigation on the top, and try to close the dropdown again)

edit ok i tried almost everything that was possible with CSS only. I don't think its possible. If anyone can tell me why, he/she will get the bounty reward.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-12-15 04:24:24

您可以有第二个透明元素出现在您点击的元素上方。这样,当用户再次点击时,他们将选择另一个元素,第一个元素将失去悬停状态:

<div class="blocker" onclick="void()"></div>
<div class="menuItem" onclick="void()"></div>

<style>
    .blocker, .menuItem {
        /* use the same position, width, height, etc */
    }
    .menuItem {
        /* make it look pretty */
        z-index: 100;
    }
    .blocker {
        z-index: 99;
    }
    .menuItem:hover {
        z-index: 98;
    }
</style>

当然,这会对桌面产生负面影响,因此您需要执行以下操作:

<style>
    .blocker { display: none; }
    .touchevents .blocker { display: block; }
</style>
<script>
    if('ontouchstart' in document)
        document.body.className += ' touchevents';
</script>

更新添加了 onclick 事件以使它们可点击。

您可以在此处查看工作演示:http://fiddle.jshell.net/vfkqS/6/

不幸的是,我找不到不需要 HTML 或 JavaScript 更改的解决方案,但我能够将它们保持在最低限度。

您总共需要进行两项非 CSS 更改:

  1. 添加一个 JavaScript 机制来识别是否支持触摸事件。请参阅上面的两行示例。
  2. 每个菜单添加一个可点击的 div (onclick="void()"),并具有可将其链接到菜单的唯一标识符。

也许能够使用 CSS 完成这两件事,但我不确定。平板电脑检测在 CSS 中有点粗略,我认为你无法使用 :before 或 :after 伪选择器做出如此复杂的东西。

You could have a second transparent element that appears above the one you tapped. That way, when the user taps again, they will be selecting the other element and the first will lose its hover status:

<div class="blocker" onclick="void()"></div>
<div class="menuItem" onclick="void()"></div>

<style>
    .blocker, .menuItem {
        /* use the same position, width, height, etc */
    }
    .menuItem {
        /* make it look pretty */
        z-index: 100;
    }
    .blocker {
        z-index: 99;
    }
    .menuItem:hover {
        z-index: 98;
    }
</style>

Of course, this will have a negative effect on the desktop, so you will want to do something like:

<style>
    .blocker { display: none; }
    .touchevents .blocker { display: block; }
</style>
<script>
    if('ontouchstart' in document)
        document.body.className += ' touchevents';
</script>

UPDATE Added onclick events to make them clickable.

You can see a working demo here: http://fiddle.jshell.net/vfkqS/6/

Unfortunately, I could not find a solution that does not require HTML or JavaScript changes, but I was able to keep them to a minimum.

You would need to make two non-CSS changes total:

  1. Add a JavaScript mechanism for identifying if touch events are supported. See two line example above.
  2. Add one div per menu which is clickable (onclick="void()") and has a unique identifier that can link it to the menu.

You may be able to do those two things with CSS but I'm not sure. Tablet detection would be a little sketchy in CSS and I don't think you can make something that sophisticated with a :before or :after pseudo-selector.

二货你真萌 2024-12-15 04:24:24

这是一个有趣的问题,与我最近提出的问题类似。如何将悬停时显示的标准导航下拉菜单与触摸事件界面结合起来。使用悬停事件作为触发器在桌面上效果非常好。在没有悬停事件(平板电脑和智能手机)的世界中,情况并非如此。

就我而言,我想到了定义行为的想法:单击/触摸事件将执行触发,悬停事件将执行微妙的指示。有关这一思路的更多详细信息,请参阅:http://www.markdotto.com/2012/02/27/bootstrap-explained-dropdowns/" rel="nofollow"> markdotto.com/2012/02/27/bootstrap-explained-dropdowns/

对于您试图解决的问题,我想知道是否使用 @media 查询你的 CSS 是一个更好的解决方案...

@media (max-width: 979px) {

    /*
        New CSS declarations to show the sub navigation lists
        in a more compact way that fits nicely on the iPad.

        Something like...
    */

    section.nav-overlay {

        display: block;
        height: 60px;
        visibility: visible;
        width: 979px; /* Or the max container width */

    }

        section.nav-overlay ul li {

            float: left;

        }

        /*

            Etc. etc. with the additional exceptions.
            You get the idea.

        */

}

通过这样做,你可以在 iPad 上创建更多的原生界面。然而,如果走这条路是不可能的,那么像布莱恩上面的那样的东西会更好。只是想给你一个替代方案。

This is an interesting question and similar to one I've had come up recently. How do you marry a standard navigation dropdown that displays on hover with a touch event interface. Using the hover event as a trigger works really well on a desktop. In a world without hover events (tablets and smart phones), not so much.

In my case I landed on the idea of defining the behaviors: click/touch event would do the triggering, hover event would do subtle indications. For more details on this line of thinking see: http://www.markdotto.com/2012/02/27/bootstrap-explained-dropdowns/

For the issue you're trying to overcome I'm wondering if using @media queries in your CSS is a better solution...

@media (max-width: 979px) {

    /*
        New CSS declarations to show the sub navigation lists
        in a more compact way that fits nicely on the iPad.

        Something like...
    */

    section.nav-overlay {

        display: block;
        height: 60px;
        visibility: visible;
        width: 979px; /* Or the max container width */

    }

        section.nav-overlay ul li {

            float: left;

        }

        /*

            Etc. etc. with the additional exceptions.
            You get the idea.

        */

}

By doing this you would create more of a native interface on the iPad. However, if going this route is off the table, something like what Brian has above is better. Just wanted to give you an alternative.

回心转意 2024-12-15 04:24:24

在活动状态上设置pointer-events: none

nav#mainnavi > ul > li > a:active {
  pointer-events: none
}

Set pointer-events: none on the active state:

nav#mainnavi > ul > li > a:active {
  pointer-events: none
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文