当前位置:网站首页>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 useopacity
Hidden 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
边栏推荐
- Pytorch框架学习记录13——利用GPU训练
- 如何封装 cookie/localStorage/sessionStorage hook?
- [译] 容器和 Kubernetes 中的退出码完整指南
- StringTable详解 串池 性能调优 字符串拼接
- 织梦模板加入php代码
- JS提升:手写发布订阅者模式(小白篇)
- C Pitfalls and Defects Chapter 5 Library Functions 5.5 Library Function Signal
- Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
- Jmeter实战 | 同用户重复并发多次抢红包
- Pytorch框架学习记录12——完整的模型训练套路
猜你喜欢
JS Improvement: Handwritten Publish Subscriber Model (Xiaobai)
R语言 数据的关系探索
数据库内核面试中我不会的问题(1)
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
关于Request复用的那点破事儿。研究明白了,给你汇报一下。
PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】
Jmeter实战 | 同用户重复并发多次抢红包
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]
JS提升:如何中断Promise的链式调用
随机推荐
Review Set/Map basics with these two hooks
PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】
Common pits in the Go language
win10版本1803无法升级1903系统如何解决
Imitation cattle forum project
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
正则表达式
Questions I don't know in database kernel interview(1)
分类接口,淘宝分类详情 API
Protocol Buffer usage
【微信小程序】【AR】threejs-miniprogram 安装(76/100)
移植MQTT源码到STM32F407开发板上
ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
kubernetes各名词缩写
列表页常见的 hook 封装
An online JVM FullGC made it impossible to sleep all night and completely crashed~
tiup mirror
wps excel 插入公式 整列
Kubernetes 如何实现组件高可用