当前位置:网站首页>进行交互或动画时如何选择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>
这样我们就让隐藏的时候有了过渡的效果, 同时隐藏之后就没有了点击事件
边栏推荐
- 9月备考PMP,应该从哪里备考?
- Addition, Subtraction, Multiplication of Large Integers, Multiplication and Division of Large Integers and Ordinary Integers
- Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
- 【节能学院】安科瑞餐饮油烟监测云平台助力大气污染攻坚战
- 我的驾照考试笔记(1)
- 密码学的基础:X.690和对应的BER CER DER编码
- An implementation of an ordered doubly linked list.
- Different operating with different locks, rounding
- Buttons with good user experience should not have hover state on mobile phones
- 研究生新同学,牛人看英文文献的经验,值得你收藏
猜你喜欢
【多任务优化】DWA、DTP、Gradnorm(CVPR 2019、ECCV 2018、 ICML 2018)
数据库单字段存储多个标签(位移操作)
The graphic details Eureka's caching mechanism/level 3 cache
4.1 配置Mysql与注册登录模块
Oracle排序某个字段, 如果这个varchar2类型的字段有数字也有文字 , 怎么按照数字大小排序?
Which websites are commonly used for patent searches?
第58章 结构、纪录与类
Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
Where should I prepare for the PMP exam in September?
LTE时域、频域资源
随机推荐
Redis does check-in statistics
【Untitled】
【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)
"No title"
Hangao data import
myid file is missing
瀚高数据导入
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
系统收集集
"Torch" tensor multiplication: matmul, einsum
泰德制药董事长郑翔玲荣膺“2022卓越影响力企业家奖” 泰德制药荣获“企业社会责任典范奖”
【torch】张量乘法:matmul,einsum
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换
[Multi-task learning] Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18
C语言实现-直接插入排序(带图详解)
Remove 360's detection and modification of the default browser
【节能学院】推进农业水价综合改革的意见解读
useful website
Convolutional Neural Network (CNN) mnist Digit Recognition - Tensorflow
LTE时域、频域资源