元素显示隐藏的9种思路_html/css_WEB-ITnose
× 目录 [1]display [2]visibility [3]hidden [4]opacity [5]overflow [6]clip [7]transform [8]覆盖 [9]偏移
前面的话
在网页制作中,元素的显示隐藏是非常常见的需求。本文将介绍元素显示隐藏的9种思路
思路一: display
对于元素显隐来说,最常见就是display:none | display:block,但是使用这种方法有个问题,元素的display属性在隐藏前并不都是block,还有可能是inline、inline-block等
[注意]如果要适用于任何元素需要提前储存元素的display值
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;">测试文字</div>
<script>show.onclick = function(){ test.style.display = 'block';} hide.onclick = function(){ test.style.display = 'none';}</script>
思路二: visibility
visibility:hidden与display:none作为隐藏元素的两种方式,常常被人们拿来比较。其实区别很简单,前者不脱离文档流,保留隐藏之前元素占据的物理区域;而后者则脱离文档流,如果重新显示则需要页面的重新绘制。还有一点区别却很少人提到,如果父级设置display:none;子级设置display:block也不会显示;而如果父级设置visibility:hidden;子级设置visibility:visible时子级会显示出来
[注意]visilibity可应用transition属性。因为visibility是离散步骤,在0到1数字范围之内,0表示隐藏,1表示显示。visibility:hidden可以看成visibility:0;visibility:visible可以看成visibility:1。于是,visibility应用transition等同于0~1之间的过渡效果。实际上,只要visibility的值大于0就是显示的。由于这个现象,我们可以利用transition实现元素的延时显示隐藏
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;">测试文字</div>
<script>show.onclick = function(){ test.style.transition = 'none'; test.style.visibility = 'visible';} hide.onclick = function(){ test.style.transition = 'visibility 0.2s 0.5s'; test.style.visibility = 'hidden';}</script>
思路三: hidden
可能有些人不太熟悉,HTML有个hidden全局属性,专门用于显示隐藏元素,与display:none的作用类似,元素隐藏时脱离文档流,无法接受javascript事件
[注意]IE7-不支持,IE10-不支持test.hidden='hidden'写法,只支持test.setAttribute('hidden','hidden')写法
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;">测试文字</div>
<script>show.onclick = function(){ test.removeAttribute('hidden'); /*test.hidden = '';*/} hide.onclick = function(){ test.setAttribute('hidden','hidden'); /*test.hidden = 'hidden';*/}</script>
思路四: opacity
对于元素显隐,opacity的使用频率也挺多。opacity的好处是,即使opacity为0的元素,仍然可以接受javascript事件,这是display:none和visiblity:hidden所不具备的。
<button id="show">显示</button><button id="hide">隐藏</button><button id="reset">还原</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;">测试文字</div>
<script>show.onclick = function(){ test.style.transition = 'none'; test.style.opacity = '1';} hide.onclick = function(){ test.style.transition = 'opacity 0.2s'; test.style.opacity = '0';}test.onclick = function(){ this.style.width = '200px';}reset.onclick = function(){ history.go();}</script>
思路五: overflow
CSS中有一个属性是overflow,overflow:hidden代表着溢出隐藏。我们可以利用父级的overflow:hidden配合父级的height:0或width:0来实现元素的显隐
[注意]当设置overflow的元素在绝对定位元素和其包含块之间的时候,overflow属性会失效
<style>#testWrap{ height: 70px; transition: height 1s; overflow: hidden;}</style>
<button id="show">显示</button><button id="hide">隐藏</button><div id="testWrap"> <div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;">测试内容</div> </div>
<script>show.onclick = function(){ testWrap.style.height = '70px';} hide.onclick = function(){ testWrap.style.height = '0';}</script>
思路六: clip
CSS裁剪clip这个属性平时用的不多,当clip:rect(top,right,bottom,left)中的top>=bottom,或者left>=right时,可实现元素的隐藏效果,效果类似于visibility:hidden
[注意]clip属性只能应用在绝对定位元素上
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;">测试内容</div>
<script>show.onclick = function(){ test.style.position ='static'; test.style.clip = 'auto';} hide.onclick = function(){ test.style.position ='absolute'; test.style.clip = 'rect(0 0 0 0)';}</script>
思路七: transform
CSS变形transform是一些效果的集合,主要是移动、旋转、缩放和倾斜这四种基本操作,还可以通过设置matrix矩阵来实现更复杂的效果。通过不同的变形函数可以实现元素显隐效果
[注意]IE9-浏览器不支持,safari3.1-8、android2.1-4.4.4、IOS3.2-8.4都需要添加前缀
【1】scale
transform:scale(0)时,元素被隐藏
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;transition: 0.5s;">测试内容</div>
<script>show.onclick = function(){ test.style.transform ='scale(1)';} hide.onclick = function(){ test.style.transform ='scale(0)';}</script>
【2】rotate
transform:rotateX(90deg)时,元素被隐藏
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;transition: 0.5s;">测试内容</div>
<script>show.onclick = function(){ test.style.transform ='rotateX(0)';} hide.onclick = function(){ test.style.transform ='rotateX(90deg)';}</script>
【3】skew
transform:skew(90deg)时,元素被隐藏
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;transition: 0.5s;">测试内容</div>
<script>show.onclick = function(){ test.style.transform ='skew(0)';} hide.onclick = function(){ test.style.transform ='skew(90deg)';}</script>
思路八: 覆盖
利用定位元素可以覆盖普通流元素的特性。为元素的before伪元素设置相同的尺寸,通过控制伪元素的定位属性,实现显隐效果
<style>#test:hover:before{ content: ""; position: absolute; width: 100px; height: 60px; background-color: white;} </style>
<div style="background:lightblue;width:100px;height:60px;margin-top: 10px;">测试内容</div>
//鼠标移入移出会出现元素的显隐效果
思路九: 偏移
元素显示隐藏的另一种常见思路是偏移,将元素移动到视窗范围外,也可以实现等价的显隐效果
【1】margin-top
利用负margin将元素移出视窗外,要注意的是设置负margin的元素并没有脱离普通流,后续元素会跟着一起移动
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;transition: 0.5s;">测试内容</div>
<script>show.onclick = function(){ test.style.marginTop ='10px';} hide.onclick = function(){ test.style.marginTop ='-9999px';}</script>
【2】left
通过设置相对定位或绝对定位元素的偏移属性,将元素移动到视窗外
<style>#test{ position: relative;/* position: absolute; */} </style>
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;transition: 0.5s;">测试内容</div>
<script>show.onclick = function(){ test.style.left ='0';} hide.onclick = function(){ test.style.left ='-9999px';}</script>
【3】translate
<button id="show">显示</button><button id="hide">隐藏</button><div id="test" style="background:lightblue;width:100px;height:60px;margin-top: 10px;transition: 0.5s;">测试内容</div>
<script>show.onclick = function(){ test.style.transform ='translate(0,0)';} hide.onclick = function(){ test.style.transform ='translate(-9999px,-9999px)';}</script>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











이 기사는 HTML & lt; Progress & Gt에 대해 설명합니다. 요소, 그 목적, 스타일 및 & lt; meter & gt의 차이; 요소. 주요 초점은 & lt; progress & gt; 작업 완료 및 & lt; meter & gt; Stati의 경우

이 기사는 HTML & LT; Datalist & GT에 대해 논의합니다. 자동 완성 제안을 제공하고, 사용자 경험을 향상시키고, 오류를 줄임으로써 양식을 향상시키는 요소. 문자 수 : 159

이 기사는 HTML & lt; meter & gt에 대해 설명합니다. 범위 내에 스칼라 또는 분수 값을 표시하는 데 사용되는 요소 및 웹 개발의 일반적인 응용 프로그램. & lt; meter & gt; & lt; Progress & Gt; 그리고 Ex

기사는 HTML5 크로스 브라우저 호환성을 보장하기위한 모범 사례에 대해 논의하고 기능 감지, 점진적 향상 및 테스트 방법에 중점을 둡니다.

이 기사는 모바일 장치의 반응 형 웹 디자인에 필수적인 Viewport Meta Tag에 대해 설명합니다. 적절한 사용이 최적의 컨텐츠 스케일링 및 사용자 상호 작용을 보장하는 방법을 설명하는 반면, 오용은 설계 및 접근성 문제로 이어질 수 있습니다.

이 기사는 html5 & lt; time & gt; 시맨틱 날짜/시간 표현 요소. 인간이 읽을 수있는 텍스트와 함께 기계 가독성 (ISO 8601 형식)에 대한 DateTime 속성의 중요성을 강조하여 Accessibilit를 향상시킵니다.

이 기사에서는 브라우저에서 직접 사용자 입력을 검증하기 위해 필요한, Pattern, Min, Max 및 Length 한계와 같은 HTML5 양식 검증 속성을 사용하는 것에 대해 설명합니다.

이 기사는 & lt; iframe & gt; 외부 컨텐츠를 웹 페이지, 공통 용도, 보안 위험 및 객체 태그 및 API와 같은 대안을 포함시키는 태그의 목적.
