当前位置:网站首页>进行交互或动画时如何选择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>
这样我们就让隐藏的时候有了过渡的效果, 同时隐藏之后就没有了点击事件
边栏推荐
- SIPp installation and use
- 【多任务优化】DWA、DTP、Gradnorm(CVPR 2019、ECCV 2018、 ICML 2018)
- 第60章 ApplicationPart自动集成整体性和独立性插件项
- 【七夕特别篇】七夕已至,让爱闪耀
- The configuration manual for the secondary development of the XE training system of the missing moment document system
- WhatsApp group sending actual combat sharing - WhatsApp Business API account
- kingbaseV8R3和postgreSQL哪个版本最接近?
- Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
- LTE时域、频域资源
- 研究生新同学,牛人看英文文献的经验,值得你收藏
猜你喜欢

【个人作品】无线网络图传模块

【Dart】dart之mixin探究

【Social Media Marketing】How to know if your WhatsApp is blocked?
![[Personal Work] Remember - Serial Logging Tool](/img/8c/56639e234ec3472f4133342eec96d6.png)
[Personal Work] Remember - Serial Logging Tool

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

SIPp 安装及使用

The graphic details Eureka's caching mechanism/level 3 cache

Creo5.0 rough hexagon is how to draw

第57章 业务逻辑之业务实体与数据库表的映射规则定义

使用微信公众号给指定微信用户发送信息
随机推荐
Addition, Subtraction, Multiplication of Large Integers, Multiplication and Division of Large Integers and Ordinary Integers
【kali-信息收集】(1.3)探测网络范围:DMitry(域名查询工具)、Scapy(跟踪路由工具)
面试突击70:什么是粘包和半包?怎么解决?
有用的网站
kingbaseV8R3和postgreSQL哪个版本最接近?
Where should I prepare for the PMP exam in September?
Does LabVIEW really close the COM port using VISA Close?
密码学的基础:X.690和对应的BER CER DER编码
使用Huggingface在矩池云快速加载预训练模型和数据集
作为程序员你应该会的软件
Convolutional Neural Network (CNN) mnist Digit Recognition - Tensorflow
因斯布鲁克大学团队量子计算硬件突破了二进制
数据库内核面试中我不会的问题(1)
The configuration manual for the secondary development of the XE training system of the missing moment document system
AQS原理和介绍
"No title"
WhatsApp group sending actual combat sharing - WhatsApp Business API account
数据库单字段存储多个标签(位移操作)
【社媒营销】如何知道自己的WhatsApp是否被屏蔽了?
数字孪生北京故宫,元宇宙推进旅游业进程