HTML5使用details標(biāo)簽:展開/收縮信息

details 標(biāo)簽提供了一種替代 JavaScript 的方法,它主要是提供了一個展開/收縮區(qū)域。details 標(biāo)簽中可以使用 summary 標(biāo)簽從屬于 details 標(biāo)簽,單擊 summary 標(biāo)簽中的內(nèi)容文字時,details 標(biāo)簽中的其他所有從屬元素將會展開或收縮。
語法如下:
<details> <summary>標(biāo)題</summary> <p>文本內(nèi)容</p> </details>
details 標(biāo)簽可以可選地接受 open 屬性,以確保在頁面載入時該元素是展開的。
<details open> </details>
【實(shí)例】使用<details>標(biāo)簽實(shí)現(xiàn)展開/收縮信息。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="author" content="腳本之家"> <title>HTML5使用details標(biāo)簽:展開/收縮信息</title> <style type="text/css"> details { overflow: hidden; background: #e3e3e3; margin-bottom: 10px; display: block; } details summary { cursor: pointer; padding: 10px; } </style> </head> <body> <details open> <summary>腳本之家介紹</summary> <p>腳本之家是國內(nèi)專業(yè)的網(wǎng)站建設(shè)資源、腳本編程學(xué)習(xí)類網(wǎng)站,提供asp、php、asp.net、javascript、jquery、vbscript、dos批處理、網(wǎng)頁制作、網(wǎng)絡(luò)編程、網(wǎng)站建設(shè)等編程資料。</p> <p>http://www.dbjr.com.cn</p> </details> </body> </html>
執(zhí)行結(jié)果:
open 屬性
open
其值得類型為 Boolean
,作用為控制元素的顯示/隱藏
因?yàn)閐etails帶了open屬性,所以默認(rèn)就是打開狀態(tài),如果不加就是隱藏狀態(tài)
收縮效果:
當(dāng)然你也可以自定義 UI,默認(rèn)的確實(shí)不好看
設(shè)置樣式
details summary { /* 文字從右到左 */ direction: rtl; /* 容器縮小到文字大小 */ width: fit-content; } /* 選中三角符號 */ details ::marker { direction: ltr; color: gray; }
<details open> <summary> 文章概要 </summary> <div>文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容</div> </details>
效果圖
替換 UI
details summary { /* 容器縮小到文字大小 */ width: fit-content; height: 20px; line-height: 20px; } /* 隱藏原始三角 */ details ::marker { font-size: 0; } /* 自定義三角 */ summary::after { content: '?'; margin-left: .5ch; position: absolute; transition: transform .2s; } details:not([open]) summary::after { transform: rotate(90deg); }
<details open> <summary> 文章概要 </summary> <div>文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容文章內(nèi)容</div> </details>
效果如下:
實(shí)際交互
無JS實(shí)現(xiàn)點(diǎn)擊顯示懸浮菜單,自定義下拉框等效果
nav { background: #eee; padding-left: 40px; } details summary { width: fit-content; padding: 5px 28px; height: 28px; line-height: 28px; } /* 打開或懸停樣式 */ [open] summary, details summary:hover { background-color: #fff; box-shadow: inset 1px 0 #eee, inset -1px 0 #eee; } /* 隱藏原始三角 */ details ::marker { font-size: 0; } /* 自定義三角 */ summary::after { content: '▼'; margin-left: .3ch; color: grey; position: absolute; transition: transform .2s; } details:not([open]) summary::after { transform: rotate(-90deg); } /* 打開開啟全屏遮罩 */ [open] summary::before { content: ''; position: fixed; top: 0; bottom: 0; left: 0; right: 0; } .box { position: absolute; min-width: 120px; border: 1px solid #ddd; border-top: 0; box-sizing: border-box; } .box div{ padding: 8px 10px; cursor: pointer; } .box div:hover{ background: #eee; } .box div sup{ display: inline-block; width: 18px; height: 18px; text-align: center; line-height: 18px; border-radius: 50%; color: #fff; background: #d50000; }
html5代碼
<nav> <details open> <summary> 我的消息 </summary> <div class="box"> <div>我的私信<sup>12</sup></div> <div>我的回答</div> <div>我的關(guān)注</div> </div> </details> </nav>
效果如下:
到此這篇關(guān)于HTML5使用details標(biāo)簽:展開/收縮信息的文章就介紹到這了,更多相關(guān)HTML5展開/收縮信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
- Microdata作為HTML5新增的一個特性,它允許開發(fā)者在HTML文檔中添加更多的語義信息,以便于搜索引擎和瀏覽器更好地理解頁面內(nèi)容,本文將探討HTML5中Microdata的使用方法以及2025-04-21
- 在HTML語法中,表格主要通過< table >、< tr >和< td >3個標(biāo)簽構(gòu)成,本文通過實(shí)例代碼講解HTML5表格語法格式,感興趣的朋友一起看看吧2025-04-21
- 這篇文章主要介紹了HTML5中使用媒體查詢和Flexbox進(jìn)行響應(yīng)式布局的方法,簡要介紹了CSS Grid布局的基礎(chǔ)知識和如何實(shí)現(xiàn)自動換行的網(wǎng)格布局,感興趣的朋友一起看看吧2025-04-21
基于Canvas的Html5多時區(qū)動態(tài)時鐘實(shí)戰(zhàn)代碼
本文介紹了如何使用Canvas在HTML5上實(shí)現(xiàn)一個多時區(qū)動態(tài)時鐘的web展示,通過Canvas的API,可以繪制出6個不同城市的時鐘,并且這些時鐘可以動態(tài)轉(zhuǎn)動,每個時鐘上都會標(biāo)注出對應(yīng)的2025-03-11HTML5 data-*自定義數(shù)據(jù)屬性的示例代碼
HTML5的自定義數(shù)據(jù)屬性(data-*)提供了一種標(biāo)準(zhǔn)化的方法在HTML元素上存儲額外信息,可以通過JavaScript訪問、修改和在CSS中使用,文章還介紹了高級用法,如存儲JSON數(shù)據(jù)、事2025-03-11HTML5中下拉框<select>標(biāo)簽的屬性和樣式詳解
在HTML5中,下拉框(<select>標(biāo)簽)作為表單的重要組成部分,為用戶提供了一個從預(yù)定義選項(xiàng)中選擇值的方式,本文將深入探討<select>標(biāo)簽的屬性、樣式,并重點(diǎn)介2025-02-27- 本文介紹了HTML5InputDatePicker對象表示HTML``元素,是HTML5中的新對象,介紹了日期、周、月份、時間、日期+時間、本地日期時間等不同類型的日期選擇器,感興趣的朋友一起看2025-02-17
- 本文介紹了HTML5中的超鏈接、相對路徑和圖片的使用方法,超鏈接可以創(chuàng)建指向另一個文檔或頁面內(nèi)部書簽的鏈接,相對路徑用于在同一服務(wù)器內(nèi)部跳轉(zhuǎn)頁面,圖片標(biāo)簽用于引入外部圖2025-02-17
- 本文介紹了HTML5超鏈接的創(chuàng)建方法,包括基本語法、創(chuàng)建圖像超鏈接的邊框去除方法以及錨點(diǎn)鏈接的使用,還討論了超鏈接的四種不同狀態(tài)(link、visited、hover、active)的CSS樣2025-02-17
HTML5使用details標(biāo)簽:展開/收縮信息
最近看一些技術(shù)網(wǎng)站發(fā)現(xiàn)了details 標(biāo)簽的妙用,這個不用js即可實(shí)現(xiàn)展開/收縮信息,很方便用來讓用戶先才答案,然后下面點(diǎn)擊再給出答案的效果,這里就為大家簡單介紹一下,2024-11-03