javascript - iOS 设备处理 :hover pseudo-class? 的默认方式是什么

标签 javascript ios css css-selectors pseudo-class

我有一个可以点击的 div(通过 jQuery $('.mydiv').click )——这会展开 div,第二次点击会折叠它。

同一个 div 应用了一些 CSS :hover 规则(即在悬停时显示更多信息)——这在 PC 上可以正常工作。

当使用 iOS 设备时,点击有效,并且还应用了 :hover 伪类 - 所以我的额外信息会显示出来。这几乎正​​是我想要发生的事情(令人惊讶)。

但是,仅在 CSS 中包含带有 :hover 伪类的内容并不意味着单击会显示它。

那么 iOS 应用(或不应用):hover 状态所遵循的规则是什么?是否有一些文档可以查看?

我真的不想在悬停下隐藏功能,而是必须编写一些特定于触摸的 JavaScript 才能使它们正常工作,因为 iOS 似乎已经为我做了一些工作 - 我只是想知道如何使用它具体功能,而不仅仅是发生在它身上!

最佳答案

您可能已经知道,iOS 实际上并不支持 :hover 伪类,因为它使用了基于触摸的界面。出于兼容性原因,它确实会在特定情况下触发鼠标事件,但这种行为并不是模拟或触发元素的 :hover 状态的最可靠方式,因为您实际上无法将元素悬停在手指的方式与使用鼠标指针的方式相同。来自 this tech note :

Additionally, Safari on iPhone OS users interact with your web content directly with their fingers, rather than using a mouse. This creates new opportunities for touch-enabled interfaces, but does not work well with hover states. For example, a mouse pointer can hover over a webpage element and trigger an event; a finger on a Multi-Touch screen cannot. For this reason, mouse events are emulated in Safari on iPhone OS. As a result, elements that rely only on mousemove, mouseover, mouseout or the CSS pseudo-class :hover may not always behave as expected on a touch-screen device such as iPad or iPhone.

也就是说,由于鼠标事件确实对应于 CSS 中基于鼠标的 UI 状态,因此在文档中对它们进行了一定程度的介绍,但不多。我确实找到了一些东西 here :

The following web technologies are not supported on iOS:

  • ...

  • Mouse-over events

    The user cannot “mouse-over” a nonclickable element on iOS. The element must be clickable for a mouseover event to occur as described in “One-Finger Events.”

  • Hover styles

    Since a mouseover event is sent only before a mousedown event, hover styles are displayed only if the user touches and holds a clickable element with a hover style. Read “Handling Events” for all the events generated by gestures on iOS.

这意味着 :hover 仅在可点击元素上受支持。可点击事件定义here作为:

A clickable element is a link, form element, image map area, or any other element with mousemove, mousedown, mouseup, or onclick handlers.

话虽如此,如果您想在 iOS 上提供一致且可靠的用户体验,最好的选择是针对触控进行设计,因为它就是为此而生的。

关于javascript - iOS 设备处理 :hover pseudo-class? 的默认方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17188558/

相关文章:

ios - NSCoding 自定义对象子数组未持久化

jquery - CSS - IE7 和 8 问题

javascript - 如何修改我的数据模型以具有计算属性?

javascript - 可以使用 bxslider getCurrentSlide 方法将类分配给事件幻灯片吗?

objective-c - 在我的应用程序 IOS 中嵌入流媒体播放器

css - Grails Twitter Bootstrap 插件分页显示困惑

html - 如何将跨度放在另一个跨度下,以便编写简单的数学?

javascript - 除了使用API​​之外,是否还有其他方法可以从外部网页获取数据来开发比较购物网站?

javascript - Parsley.js 如果值为空,请勿使用远程验证

ios - Xcode8 Storyboard如何在景观中只设置一个 View Controller ?