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 属性的层。

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 【Time】 不确定度的A类、B类评定及合成

    【Time】 不确定度的A类、B类评定及合成不确定度的A类、B类评定及合成由于测量结果的不确定度往往由多种原因引起的,对每个不确定度来源评定的标准偏差,称为标准不确定度分量,用符号ui表示。(1)不确定度的A类评定用对观测列进行统计分析的方法来评定标准不确定度,称为不确定度A类评定;所得到的相应标准不确定度称为A类不确定度分量,用符号uA表示。它是用实验标准偏差来表征。计算公式:一次测量结果An的uA=S;…

  • PyCharm如何设置 “ctrl+滚轮” 实现字体的放大和缩小「建议收藏」

    PyCharm如何设置 “ctrl+滚轮” 实现字体的放大和缩小「建议收藏」一、PyCharm字体放大的设置File–>setting–>Keymap–>在搜索框中输入increase—>IncreaseFontSize(双击)–>在弹出的对话框中选择AddMouseShortcut–>弹出的对话框后按住ctrl键的同时鼠标滚轮向上滑。二、Pycharm字体缩小的设置:File–>setting–>Keymap–>在搜索框中输入decrease—>

  • 矩阵的意义_行满秩矩阵的广义逆

    矩阵的意义_行满秩矩阵的广义逆文章目录一、代码二、拓展三、总结一、代码在LaTeX中表示广义逆、伪逆:A^{\dagger},\AA^{\dagger}=(AA^{\dagger})^H效果如下:A†, AA†=(AA†)HA^{\dagger},\AA^{\dagger}=(AA^{\dagger})^HA†, AA†=(AA†)H特殊的还有其他符号见下表。二、拓展部分数学符号如下:A^\dagger,A^\ddagger,A^{\|},A^{**},A^{\dagge

  • netCMS学习笔记

    netCMS学习笔记二者关系:“netCMS”出自“风讯”,“netCMS”完全开源,有相关文档资料。“风讯”开源版删除了一些源码。学习内容:一、模板实现原理   标签   netCMS自定义了一系列标签,在模版页面中插入这些标签,在发布网页时类“NetCMS.Publish.Template”用正则表达示筛选出这些标签,然后从数据库表“NT_News”中读取相应的内容,将标签替换。   模版   模版引用的CSS,图片等路径,必需是绝对路径。   前台浏览方式   分“动态”和“静态”两种模式。   在“动态”

  • java 身份证15位转18位「建议收藏」

    java 身份证15位转18位「建议收藏」1/**2*根据身份证号获取性别3*4*@parampid5*身份证号6*@return性别F为女M为男7*/8publicstaticStringg

  • java中保留两位小数的输出

    java中保留两位小数的输出例如平方和与倒数和,最后输出要求保留两位小数,下面这个程序的注意点已用红色字体标记importjava.text.DecimalFormat;importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmet

发表回复

您的电子邮箱地址不会被公开。

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