html - 选中/取消选中时如何使用相同的关键帧?
问题描述
我写了这样一个代码来在检查时操作不透明度。这行得通。
#check1:checked+.box {
animation: blink 1s;
}
@keyframes blink {
0%,
99% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<input type="checkbox" id="check1">
<div class="box">
<div class="content">
<p>content</p>
<button type="button">
<label for="check1">click me</label>
</button>
</div>
</div>
我也想在取消勾选的时候做同样的操作,所以我添加了动画属性。
但是,这不起作用,检查时的动画也不起作用。为什么会这样?
#check1 + .box {
animation: blink 1s;
}
#check1:checked + .box {
animation: blink 1s;
}
@keyframes blink {
0%, 99% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<input type="checkbox" id="check1">
<div class="box">
<div class="content">
<p>content</p>
<button type="button">
<label for="check1">click me</label>
</button>
</div>
</div>
另外,我定义了一个与另一个名称完全相同处理的动画,并且它正常工作。为什么会这样?有没有智能的 CSS 解决方案?
#check1+.box {
animation: blink1 1s;
}
#check1:checked+.box {
animation: blink2 1s;
}
@keyframes blink1 {
0%,
99% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes blink2 {
0%,
99% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<input type="checkbox" id="check1">
<div class="box">
<div class="content">
<p>content</p>
<button type="button">
<label for="check1">click me</label>
</button>
</div>
</div>
解决方案
经历它,我希望它对你有用
#check1+.box {
opacity:1;transition: 1s;
}
#check1:checked+.box {
opacity:0;transition: 1s;
}
<input type="checkbox" id="check1">
<div class="box">
<div class="content">
<p>content</p>
<button type="button">
<label for="check1">click me</label>
</button>
</div>
</div>
推荐阅读
- django - 在 django 中截断文本
- c - Minifilter 驱动程序 Windows 10 之前和包括 1809 和 1903 及更高版本之间的差异(在创建文件时调用 FileRenameInformation)
- laravel - 播种具有多个关系的表?
- documentation - 开发者文档生成器
- javascript - 恢复场景后如何防止精灵静止不动?移相器 3
- java - 用于检查 Internet 速度的 Java API(上传/下载)
- algorithm - 在动态编程中,我总是必须填满整个表格吗?
- r - 询问如何在 R 中使用 ggplot 制作世界热图?
- python - 类型提示特定格式的字符串?
- php - JSON解码数组到字符串转换错误