Opacity 属性引发的层叠问题

Opacity 属性引发的层叠问题Opacity属性引发的层叠问题总结

大家好,又见面了,我是你们的朋友全栈君。

有很长一段时间没有更新博客了,近一段时间开始重新梳理知识点和写博客了,新的博客地址:欢迎访问

一般情况下,网页中的层叠规律是这样的:如果两个层都没有设置 position 属性为 absolute 或者 relative 属性,哪个层的HTML代码放在后面,哪个层就显示在上面。
Demo:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Opacity 属性引发的层叠问题</title>
	<style type="text/css">
		.box{
			width: 200px;
			height: 200px;
			background-color: red;
			font-size: 100px;
			line-height: 200px;
			text-align: center;
			color: #fff;
			cursor: pointer;
		}
		.box2{
			background-color: blue;
			margin-left: 30px;
			margin-top: -160px;
		}
		.box3{
			background-color: green;
			margin-left: 60px;
			margin-top: -160px;
		}
	</style>
</head>
<body>
	<div class="box box1" id="box1"></div>
	<div class="box box2" id="box2"></div>
	<div class="box box3" id="box3"></div>
</body>
</html>

这里写图片描述
这里的三个div,box3在最上面,box1在最下面。如果指定了 position 属性,并且设置了 z-index 属性,谁的值大,谁就在上面。
当给box1设置opacity:0.8时,神奇的事情发生了,它覆盖了另外两个层。只有另外两个div也设置了小于1的opacity值时,才可以覆盖box1。
Demo2:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Opacity 属性引发的层叠问题</title>
	<style type="text/css">
		.box{
			width: 200px;
			height: 200px;
			background-color: red;
			color: #fff;
			cursor: pointer;
		}
		.box1{
			opacity: 0.8;
		}
		.box2{
			background-color: blue;
			margin-left: 30px;
			margin-top: -160px;
		}
		.box3{
			background-color: green;
			margin-left: 60px;
			margin-top: -160px;
			opacity: 0.7;
		}
	</style>
</head>
<body>
	<div class="box box1" id="box1">box1</div>
	<div class="box box2" id="box2">box2</div>
	<div class="box box3" id="box3">box3</div>
<script type="text/javascript">
function $(id){
	return document.getElementById(id);
}
$("box1").onclick = function(){
	alert("box1");
}
$("box2").onclick = function(){
	alert("box2");
}
$("box3").onclick = function(){
	alert("box3");
}
</script>
</body>
</html>

这里写图片描述
这个例子中的层叠顺序box2在最下面,box3在最上面,box1在中间。因为给box3设置了opacity: 0.7。这样看不是很明显,我们可以通过给每一个div绑定点击事件来测试。

一般情况下,指定了 position 并且指定了 z-index 值的层,拥有比普通层更高的层次,那么指定 opacity 的层和指定了 position 的层相比呢?我们对box2加上 position:relative 看看。
Demo3:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Opacity 属性引发的层叠问题</title>
	<style type="text/css">
		.box{
			width: 200px;
			height: 200px;
			background-color: red;
			color: #fff;
			cursor: pointer;
		}
		.box1{
			opacity: 0.8;
		}
		.box2{
			background-color: blue;
			margin-left: 30px;
			margin-top: -160px;
			position: relative;
		}
		.box3{
			background-color: green;
			margin-left: 60px;
			margin-top: -160px;
			opacity: 0.7;
		}
	</style>
</head>
<body>
	<div class="box box1" id="box1">box1</div>
	<div class="box box2" id="box2">box2</div>
	<div class="box box3" id="box3">box3</div>
<script type="text/javascript">
function $(id){
	return document.getElementById(id);
}
$("box1").onclick = function(){
	alert("box1");
}
$("box2").onclick = function(){
	alert("box2");
}
$("box3").onclick = function(){
	alert("box3");
}
</script>
</body>
</html>

这里写图片描述
从上面的例子可以看出,对层使用 position 属性的 relative 之后,可以使其层次和 opacity 相同,这样之后,按照正常的排序进行层叠显示(absolute 属性值结果和 relative 属性值表现的相同)。
当我们取消了bxo3的 opacity 属性之后,我们可以看到,box3 被排在了最下面。如下图所示:
这里写图片描述
Demo4:
这个例子中除了给box2设置position:relative 属性,还使用了 z-index。我们对box2进行了 z-index 的设置(z-index: 100),这样一来box2变成了最顶级的了。

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Opacity 属性引发的层叠问题</title>
	<style type="text/css">
		.box{
			width: 200px;
			height: 200px;
			background-color: red;
			color: #fff;
			cursor: pointer;
		}
		.box1{
			opacity: 0.8;
		}
		.box2{
			background-color: blue;
			margin-left: 30px;
			margin-top: -160px;
			position: relative;
			z-index: 100;
		}
		.box3{
			background-color: green;
			margin-left: 60px;
			margin-top: -160px;
			opacity: 0.7;
		}
	</style>
</head>
<body>
	<div class="box box1" id="box1">box1</div>
	<div class="box box2" id="box2">box2</div>
	<div class="box box3" id="box3">box3</div>
<script type="text/javascript">
function $(id){
	return document.getElementById(id);
}
$("box1").onclick = function(){
	alert("box1");
}
$("box2").onclick = function(){
	alert("box2");
}
$("box3").onclick = function(){
	alert("box3");
}
</script>
</body>
</html>

层叠问题总结
使用了 position 属性值为 absolute、relative 的层,将会比普通层更高层次。使用了小于1的 opacity 属性的层,也比普通层更高层次并且和指定 position 的层同层,但是不支持 z-index 属性,所以指定 position 的层,可以使用 z-index 属性,来覆盖带有小于1的 opacity 属性的层。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141263.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Solr使用入门指南

    Solr使用入门指南

    2021年12月15日
    37
  • C语言“fread”函数的用法?「建议收藏」

    C语言“fread”函数的用法?「建议收藏」C语言“fread”函数的用法为“size_tfread(void*buffer,size_tsize,size_tcount,FILE*stream)”,其作用是从一个文件流中…

    2022年9月12日
    0
  • 谈谈自己对正则化的一些理解~

    谈谈自己对正则化的一些理解~上学的时候,就一直很好奇,模式识别理论中,常提到的正则化到底

    2022年7月14日
    12
  • Mongo分库方案选型

    Mongo分库方案选型 Mongo分库方案两种形式分析: 1. mongo sharding方式:1.1. 深翻页的问题举例:当mongo的分片是5片时,分页查询(如果按照创建时间倒叙查询)第一页,每页50条数据,则mongo sharding在每个分片上取50条数据,一共50*5条数据,然后进行汇总,计算出前50条正确数据作为返回结果。如果是翻页到1000页,那么mongo sharding需要从…

    2022年6月24日
    27
  • C语言 文件读写的实现

    C语言 文件读写的实现关于C语言的文件读写,我将介绍下面这几种方式:字符的读写:使用fgetc()函数和fputc()函数;字符串的读写:使用fgets()函数和fputs()函数;格式化的读写(主要用于文本文件):使用fscanf()函数和fprintf()函数。字符读写:1.fputc()函数fputc(c,fp);//用于将一个字符写入文件其中,…

    2022年5月5日
    46
  • idea激活码2021.10.3【2021最新】

    (idea激活码2021.10.3)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月27日
    33

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号