搜尋
首頁web前端html教學元素显示隐藏的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>    

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
超越HTML:網絡開發的基本技術超越HTML:網絡開發的基本技術Apr 26, 2025 am 12:04 AM

要構建一個功能強大且用戶體驗良好的網站,僅靠HTML是不夠的,還需要以下技術:JavaScript賦予網頁動態和交互性,通過操作DOM實現實時變化。 CSS負責網頁的樣式和佈局,提升美觀度和用戶體驗。現代框架和庫如React、Vue.js和Angular,提高開發效率和代碼組織結構。

HTML中的布爾屬性是什麼?舉一些例子。HTML中的布爾屬性是什麼?舉一些例子。Apr 25, 2025 am 12:01 AM

布爾屬性是HTML中的特殊屬性,不需要值即可激活。 1.布爾屬性通過存在與否控制元素行為,如disabled禁用輸入框。 2.它們的工作原理是瀏覽器解析時根據屬性的存在改變元素行為。 3.基本用法是直接添加屬性,高級用法可通過JavaScript動態控制。 4.常見錯誤是誤以為需要設置值,正確寫法應簡潔。 5.最佳實踐是保持代碼簡潔,合理使用布爾屬性以優化網頁性能和用戶體驗。

如何驗證您的HTML代碼?如何驗證您的HTML代碼?Apr 24, 2025 am 12:04 AM

HTML代碼可以通過在線驗證器、集成工具和自動化流程來確保其清潔度。 1)使用W3CMarkupValidationService在線驗證HTML代碼。 2)在VisualStudioCode中安裝並配置HTMLHint擴展進行實時驗證。 3)利用HTMLTidy在構建流程中自動驗證和清理HTML文件。

HTML與CSS和JavaScript:比較Web技術HTML與CSS和JavaScript:比較Web技術Apr 23, 2025 am 12:05 AM

HTML、CSS和JavaScript是構建現代網頁的核心技術:1.HTML定義網頁結構,2.CSS負責網頁外觀,3.JavaScript提供網頁動態和交互性,它們共同作用,打造出用戶體驗良好的網站。

HTML作為標記語言:其功能和目的HTML作為標記語言:其功能和目的Apr 22, 2025 am 12:02 AM

HTML的功能是定義網頁的結構和內容,其目的在於提供一種標準化的方式來展示信息。 1)HTML通過標籤和屬性組織網頁的各個部分,如標題和段落。 2)它支持內容與表現分離,提升維護效率。 3)HTML具有可擴展性,允許自定義標籤增強SEO。

HTML,CSS和JavaScript的未來:網絡開發趨勢HTML,CSS和JavaScript的未來:網絡開發趨勢Apr 19, 2025 am 12:02 AM

HTML的未來趨勢是語義化和Web組件,CSS的未來趨勢是CSS-in-JS和CSSHoudini,JavaScript的未來趨勢是WebAssembly和Serverless。 1.HTML的語義化提高可訪問性和SEO效果,Web組件提升開發效率但需注意瀏覽器兼容性。 2.CSS-in-JS增強樣式管理靈活性但可能增大文件體積,CSSHoudini允許直接操作CSS渲染。 3.WebAssembly優化瀏覽器應用性能但學習曲線陡,Serverless簡化開發但需優化冷啟動問題。

HTML:結構,CSS:樣式,JavaScript:行為HTML:結構,CSS:樣式,JavaScript:行為Apr 18, 2025 am 12:09 AM

HTML、CSS和JavaScript在Web開發中的作用分別是:1.HTML定義網頁結構,2.CSS控製網頁樣式,3.JavaScript添加動態行為。它們共同構建了現代網站的框架、美觀和交互性。

HTML的未來:網絡設計的發展和趨勢HTML的未來:網絡設計的發展和趨勢Apr 17, 2025 am 12:12 AM

HTML的未來充滿了無限可能。 1)新功能和標準將包括更多的語義化標籤和WebComponents的普及。 2)網頁設計趨勢將繼續向響應式和無障礙設計發展。 3)性能優化將通過響應式圖片加載和延遲加載技術提升用戶體驗。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具