简体   繁体   English

为什么这个焦点事件在按 Tab 时会破坏 html 样式?

[英]Why does this focus event break html style when pressing tab?

I am encountering a bizarre behavior.我遇到了一个奇怪的行为。 I have simple blocks that contains a hidden overflow.我有包含隐藏溢出的简单块。 A hoover event is triggered to display the hidden overflow.触发 hoover 事件以显示隐藏的溢出。 the hidden part has position absolute.隐藏部分有 position 绝对值。

HTML: HTML:

<div class='container'>
  <div class="edito">
    <img src="https://via.placeholder.com/273x211" />
    <div class="inner">
      <p>Lorem ipsum dolores</p>
      <a href="#cta">See more</a>
    </div>
  </div>

  <div class="edito">
    <img src="https://via.placeholder.com/273x211" />
    <div class="inner">
      <p>Lorem ipsum dolores</p>
      <a href="#cta">See more</a>
    </div>
  </div>

  <div class="edito">
    <img src="https://via.placeholder.com/273x211" />
    <div class="inner">
      <p>Lorem ipsum dolores</p>
      <a href="#cta">See more</a>
    </div>
  </div>
</div>

CSS : CSS

.edito {
  width: 273px;
  height: 211px;
  position: relative;
  border: 2px solid;
  overflow:hidden;

}

.edito .inner {
  position: absolute;
    left: 0;
  width: 100%;
  height: 195px;
  transition: top 0.8s ease;
  top: calc(100% - 50px);
}

.edito:hover .inner {
  background-color: purple;
  top: calc(100% - 150px);
}

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: space-around;
}
img {
  position:relative;
}

Now when someone (with accessibility issue for eg) tries to press tab to navigate between these blocks, the style is broken.现在,当有人(例如存在可访问性问题)尝试按 Tab 在这些块之间导航时,样式会被破坏。

You can see this here:你可以在这里看到这个:

jsfiddle.net/0ubqwfxc

Try to press tab to navigate to the third block;尝试按 Tab 键导航到第三个块; the style of first ones is broken.第一个的风格被打破了。

Why this happens and how to fix it?为什么会发生这种情况以及如何解决?

Thank you谢谢

It breaks because when the user clicks tab it selects the link and the link automatically goes up.它会中断,因为当用户单击选项卡时,它会选择链接并且链接会自动上升。 To stop them from focusing on a link with tab simply add the attribute tabindex="-1" to your <a> tag like this <a href="#cta" tabindex="-1">See more</a> Working example: https://codepen.io/Ajjarindahouse/pen/qBbjERM要阻止他们关注带有标签的链接,只需将属性tabindex="-1"添加到您的<a>标记中,如下所示<a href="#cta" tabindex="-1">See more</a>例如: https://codepen.io/Ajjarindahouse/pen/qBbjERM

I managed to solve this by handling selector focus-within like this:我设法通过像这样处理选择器焦点来解决这个问题:

.edito:focus-within .inner {
  top: calc(100% - 195px);
  transition: top 0s ease;
}

Can be tested here: http://jsfiddle.net/tzfoc0sr/可以在这里测试: http://jsfiddle.net/tzfoc0sr/

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

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