当前位置:网站首页>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
边栏推荐
- C pitfalls and pitfalls Chapter 7. Portability pitfalls 7.10 Free first, then realloc
- 关键字搜索:“淘宝商品 API ”
- ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
- 分类接口,淘宝分类详情 API
- 图片识别商品接口 API:天猫淘宝
- 面试突击70:什么是粘包和半包?怎么解决?
- C陷阱与缺陷 第8章 建议与答案 8.1 建议
- 线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
- tiup mirror grant
- Graph adjacency matrix storage
猜你喜欢

OSG笔记:设置DO_NOT_COMPUTE_NEAR_FAR,手动计算远近平面

【接口测试】JMeter调用JS文件实现RSA加密

JS hoisting: how to break the chain of Promise calls

图的邻接矩阵存储

ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
![[Chinese tree tags - CTB]](/img/f4/b985da2ff83b2a9ab4eebb8bd060bf.png)
[Chinese tree tags - CTB]

Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data

【中文树库标记---CTB】

R语言 数据的关系探索

C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
随机推荐
字符串
JSD - 2204 - Knife4j framework - processing - Day07 response results
Go 语言中常见的坑
网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
基于FPGA的任意字节数(单字节、多字节)的串口(UART)发送(含源码工程)
案例:MySQL主从复制与读写分离
JS提升:手写发布订阅者模式(小白篇)
Common pits in the Go language
Pytorch学习记录(八):生成对抗网络GAN
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
位运算简介
关键字搜索:“淘宝商品 API ”
技术栈概览
C专家编程 第1章 C:穿越时空的迷雾 1.5 今日之ANSI C
织梦通过数据库查询调用当前文章的留言
tiup mirror clone
OSG笔记:设置DO_NOT_COMPUTE_NEAR_FAR,手动计算远近平面
STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障
如何用Chrome编辑以及调试代码
微服务负载均衡器Ribbon