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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • objectmapper json转对象_篆体字转换器在线转换

    objectmapper json转对象_篆体字转换器在线转换JSONObject转换为Mapimportcom.alibaba.fastjson.TypeReference;importcom.alibaba.fastjson.JSONObject;JSONObjectobj=newJSONObject();{obj.put(“key1″,”value1”);obj.put(“key2″,”value2”);obj.put(“key3″,”value3”);}Map<String,String>params=

    2022年10月5日
    3
  • 微信小程序开发 – 用户授权登陆「建议收藏」

    微信小程序开发 – 用户授权登陆「建议收藏」本篇将帮助读者实现基于微信开发者工具&amp;C#环境下的用户在小程序上的授权登陆。准备:微信开发者工具下载地址:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html微信小程序开发文档:https://developers.weixin.qq.com/miniprogram/d…

    2022年6月12日
    31
  • QT QTcpSocket「建议收藏」

    QT QTcpSocket「建议收藏」QTcpSocket类提供TCP套接字。TCP(传输控制协议)是一种可靠的、面向流的、面向连接的传输协议。它特别适合于数据的连续传输。QTcpSocket是QAbstractSocket的一个方便的子类,它允许您建立TCP连接和传输数据流。有关详细信息,请参见QAbstractSocket文档。注意:TCP套接字不能在QIODevice::Unbuffered模式下打开。请参见QTcpServer、QUdpSocket、QNetworkAccessManager、Fortune服务器示例、Fortu

    2025年10月17日
    3
  • 【协议】MQTT、CoAP、HTTP比较,MQTT协议优缺点

    【协议】MQTT、CoAP、HTTP比较,MQTT协议优缺点从这几个层面进行比较与描述:1.协议栈:MQTT:TCP长链接,CoAP:UDP低功耗短连接,HTTP:TCP2.请求方式MQTT:发布订阅,CoAP:request/response,HTTP:request/response3.通讯消息格式:MQTT:CoAP:HTTP:4.使用特点与使用场…

    2022年6月20日
    60
  • vue父子组件传值 简单了解vuex

    vue父子组件传值 简单了解vuex一、vue的父子组件之间是如何传值的?首先呢,需要说说的是,vue既然有双向绑定,那为何会有父子组件之间的传值问题?这个问题也简单,vue的组件会供其他的vue页面进行调用,如果数组都是双向绑定的话,那么就容易混乱了,比如a,b页面绑了一个num=10,那b,c页面又绑了num=5,那vue实例的num到底听谁的?所以,这就是vue官网为什么说组件之间的数据只能是单项流通的,而且由父组件传递给…

    2022年5月16日
    93
  • 基因突变的实例:加拿大无毛猫

    基因突变的实例:加拿大无毛猫

    2021年11月15日
    74

发表回复

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

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