简介
在本实验中,我们将通过创建一个在状态变化时带有动画效果的自定义单选按钮来探索 CSS 编程概念。本实验将指导你完成使用弹性盒模型(Flexbox)为单选按钮创建布局、重置 <input> 元素的样式以及使用 ::before 元素创建单选按钮内圈的过程。在实验结束时,你将对 CSS 样式和动画技术有更深入的理解。
这是一个实验(Guided Lab),提供逐步指导来帮助你学习和实践。请仔细按照说明完成每个步骤,获得实际操作经验。根据历史数据,这是一个 初级 级别的实验,完成率为 100%。获得了学习者 100% 的好评率。
自定义单选按钮
虚拟机中已经提供了 index.html 和 style.css。
要创建一个在状态变化时带有动画效果的样式化单选按钮,请按照以下步骤操作:
- 使用弹性盒模型(Flexbox)创建一个
.radio-container,为单选按钮创建合适的布局。 - 重置
<input>元素的样式,并用它来创建单选按钮的轮廓和背景。 - 使用
::before元素创建单选按钮的内圈。 - 通过使用
transform: scale(1)和 CSS 过渡,在状态变化时创建动画效果。
以下是一个 HTML 代码片段示例:
<div class="radio-container">
<input class="radio-input" id="apples" type="radio" name="fruit" />
<label class="radio" for="apples">苹果</label>
<input class="radio-input" id="oranges" type="radio" name="fruit" />
<label class="radio" for="oranges">橙子</label>
</div>
以及相应的 CSS:
.radio-container {
display: flex;
align-items: center;
}
.radio-container * {
box-sizing: border-box;
}
.radio-input {
appearance: none;
width: 16px;
height: 16px;
margin: 0;
border: 1px solid #cccfdb;
border-radius: 50%;
display: grid;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
}
.radio-input::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
transform: scale(0);
transition: 0.3s transform ease-in-out;
box-shadow: inset 6px 6px #ffffff;
}
.radio-input:checked {
background: #0077ff;
border-color: #0077ff;
}
.radio-input:checked::before {
transform: scale(1);
}
.radio {
cursor: pointer;
padding: 6px 8px;
margin-right: 6px;
}
请点击右下角的“Go Live”,在端口 8080 上运行网络服务。然后,你可以刷新“Web 8080”标签页来预览网页。
总结
恭喜你!你已经完成了“自定义单选按钮”实验。你可以在 LabEx 中练习更多实验来提升你的技能。