html - 动画元素在外观上平滑淡入,但在消失时立即隐藏
问题描述
我有一个元素opacity: 0
。
当某个选择器匹配时,我想opcacity
平滑过渡到1
.
当该选择器不再匹配时,我想opacity
立即恢复为0
.
我怎么做?
PS 没有 JS,当然。
.the {
display: inline-block;
border: 1px solid deepskyblue;
background-color: lightblue;
padding: 2px 5px;
border-radius: 4px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
pointer-events: none;
}
input:checked ~ .the {
opacity: 1;
}
<label>
<input type="checkbox">
Show element
<span class="the">
I'm the element to animate!
</span>
</label>
解决方案
您可以添加transition
到:checked
规则:
.the {
display: inline-block;
border: 1px solid deepskyblue;
background-color: lightblue;
padding: 2px 5px;
border-radius: 4px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
pointer-events: none;
}
input:checked ~ .the {
transition: opacity .5s;
opacity: 1;
}
<label>
<input type="checkbox">
Show element
<span class="the">
I'm the element to animate!
</span>
</label>
推荐阅读
- reactjs - 有没有办法缩短这个在 React 中使用钩子的更改处理程序?
- docker - VSCode / Docker devcontainer.json 问题
- window - OpenTok Windows SDK 2.16.6 音频未流式传输
- javascript - 为什么我想要的字符串没有用 javascript 中的另一个替换?
- node.js - 搜索数组 MongoDB NodeJS
- python - 将 none 值替换为同一列表中的值
- javascript - d3js 无法访问数据索引
- reinforcement-learning - 在 openai gym 和 stable-baselines 中为无效动作添加逻辑的问题
- c# - 无法使用 asp.net MVC 应用程序连接到 TFS
- symfony - Symfony 3 控制台异常日志仅显示在控制台上