关于 z-index为负值的情况 与 height=100%的疑惑

本文探讨了CSS中z-index设置为负值时的行为,并详细分析了不同浏览器下position:absolute与height:100%的表现差异,包括IE6和其他标准浏览器的具体实现。

最近遇到的一个小问题,记录下。

 

1.将z-index设为负值的话,元素会最贴近BODY,但是不能穿过body,之前我直接在body上加了个需隐藏的元素textarea,发现怎么也消失不了。最后加了个带背景的DIV把textarea套起来就可以了。用DIV把他遮住。

 

 

<body> 
	<div style="background:#fff">
		<textarea id="log" cols="50" rows="10" ></textarea>
		<input id="popBtn" type="button" value="pop a window" />
	</div>
</body>
 

2.关于不同浏览器的position:absolute;height:100%的表现

 

1)看以下代码:

 

 

<body style="height:3000px"> 
	<div id="test" style="position:absolute;top:0;left:0;width:100%;height:100%;backgroung:#ddd;">
		
	</div>
</body>

 

开始以为test会遮住整个页面(高3000px),但结果是:

IE6:高3000px

ff:长度搞好是窗口高度

 

原因:height取值

描述
auto默认。浏览器会计算出实际的高度。
length使用 px、cm 等单位定义高度。
%基于包含它的块级对象的百分比高度。
inherit规定应该从父元素继承 height 属性的值。

 

而将position设为absolute之后,此元素会相对于 static 定位以外的祖先元素进行定位,一级一级的向上找,直到找到 static 定位以外的祖先元素,并以此元素为基准进行定位。

 

所以在这时包含它的块级对象是window,应按window的高度取值。

 

为什么IE6是3000px呢?因为ie6会默认给body赋值position:relative;

 

2)那么将body的height:3000px.取消掉又是什么情况呢?

 

 

<body> 
	<div id="test" style="position:absolute;top:0;left:0;width:100%;height:100%;backgroung:#ddd;">
	</div>

<div style="height:4000px;"></div>
</body>
 

 

 

浏览器表现如下:

ie6:只有很小的一截,约2、3行的样子

ff:长度刚好是窗口高度

 

FF的表现很好理解,但为什么ie6的高不是4000px(左右)呢?

原因是,在IE6中,如果不设定父级元素的高度,则设定层高x%是无用的

这个时候给bodyheight:100%就行了,但是此时test的高度仍然不是4000px,而是和标准浏览器一样,长度刚好是窗口高度。


3)IE与标准浏览器的body\html 100%高度算法的不同

A:将body设为100%

 

标准浏览器:

body = document

html = docuemnt

 

ie6:

body = window ;执行$("body").height()后,body = document,原因不明

html = window

 

B:将body设为100%、html设为100%

 

标准浏览器:

body = window

html = window

 

ie6:

body = document

html = window

 

总结:

对于标准浏览器:body 倚赖于html,html依赖于window。

如果html的height为auto,那么html=document.如果height为100%,那么html=window.

如果body的height为auto,那么body=document.如果height为100%,那么body=html.

所以,当body和html为height:100%的时候,各自才会倚赖于父元素。

 


 

 

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>轮播图页面</title> <style> /* 顶部搜索框样式 */ .search-container { padding: 15px; background-color: #f8f8f8; position: sticky; top: 0; z-index: 100; } .search-box { width: 100%; padding: 10px 15px; border: 1px solid #ddd; border-radius: 20px; font-size: 16px; } /* 轮播图容器 */ .swiper { position: relative; height: 250px; overflow: hidden; margin-top: 10px; } /* 轮播图片容器 */ .swiper-wrapper { display: flex; width: 400%; /* 4张图 * 100% */ height: 100%; transition: transform 0.5s ease; } /* 单张轮播图 */ .swiper-slide { width: 25%; /* 100% / 4 */ height: 100%; } /* 轮播图图片样式 */ .swiper-slide img { width: 100%; height: 100%; object-fit: cover; } /* 指示点容器 */ .swiper-pagination { position: absolute; bottom: 15px; left: 0; right: 0; display: flex; justify-content: center; gap: 8px; } /* 指示点样式 */ .swiper-pagination-bullet { width: 10px; height: 10px; border-radius: 50%; background-color: #ccc; /* 未选中灰色 */ cursor: pointer; transition: background-color 0.3s; } /* 选中状态指示点 */ .swiper-pagination-bullet-active { background-color: #2196F3; /* 选中蓝色 */ } </style> </head> <body> <!-- 顶部搜索框 --> <div class="search-container"> <input type="search" class="search-box" placeholder="搜索" onfocus="if(this.value === '搜索') this.value = ''" onblur="if(this.value === '') this.value = '搜索'" inputmode="numeric" > </div> <!-- 轮播图区域 --> <div class="swiper"> <div class="swiper-wrapper"> <div class="swiper-slide"> <img src="../image/IMG_20250904_134254.jpg" alt="图片1"> </div> <div class="swiper-slide"> <img src="../image/IMG_20250904_182122.jpg" alt="图片2"> </div> <div class="swiper-slide"> <img src="../image/IMG_20250907_113806.jpg" alt="图片3"> </div> <div class="swiper-slide"> <img src="../image/IMG_20250907_180910.jpg" alt="图片4"> </div> </div> <!-- 指示点 --> <div class="swiper-pagination"> <span class="swiper-pagination-bullet swiper-pagination-bullet-active"></span> <span class="swiper-pagination-bullet"></span> <span class="swiper-pagination-bullet"></span> <span class="swiper-pagination-bullet"></span> </div> </div> <script> // 轮播图功能 let currentIndex = 0; const slides = document.querySelector('.swiper-wrapper'); const bullets = document.querySelectorAll('.swiper-pagination-bullet'); const totalSlides = bullets.length; // 自动轮播函数 function autoSlide() { currentIndex = (currentIndex + 1) % totalSlides; updateSlider(); } // 更新轮播位置和指示点 function updateSlider() { slides.style.transform = `translateX(-${currentIndex * 25}%)`; // 更新指示点状态 bullets.forEach((bullet, index) => { bullet.classList.toggle('swiper-pagination-bullet-active', index === currentIndex); }); } // 点击指示点切换 bullets.forEach((bullet, index) => { bullet.addEventListener('click', () => { currentIndex = index; updateSlider(); resetTimer(); // 重置计时器 }); }); // 重置自动轮播计时器 function resetTimer() { clearInterval(slideInterval); slideInterval = setInterval(autoSlide, 2000); } // 初始化自动轮播 let slideInterval = setInterval(autoSlide, 2000); </script> </body> </html> 没有轮播效果
09-16
1 以下哪些属性,可以让元素不论本身是什么元素类型,设置之后都会产生块级框(块级框就是元素可以设置宽高) A display:block; B position:fixed; C float:left; D position:absolute; 2 给元素设置透明度,可以用哪些方法设置,下面哪个是正确的?(背景透明和元素透明都算) A opacity:100; B opacity:0.5; C background:rgba(255,255,255,0.5); D background:rgba(255,255,255,100); 3 以下对position的描述正确的是 A 此属性规定元素的定位方式 B 此属性规定元素背景图片的位置 C 此属性规定只能块状元素才能使用 D 绝对定位会让元素脱离文档流 4 position定位属性设置哪些属性值,添加z-index属性会有效 A static B absolute C relative D fixed 5 以下选项中是position定位的属性值的选项有哪些 A static B auto C fixed D relative 6 以下选项中display的属性值为inline-block的标签有哪些 A img B input C textarea文本域 D select下列列表 7 盒子内的文本溢出,让盒子显示滚动条,以下那个属性不能实现此效果 A display B overflow C float D position 8 以下不属于overflow属性属性值的是 A left B 0px C auto D hidden 9 以下对z-index属性描述正确的是 A 给元素设置z-index属性可以改变元素的大小 B 给定元素设置z-index属性可以改变元素的堆叠顺序 C 元素默认的z-index值是1 D z-index属性值可以是负值 10 以下是display属性值的选项有 A none B inline C inline-block D block 11 z-index结合以下哪些选项一起使用有效。 A position:absolute; B position:fixed; C position:static; D position:relative; 12 让元素的高度自适应窗口的高度,以下哪些选项可以实现? A 元素{height:100%} B html{height:100%;}body{height:100%}元素{height:100%;} C html body{height:100%};元素{height:100%} D html,body{height:100%;}元素{height:100%;} 13 盒子内的文本溢出盒子范围,以下选项中可以隐藏溢出文本且不产生滚动条的选项是? A overflow:auto; B overflow-xy:hidden; C overflow-x:hidden;overflow-y:hidden; D overflow:hidden;
最新发布
11-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值