当前位置:网站首页>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
边栏推荐
猜你喜欢

徒步,治好了我的精神内耗
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it

Internet使用的网络协议是什么

对C语言结构体内存对齐的理解
Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决

Hiking, cured my mental internal friction

JS提升:手写发布订阅者模式(小白篇)

98. Embedded controller EC actual combat EC development board development completed

with语句和上下文管理器

宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
随机推荐
98.嵌入式控制器EC实战 EC开发板开发完成
封装一个管理 url 状态的 hook
测试的意义并不是能找到全部的缺陷
C专家编程 前言
Get started with Grafana in 15 minutes
使用员工管理软件,解锁人力生产力新水平,提高人力资源团队灵活性
kubernetes各名词缩写
对C语言结构体内存对齐的理解
微服务负载均衡器Ribbon
微信小程序云开发|个人博客小程序
【中文树库标记---CTB】
Go 语言中常见的坑
C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
1374. 生成每种字符都是奇数个的字符串 : 简单构造模拟题
An online JVM FullGC made it impossible to sleep all night and completely crashed~
C专家编程 第1章 C:穿越时空的迷雾 1.2 C语言的早期体验
那些关于DOM的常见Hook封装(一)
2022年秋招,软件测试开发最全面试攻略,吃透16个技术栈
C陷阱与缺陷 附录B Koenig和Moo夫妇访谈
分类接口,淘宝分类详情 API