[英]Accessibility - triggering hover and focus events on TAB key press
I've been dabbling in simple CSS transitions and hover events etc recently. 我最近一直在尝试简单的CSS过渡和悬停事件等。 I notice that when you press the TAB key it generally finds links which is fine but...
我注意到,当您按TAB键时,通常可以找到链接,但是...
If I have a hover event, like a piece of text is revealed or something similar, how can I ensure that pressing the TAB key will trigger hover and or focus events? 如果发生悬停事件,例如显示了一段文字或类似内容,如何确保按TAB键将触发悬停和/或焦点事件?
This is because I have a page full of squares made up of DIVs
that look similar to this: 这是因为我有一个充满正方形的页面,这些正方形由类似于以下内容的
DIVs
:
When you hover over this block with your mouse it changes color via a hover event, essentially to visually inform the user that the element is in some way interactive. 当您将鼠标悬停在此块上时,它会通过悬停事件来更改颜色,本质上是在视觉上通知用户该元素以某种方式是交互式的。
Is there a way I could trigger the hover event with the TAB key or even the arrow keys? 有没有一种方法可以用TAB键甚至箭头键触发悬停事件? My reasoning is because if for some reason you did not have a mouse or touch device you could potentially miss out on content.
我的理由是,如果由于某种原因您没有鼠标或触摸设备,则可能会错过内容。
Amending my question slightly 稍微修改我的问题
So the TAB key is treated as a :focus
event and works well when you give a link a :hover
state but is it possible for the TAB key to acknowledge DIV elements? 因此,TAB键被视为
:focus
事件,并且在为链接赋予:hover
状态时效果很好,但是TAB键是否有可能确认DIV元素?
With CSS you can use also :focus
, try this: 使用CSS时,您也可以使用
:focus
,请尝试以下操作:
div { float:left; margin:2px; } a { display:block; height:100px; width:100px; line-height:100px; text-align:center; background:purple; color:white; transition:.3s linear; } a:hover, a:focus { background:orange; }
<div><a href="">item1</a></div> <div><a href="">item2</a></div> <div><a href="">item3</a></div> <div><a href="">item4</a></div> <div><a href="">item5</a></div>
This is with respect to your last comment: 这是关于您的最后评论:
So the TAB key is treated as a :focus event and works well when you give a link a :hover state but is it possible for the TAB key to acknowledge DIV elements ?
因此,TAB键被视为:focus事件,并且当您为链接赋予:hover状态时效果很好,但是TAB键是否有可能确认DIV元素 ?
I believe you are looking for tabindex="0"
. 我相信您正在寻找
tabindex="0"
。 Adding that attribute will make your element capable of receiving focus. 添加该属性将使您的元素能够获得焦点。
So <div tabindex="0">Hello World</div>
因此
<div tabindex="0">Hello World</div>
The tabindex
doesn't have to be 0. It can be negative, 0, or a possitive integer following these rules: 所述
tabindex
不必为0。它可以是负的,0,或按照下列规则独到之处整数:
From MDN on tabindex 从TabIndex的MDN
- a negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation;
负值表示该元素应该是可聚焦的,但不能通过顺序键盘导航来访问;
- 0 means that the element should be focusable and reachable via sequential keyboard navigation, but its relative order is defined by the platform convention;
0表示该元素应该是可聚焦的并且可以通过顺序键盘导航到达,但是其相对顺序由平台约定定义;
- a positive value means should be focusable and reachable via sequential keyboard navigation;
正值表示应该有重点并且可以通过顺序键盘导航来访问; its relative order is defined by the value of the attribute: the sequential follow the increasing number of the tabindex.
它的相对顺序由属性的值定义:依次跟随tabindex的数量递增。 If several elements share the same tabindex, their relative order follows their relative position in the document.
如果多个元素共享相同的tabindex,则它们的相对顺序将遵循它们在文档中的相对位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.