当前位置:网站首页>进行交互或动画时如何选择Visibility, Display, and Opacity
进行交互或动画时如何选择Visibility, Display, and Opacity
2022-08-01 20:32:00 【南飞雁】
创建淡入/淡出效果

例如,创建淡入/淡出效果
在 Web 前端开发中,我们可以使用display, opacity,visibility和其他 CSS 属性来显示和隐藏元素。
这些属性有何不同?如果我们想在显示和隐藏时设置动画怎么办?本文将为您介绍。
我们先来看看他们的区别:

可以看出,它们之间还是有一些区别的,我们需要根据具体情况来选择属性。
比如鼠标悬停淡入淡出效果,我们就不能用display。因为它不是可过渡动画的 CSS 属性。换句话说,它不能在给定时间内更改。
visibility 和 opactiy 两个是可以设置过渡动画的
<style>
div {
border: 1px solid #000;
color: red;
font-size: 36px;
width: 150px;
height: 150px;
}
div > span {
opacity: 0;
transition: opacity 1s linear;
}
div:hover > span {
visibility: visible;
opacity: 1;
}
</style>
<div>
<span>content</span>
</div>

看起来很棒。但是如果我们给使用opacity隐藏元素绑定了点击事件, 当opacity: 0时还是可以被点击的, 那么此时我又想有过渡效果, 又想他在隐藏的时候不能被点击, 只有在显示的时候,才能被点击
<style>
#upper {
position: absolute;
width: 150px;
height: 150px;
opacity: 1;
visibility: visible;
background-color: beige;
transition-property: opacity, visibility;
transition-duration: 0.6s;
}
#lower {
background-color: aquamarine;
width: 200px;
height: 200px;
}
</style>
<div id="upper"></div>
<div id="lower"></div>
<script>
const [upper, lower] = ["upper", "lower"].map((i) =>
document.getElementById(i)
);
upper.addEventListener("click", () => {
upper.style.opacity = 0;
// Toggle comments here to see the difference
upper.style.visibility = "hidden";
});
const randomColor = () => `#${(~~(Math.random() * (1 << 24))).toString(16)}`;
lower.addEventListener("click", () => {
lower.style.backgroundColor = randomColor();
});
</script>
这样我们就让隐藏的时候有了过渡的效果, 同时隐藏之后就没有了点击事件
边栏推荐
- myid file is missing
- 30-day question brushing plan (5)
- 洛谷 P2440 木材加工
- 微信小程序云开发|个人博客小程序
- [Energy Conservation Institute] Ankerui Food and Beverage Fume Monitoring Cloud Platform Helps Fight Air Pollution
- 【kali-信息收集】(1.5)系统指纹识别:Nmap、p0f
- 【节能学院】推进农业水价综合改革的意见解读
- 任务调度线程池-应用定时任务
- 启明云端分享|盘点ESP8684开发板有哪些功能
- Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
猜你喜欢

17. Load balancing

解除360对默认浏览器的检测与修改

【节能学院】智能操控装置在高压开关柜的应用

【无标题】

因斯布鲁克大学团队量子计算硬件突破了二进制

New graduate students, great experience in reading English literature, worthy of your collection

【多任务学习】Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18

【torch】张量乘法:matmul,einsum

基于FPGA的任意字节数(单字节、多字节)的串口(UART)发送(含源码工程)

专利检索常用的网站有哪些?
随机推荐
Redis does check-in statistics
【无标题】
解除360对默认浏览器的检测与修改
The Internet giant development process
[Personal work] Wireless network image transmission module
KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
【多任务模型】Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized(RecSys‘20)
【kali-信息收集】(1.6)服务的指纹识别:Nmap、Amap
【ES】ES2021 我学不动了,这次只学 3 个。
【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)
数据库内核面试中我不会的问题(1)
Batch get protein .pdb files based on Uniprot ID/PDB ID
【Dart】dart构造函数学习记录(含dart单例模式写法)
Creo5.0 rough hexagon is how to draw
latex论文神器--服务器部署overleaf
regular expression
nacos installation and configuration
58: Chapter 5: Develop admin management services: 11: Develop [admin face login, interface]; (not measured) (using Ali AI face recognition) (demonstrated, using RestTemplate to implement interface cal
给定中序遍历和另外一种遍历方法确定一棵二叉树
【节能学院】智能操控装置在高压开关柜的应用