CSS:悬停在移动设备或其他设备上作为切换 [英] CSS :hover on mobile or other device as toggle

查看:16
本文介绍了CSS:悬停在移动设备或其他设备上作为切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的 :hover 滑入式封面,如图所示,它应该滑入一个最喜欢的文章"控件,然后用户可以点击该控件来收藏这个项目.

虽然它在桌面上通过鼠标悬停和单击运行良好,但我不确定它是否可以用作移动设备或其他设备上的有效控件(即单击切换,然后再次单击收藏项).

如果我理解正确,至少在 iOS (Safari) 和 Android (Chrome) 上,默认浏览器行为是将触摸模拟为 hoverclick.但它是标准吗?例如.

  • Windows Phone 或 Wii U 也会这样做吗?
  • hover 会在 300 毫秒后触发 click,所以会出现幽灵点击问题吗?

我当然可以在这个元素上绑定一个点击/触摸事件,只是想知道现在 css :hover 是否足够了.

澄清:我不是在询问 :hover 支持,它只在指针驱动的环境中有意义.我在问设备是否可以并且应该在用户单击/点击时处理可悬停的元素(如 iOS/Android 那样)

解决方案

你的问题不是很清楚,我不明白你是否在问我可以在所有设备上使用 :hover?"或:hover 在所有设备上的行为是否相同?"或:hover 是网络上的标准元素吗?"

此外,这在很大程度上取决于您对所有设备"的概念,如果您考虑的是当前最常用的设备,或者您还考虑了鲜为人知和使用过的设备.

我将引用以下内容,但我很确定您已经阅读了:

<块引用>

交互式用户代理有时会更改渲染以响应用户操作.CSS 为常见情况提供了三个伪类:

:hover 伪类在用户指定元素时适用(使用一些指点设备),但不会激活它.例如,一个可视化用户代理可以在光标(鼠标指针)悬停在元素生成的框上.用户代理不支持交互媒体不必支持这个伪类.一些支持交互式媒体的符合要求的用户代理可能不是能够支持这个伪类(例如,笔设备).:active伪类在用户激活元素时适用.例如,在用户按下鼠标按钮和释放它.

CSS 没有定义哪些元素可能处于上述状态,或者如何进入和离开状态.脚本可能会改变元素是否对用户事件做出反应,并且不同的设备和 UA 可能有指向或激活元素的不同方式.

5.11.3 动态伪类::hover、:active 和 :focushttp://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes

正如您在 W3C 规范中看到的,它声称 :hover 伪类对于非交互式媒体用户代理以及某些交互式媒体用户代理.因此可以安全地假设 :hover 并不总是受支持.

要深入了解这个问题,请阅读以下 Safari Mobile 规范:

<块引用>

此外,iOS 上的 Safari 用户会与您的网络内容进行交互直接用手指,而不是使用鼠标.这会产生触控界面的新机会,但效果不佳悬停状态.例如,鼠标指针可以悬停在网页元素并触发事件;手指在 Multi-Touch 屏幕上不能.因此,鼠标事件在 iOS 上的 Safari 中被模拟.结果,仅依赖于 mousemove、mouseover、mouseout 的 元素或者 CSS 伪类 :hover 在触摸屏设备,例如 iPad 或 iPhone.

您可以直接处理触摸甚至检测高级手势iOS 上的 Safari,使用 DOM Touch 事件 touchstart、touchmove、touchend 和 touchcancel.与模拟的鼠标事件不同,DOM触摸事件专门设计用于触摸界面,所以他们的行为是可靠的和预期的.

5.准备触摸界面https://developer.apple.com/library/content/技术说明/tn2010/tn2262/_index.html

Apple 在此明确声明他们倾向于使用触摸手势来模拟指针,但是他们明确建议避免使用 :hover 伪类,因为在他们的触摸设备上的行为不会相同.

我们可以深入挖掘并获取地球上存在的每个用户代理的所有文档,但前两个足以假设以下内容:

  • 非交互式设备不必支持:hover
  • 交互式设备可以支持伪类(但这不是强制性的,他们可以忽略它,例如屏幕阅读器或盲文屏幕)
  • 没有指针的 Apple 触摸设备模拟 :hover
  • 可以安全地假设当前的触摸设备也模拟 :hover
  • 可以安全地假设任何其他浏览器/设备不一定必须支持 :hover,具体取决于它们的界面.
  • 很可能最近的浏览器都支持 :hover,因为这是对用户的视觉辅助.

为了回答我上面假设的所有问题:

<块引用>

:hover 是网络上的标准元素吗?"

Hover 是标准的 W3C,事实上它声称它必须由指针事件触发,对于某些接口来说不是必需的.

<块引用>

我可以在所有设备上使用 :hover 吗?"

是的,你可能可以.不支持 :hover 的设备很可能是可能不是您的主要目标的设备/用户.最好问问自己谁将成为我产品的最终用户?"如果他们只是移动用户或盲人或只是喜欢使用 Nintendo DS 浏览的人,则不要使用 :hover 事件,否则使用.

<块引用>

:hover 会在所有设备上表现相同吗?"

不,正如 Apple 在他们的设备上声明的那样,其行为与台式机不同,这可能反映了所有没有指针的设备上的相同行为.

如果您打算通过悬停状态进行用户操作,请不要这样做.这通常是不好的做法,在任何情况下都应该避免,包括桌面设备.悬停不是号召性用语,点击是.悬停不应该被视为切换",而更像是用户的视觉助手,让他/她理解该元素,如果被点击,就会触发一个动作.

如果我理解您的应用程序,那么悬停是不可靠的,在您的特定情况下,您应该重新考虑它应该如何工作.使用更可靠的方法(您的用户期望)

I am doing a simple :hover slide-in cover as shown in picture, it's supposed to slide in a "favorite article" control, which user can then click to favorite this item.

While it work well on desktop with mouse hover and click, I am not sure if it can be used as an effective control on mobile or other device (ie. click to toggle, then click again to favorite item) .

If I understand correctly, at least on iOS (Safari) and Android (Chrome), the default browser behaviour is to emulate touch as both hover and click. But is it a standard? eg.

  • Will Windows Phone or maybe a Wii U does the same?
  • Will click be fired about 300ms after hover, so there can be ghost click issue?

I can certainly bind a click/touch event on this element, just wondering if css :hover is sufficient nowadays.

To clarify: I am not asking about :hover support, which only make senses in a pointer driven environment. I am asking if devices can and should handle hover-able element as users click/tap (as iOS/Android do)

解决方案

Your question isn't totally clear and I cannot understand whether you're asking "Can I use :hover across all the devices?" or "Will :hover behave the same across all the devices?" or "Is :hover a standard element on the web?"

Also it greatly depends of your concept of "all devices", if you have in mind the currently most used devices or you are taking in account also the less-known and used devices.

I will quote you the following, but I am pretty sure you have already read that:

Interactive user agents sometimes change the rendering in response to user actions. CSS provides three pseudo-classes for common cases:

The :hover pseudo-class applies while the user designates an element (with some pointing device), but does not activate it. For example, a visual user agent could apply this pseudo-class when the cursor (mouse pointer) hovers over a box generated by the element. User agents not supporting interactive media do not have to support this pseudo-class. Some conforming user agents supporting interactive media may not be able to support this pseudo-class (e.g., a pen device). The :active pseudo-class applies while an element is being activated by the user. For example, between the times the user presses the mouse button and releases it.

CSS does not define which elements may be in the above states, or how the states are entered and left. Scripting may change whether elements react to user events or not, and different devices and UAs may have different ways of pointing to, or activating elements.

5.11.3 The dynamic pseudo-classes: :hover, :active, and :focus http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes

As you can see on the W3C specification it claims that the :hover pseudo-class is not required to a non-interactive media user agents as well as some interactive media user agents. Therefore is safe to assume :hover is not always supported.

To dig deep on the matter, take a read at the following specification for Safari Mobile:

Additionally, Safari on iOS 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 iOS. 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.

You can handle touches directly or even detect advanced gestures in Safari on iOS, using the DOM Touch events touchstart, touchmove, touchend, and touchcancel. Unlike mouse events which are emulated, DOM Touch events are specifically designed to work with touch interfaces, so their behavior is reliable and expected.

5. Prepare for a touch interface https://developer.apple.com/library/content/technotes/tn2010/tn2262/_index.html

Apple clearly states here that they tend to emulate the pointer with the touch gestures, however they clearly suggest to avoid using the :hover pseudo-class as won't behave the same on their touch device.

We could dig deeper and fetch every documentation for each user-agent existing on earth but the previous two are enough to assume the following:

  • Non interactive devices do not have to support :hover
  • Interactive devices can support the pseudo-class (but it's not mandatory and they can ignore it, for example screen-readers or braille screens)
  • Apple touch devices in absence of a pointer emulates :hover
  • It is safe to assume current touch devices also emulates :hover
  • It is safe to assume any other browser/device don't necessarily have to support :hover depending on their interface.
  • Very likely the recent browsers will all support :hover because is a visual aid for the user.

So to answer to all the question(s) I have assumed above:

"Is :hover a standard element on the web?"

Hover is a standard W3C in fact it claims it must be triggered by a pointer event, but isn't required for some interfaces.

"Can I use :hover across all the devices?"

Yes you probably can. The devices which won't support :hover very likely are devices/users that probably aren't your main target. Better ask yourself "Who will be the end-user of my product?" if they are only mobile users or only blind people or only people who like to browse using the Nintendo DS then don't use :hover events, otherwise do.

"Will :hover behave the same across all the devices?"

No, as Apple stated on their devices will not behave the same as a desktop would, and that probably reflects the same behaviour on all devices without a pointer.

If you plan to have an user action via a hover state don't do it. This is generally bad practice and it should avoided in any case, including desktop devices. Hover is not an call to action, click is. Hover should not be treated as a "toggle" but more like a visual helper for the user making him/her understand that element, if clicked, triggers an action.

If I understood your application then hover isn't reliable and in your specific case you should rethink on how it should work. Use a more reliable method (and expected from your user)

这篇关于CSS:悬停在移动设备或其他设备上作为切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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