html - 仅 CSS 且布局友好 从 Fluent Design System 中显示焦点
问题描述
在Reveal 焦点文档中:
但是,作为文档
显示焦点会增加焦点视觉对象的大小,这可能会导致 UI 布局出现问题。在某些情况下,您需要自定义显示焦点效果以针对您的应用进行优化。
您将如何以上述方式创建不影响 UI 的效果?
我的 Reveal 焦点组件:
- 显示发光是
box-shadow
- 主要焦点视觉是
outline
- 次要焦点视觉是
border
- 背景
但似乎有些不对劲,我不太明白。是box-shadow
,是间距(比如margin
,我没有设置任何你可以看到的),还是其他的?如果你想让它看起来像下面的 gif 那样,你会如何修复它?
body {
background-color: #000;
padding: 5px 100px;
}
.tile {
display: inline-block;
height: 82px;
background-color: #555555;
}
.x1 { width: 19%; }
.x2 { width: 38%; }
.reveal-focus {
border: 1px solid transparent;
outline: 2px solid transparent;
}
.reveal-focus:focus {
outline-color: #61B250;
box-shadow: 0 0 15px 3px #61B250;
}
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x2"></a>
<a href="#" class="tile reveal-focus x2"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
解决方案
阴影被放置在出现在焦点之前的元素之上,但在它之后的元素之下。您需要添加position: relative
到所有元素,并添加到z-index: 1
重点元素。
为确保这不会干扰任何其他堆叠,请应用于position: relative; z-index: 0
容器。这确保了它有自己的堆栈上下文。
您显示的 GIF 似乎也有轻微的动画效果,在消退到正常之前,发光会更加强烈。这可以通过 非常简单地实现animation
。
body {
background-color: #000;
padding: 5px 100px;
}
.tile {
display: inline-block;
height: 82px;
background-color: #555555;
}
.x1 { width: 19%; }
.x2 { width: 38%; }
.reveal-focus {
border: 1px solid transparent;
outline: 2px solid transparent;
position: relative;
}
.reveal-focus:focus {
border-color: #000;
outline-color: #61B250;
box-shadow: 0 0 15px 3px #61B250;
animation: glowfade 0.4s linear;
z-index: 1;
}
@keyframes glowfade {
from {box-shadow: 0 0 30px 6px #61B250;}
to {box-shadow: 0 0 15px 3px #61B250;}
}
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x2"></a>
<a href="#" class="tile reveal-focus x2"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
<a href="#" class="tile reveal-focus x1"></a>
根据需要调整值。
推荐阅读
- python - pyqt5动态生成QCheckBox并将动作连接到新元素
- ubuntu - ubuntu中的八度:'rm'在第1行第1列附近未定义
- ruby-on-rails - 如何确保在重定向中传递 ID
- reactjs - Flow 使用 bind() 为 React Native 中的常见模式引发错误?
- clojure - 如何在向量中搜索元素区域?
- dart - Flutter在后台运行websocket?
- python - 使用 tkinter 创建选择列表对话框
- python - BeautifulSoup 抓取附加数据
- java - 如何覆盖 Java 按钮以执行新操作
- java - Web 服务不会在字符串中减去时间