当前位置:网站首页>How to choose Visibility, Display, and Opacity when interacting or animating
How to choose Visibility, Display, and Opacity when interacting or animating
2022-08-01 21:07:00 【Nan Feiyan】
Create fade in/淡出效果

例如,Create fade in/淡出效果
在 Web 前端开发中,我们可以使用display, opacity,visibility和其他 CSS properties to show and hide elements.
How do these properties differ?What if we want to animate when showing and hiding?本文将为您介绍.
Let's take a look at their differences first:

可以看出,There are still some differences between them,We need to choose the attribute according to the specific situation.
For example, the mouseover fades in and out,我们就不能用display.Because it's not transition-animable CSS 属性.换句话说,It cannot be changed within a given time.
visibility 和 opactiy Two are transition animations that can be set
<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>

看起来很棒.But if we give useopacityHidden elements are bound to click events, 当opacity: 0can still be clicked, Then I want to have a transition effect at this time, Also thought he couldn't be clicked when he was hidden, only when displayed,才能被点击
<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>
In this way, we have a transition effect when hiding, At the same time, after hiding, there is no click event
边栏推荐
猜你喜欢
随机推荐
通过这两个 hook 回顾 Set/Map 基础知识
使用百度EasyDL实现厂区工人抽烟行为识别
PyTorch笔记 - Attention Is All You Need (2)
MySQL Syntax Basics
空间数据库开源路,超图+openGauss风起禹贡
技能大赛训练:A部分加固题目
LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
[译] 容器和 Kubernetes 中的退出码完整指南
有点奇怪!访问目的网址,主机能容器却不行
基于FPGA的任意字节数(单字节、多字节)的串口(UART)发送(含源码工程)
Classification interface, Taobao classification details API
Graph adjacency matrix storage
C陷阱与缺陷 第7章 可移植性缺陷 7.8 随机数的大小
职场如象棋,测试/开发程序员如何突破成长瓶颈期?
STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障
正则表达式
R语言 数据的关系探索
An online JVM FullGC made it impossible to sleep all night and completely crashed~
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
淘宝获取收货地址列表的 API

![[Chinese tree tags - CTB]](/img/f4/b985da2ff83b2a9ab4eebb8bd060bf.png)







