藉助HTML5details,summary無JS實現各種互動效果
瞭解HTML5 details, summary預設互動行為
<details>
標籤在Chrome,Firefox等瀏覽器下預設是有展開收起行為的,例如下面HTML:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>這是摘要1<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>></span>這裡具體描述,標籤相對隨意,例如這裡使用的<p>標籤。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
結果UI表現為:
具體描述為:
-
只顯示了
<summary>
標籤內容,而<p>
預設隱藏了; -
<summary>
標籤前面出現了一個小三角;
小三角圖形的隱喻是:我是可點選的,點選我可能會出現寶箱。
OK,我們不妨就點選一下,結果如下圖:
具體描述為:
-
原本隱藏的
<p>
標籤顯示出來了; -
<summary>
標籤前面的小三角方向朝下了;
此時我們再一次點選,<p>
標籤內容又會隱藏收起,箭頭方向還原,如下圖:
活脫脫一個天然的展開收起效果。
展開與收起是通過open
屬性控制的
通過在<details>
標籤上新增布林型別的open
屬性,可以讓我們的詳情資訊預設就是展開狀態,如下HTML示意:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>這是摘要2<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>></span>這裡<details>標籤設定了HTML布林屬性open,因此,預設是展開狀態。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
結果如下截圖:
如果我們使用JS指令碼手動移除這個open屬性,即使沒有點選行為的發生,我們內容也會收起。
<summary>
如果預設
<summary>
標籤如果預設,則<details>
元素會在內部自動建立一個<summary>
內容,預設的文案是“詳細資訊”。如下HTML程式碼:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>></span>如果<summary>預設,則會自動補上,文案是“詳細資訊”。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
結果如下截圖所示:
您可以狠狠地點選這裡:HTML details,summary基本效果Demo。
details瀏覽器內建UI可以自定義
<details>
標籤預設的小三角樣式有些簡陋,在實際應用的時候,往往不是我們希望的樣子,不要擔心,我們是可以對其進行自定義的。在Chrome等瀏覽器下使用::-webkit-details-marker
,在Firefox瀏覽器下使用::-moz-list-bullet
可以對小三角進行UI控制,例如改變顏色,改變大小,使用自定義的圖形代替,或者直接隱藏等,我們來看幾個簡單的案例。
小三角右側顯示同時顏色變淡
HTML程式碼如下:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>class</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“details-1”</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>這是示例1<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>></span>本案例展示對小三角UI重定義:包括顯示在右側,顏色減淡等。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
CSS如下:
.details-1 summary {
width: -moz-fit-content;
width: fit-content;
direction: rtl;
}
.details-1 ::-webkit-details-marker {
direction: ltr;
color: gray;
margin-left: .5ch;
}
.details-1 ::-moz-list-bullet {
direction: ltr;
color: gray;
margin-left: .5ch;
}
結果如下圖所示:
當我們點選摘要標題升起的時候,表現為下圖(截自Firefox):
眼見為實,您可以狠狠地點選這裡:HTML details小三角位置顏色改變Demo
而實際上實際開發的時候,對小三角UI更便捷的定製方法是:隱藏瀏覽器原生的小三角,然後藉助::before
或::after
偽元素重新生成我們想要的UI效果,下面這個案例就將展示相關的處理。
隱藏瀏覽器原生的小三角並使用自定義三角替換
HTML結構還是類似的:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>class</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“details-2”</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>這是示例2<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>></span>本案例隱藏原生小三角,使用自定義小三角。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
CSS主要分為2部分,一部分是隱藏瀏覽器原生的小三角,另外一部分是使用偽元素生成自定義的三角效果。
首先看一下隱藏<details>
標籤預設的小三角的CSS:
/* 隱藏預設三角 */
.details-2 ::-webkit-details-marker {
display: none;
}
.details-2 ::-moz-list-bullet {
font-size: 0;
}
可以看到Chrome瀏覽器和Firefox瀏覽器的小三角隱藏採用的是不同的策略。在Chrome瀏覽器下,我們可以直接設定display:none
進行隱藏,但是這一招在Firefox瀏覽器下確實沒有效果的,即使設定display:none!important
也是如此,根據我的測試,只有font-size:0
能夠比較完美的隱藏。類似position:absolute;visibility:hidden
這種常見的隱藏也是不行的,因為position:absolute
無法生效。
然後是自定義小三角顯示的CSS,這裡採用的是::after
偽元素模擬的:
/* 自定義的三角 */
.details-2 summary::after {
content: ``;
position: absolute;
width: 1em; height: 1em;
margin: .2em 0 0 .5ch;
background: url(./arrow-on.svg) no-repeat;
background-size: 100% 100%;
transition: transform .2s;
}
.details-2:not([open]) summary::after {
margin-top: .25em;
transform: rotate(90deg);
}
最終效果如下圖所示:
收起時候:
眼見為實,您可以狠狠地點選這裡:HTML details小三角自定義Demo。
最後有一點需要注意一下,就是如果<details>
標籤內並沒有<summary>
元素,則我們的對三角的自定義程式碼都是無效的,可以使用一個空的<summary>
元素佔位,類似這樣:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>></span>內容。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
Chrome瀏覽器下點選時候outline輪廓等體驗處理
UI可以定製了,但是還有個不容忽視的體驗問題,那就是在Chrome瀏覽器下點選時候會出現outline
輪廓,如下圖所示:
在實際專案開發的時候,產品和設計一定會讓你把這個效果去掉的。以及,當我們<summary>
元素點選較快的時候,文字會被選中,也不是我們想看到的。
阻止文字選中,我們可以:
summary {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
對於outline
輪廓,比較直接的做法是:
summary {
outline: 0;
}
但是這樣處理對無障礙訪問而是非常不友好的,那有沒有什麼辦法兼顧視覺體驗和無障礙訪問體驗呢?
我的做法是這樣子的:
利用<a>
標籤的outline
互動體驗
瀏覽器對<a>
標籤元素的outline
輪廓進行了專門的體驗優化處理,滑鼠點選的時候不顯示輪廓,鍵盤訪問時候顯示輪廓。於是我們可採用李代桃僵策略,讓<summary>
元素的outline
交給<a>
元素,方法就是在<summary>
中再內嵌一個<a>
,同時通過tabindex
屬性remove
掉<summary>
原本的可訪問性。HTML程式碼示意如下:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>tabindex</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“-1”</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“javascript:”</span></span>></span>這是示例<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>></span>點選無外框,鍵盤focus有。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
CSS如下:
summary {
user-select: none;
outline: 0;
}
summary a {
color: inherit;
}
此時,在Chrome瀏覽器下,我們點選摘要資訊,沒有任何outline
輪廓出現;但是當我們使用Tab
鍵索引時候,可以看到下圖所示的輪廓效果:
輪廓區域比原生的<summary>
要小,但這無傷大雅,而且實際專案開發的時候,我們會去掉小箭頭,此時只要設定<a>
標籤display:block
,則輪廓就可以和<summary>
保持一致了。
接下來,我們按下Space
空格鍵,就會發現<details>
元素內的內容資訊不斷的展開與收起:
您可以狠狠地點選這裡:HTML5 summary outline輪廓a
標籤處理Demo。
然後上面實現並不完美,相比原生的<summary>
元素,Enter
Enter鍵展開收起效果丟失了。這是因為HTML元素中如果多個focusable
同時帶click
瀏覽器行為元素巢狀的時候,點選裡面的元素,外部元素的瀏覽器行為是不會觸發的。類似的有<label>
內嵌<a>
標籤。
對於<a>
標籤,其瀏覽器行為只能通過Enter鍵觸發,空格鍵是無效的;但是對於<summary>
,Enter鍵和空格鍵都能觸發展開收起行為,這就是為什麼上面程式碼空格鍵有效,Enter鍵無效的原因。
如果想要同時支援Enter鍵展開與收起,可以對HTML如下處理:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>tabindex</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“-1”</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“javascript:”</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>onClick</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“this.parentNode.click();”</span></span>></span>這是示例<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>></span>點選無外框,鍵盤focus有。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>content</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
需要注意的是上面處理在<summary>
自己額外繫結click
事件時候可能會有double
觸發的問題,此時,阻止<a>
元素的冒泡即可。
JS捕獲鍵盤行為手動設定outline
這個方法不需要對HTML進行任何的改動,是通過CSS和JS配合對全域性的<summary>
元素進行outline
優化。
CSS如下:
summary {
user-select: none;
outline: 0;
}
summary[focus] {
outline: 1px dotted;
outline: 5px auto -webkit-focus-ring-color;
}
JS如下:
window.addEventListener(`keydown`, function () {
window.isKeyEvent = true;
setTimeout(function () {
window.isKeyEvent = false;
}, 100);
});
document.addEventListener(`focusin`, function (event) {
var target = event.target; if (target && target.tagName.toLowerCase() == `summary` && window.isKeyEvent == true) {
target.setAttribute(`focus`, ``);
}
});
document.addEventListener(`focusout`, function (event) {
var eleFocusAll = document.querySelectorAll(`summary[focus]`);
[].slice.call(eleFocusAll).forEach(function (summary) {
summary.removeAttribute(`focus`);
});
});
只要把上面的CSS和JS複製到頁面中,視覺體驗和互動體驗完美支援的<summary>
元素outline
效果就有了。
表現為,點選<summary>
沒有任何outline
,鍵盤focus
時候出現,且和瀏覽器原生outline
效果一模一樣,Space
鍵和Enter
鍵展開與收起訪問完全保留。
眼見為實,您可以狠狠的點選這裡:HTML5 summary outline輪廓JS優化Demo。
例如下圖就是鍵盤Tab鍵focus後回車後的效果:
每每看到如此極致的使用者體驗處理,心情都大好。
原理:關鍵是全域性監聽
keydown
事件,如果有發生,則認為此100ms
內的頁面focus
行為均是鍵盤產生,從而有效區分是點選觸發的focus
行為還是鍵盤觸發的focus
行為,如果是鍵盤觸發,給<summary>
元素手動增加outline
效果。
基於details
元素行為的各種互動效果案例
瞭解了<details>
元素的點選互動行為;解決了UI定製難題;解決了outline
的體驗問題,下面我們就可以付諸實踐,不借助任何JS來實現各種我們平常見到的互動效果。
“更多”展開與收起效果
實現最終效果如下gif:
因為“更多”元素是在底部,因此效果實現的要點的所有的內容資訊都放在<summary>
元素內部,然後通過<details>
元素的open
屬性控制UI的變化。
HTML和CSS程式碼如下,其中,最核心部分已經紅色高亮:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>></span>據臺媒報導,大…青睞。<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>class</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“more”</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>></span>其他幾首歌曲…<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>p</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span>更多<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span> <br><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>><br><br></span>::-webkit-details-marker {
- display: none;
- }
- ::-moz-list-bullet {
- font-size: 0;
- float: left;
- }
- .more {
- display: none;
- }
- [open] .more {
- display: block;
- }
- [open] summary a {
- font-size: 0;
- }
- [open] summary a::before {
- content: `收起`;
- font-size: 14px;
- }</p>
把“更多”對應的資訊放在.more
元素內,然後通過[open]
屬性選擇器控制器顯示,效果即達成。
您可以狠狠的點選這裡:HTML5 details/summary更多展開收起Demo。
無JS實現點選顯示懸浮選單,自定義下拉框等效果
效果如下gif:
沒有任何JS參與。HTML結構如下:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>我的訊息<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>class</span></span>=<span class=“value” style=“box-sizing: inherit;color: rgb(42, 161, 152);”><span class=“value” style=“box-sizing: inherit;”>“box”</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>我的回答<span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>sup</span></span>></span>12<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>sup</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>我的私信<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>未評價訂單<span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>sup</span></span>></span>2<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>sup</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>我的關注<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
然後CSS讓.box
元素絕對定位即可,顯示和隱藏<details>
元素內建行為就搞定了。
您可以狠狠地點選這裡:HTML5 details/summary懸浮選單Demo。
Accordion多項摺疊效果
此效果常見於條目比較多的垂直導航欄,新聞條目等。
例如下面實現的效果:
這個更加簡單了,就是一堆<details>
元素並排放置就可以了,如下HTML:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dt</span></span>></span>訂單中心<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dt</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>我的訂單<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>我的活動<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>評價曬單<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>購物助手<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dt</span></span>></span>關注中心<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dt</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>關注的商品<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span>
- …<br><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>><br><br></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>></span>
- …<br><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
計算CSS沒有任何設定,效果也天然達成。
您可以狠狠地的點選這裡:HTML5 details/summary多列選單摺疊Demo。
帶slideUp/slideDown效果的多項摺疊選單
案例3中的展開項顯示的時候是非常生硬的突然顯示,實際上我們可以藉助一些選擇器技巧以及CSS3 transition
屬性讓選單展開收起的時候是有動畫效果的,效果如下gif截圖:
此效果實現原理核心是[open]
屬性選擇器,和加號+
相鄰兄弟選擇器。
首先看下HTML,展開列表結構發生了變化,不是作為<details>
的子元素,而是作為其相鄰兄弟元素存在,HTML示意:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>open</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”> <<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>訂單中心<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dl</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>我的訂單<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>我的活動<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>評價曬單<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>></span><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span> <span class=“attribute” style=“box-sizing: inherit;color: rgb(181, 137, 0);”><span class=“attribute” style=“box-sizing: inherit;”>href</span></span>></span>購物助手<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>a</span></span>></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dd</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>dl</span></span>><br><br></span>…</p>
上面<dl>
定義列表就是展開收起的內容,其作為兄弟元素和<details>
元素平起平坐,於是,我們就可以利用點選<summary>
元素<details>
元素的open
屬性會變化的特性實現我們想要的動畫效果,CSS如下:
details + dl {
max-height: 0;
transition: max-height .25s;
overflow: hidden;
}
[open] + dl {
max-height: 100px;
}
藉助相鄰兄弟選擇器以及max-height
任意元素slideUp/slideDown技術就可以效果達成。
您可以狠狠地點選這裡:HTML5 details/summary多列選單滑入滑出Demo。
多級巢狀的樹形選單互動效果
這裡的樹形選單效果實現也很簡單,多個<details>
元素相互巢狀就可以,效果Gif如下:
HTML結構大致如下:
- <p style=“box-sizing: inherit;font-family: monospace, monospace;font-size: 1em;display: block;padding: 0.5em;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;line-height: 2em;”><span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>我的視訊<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>爆肝工程師的異世界狂想曲<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>tv1-720p.mp4<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>tv2-720p.mp4<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>
- … <br> <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>tv10-720p.mp4<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>七大罪<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>summary</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>七大罪B站00合集.mp4<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>珍藏動漫網盤地址.txt<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>
- <span class=“tag” style=“box-sizing: inherit;”><<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>></span>我們的小美好.mp4<span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>div</span></span>><br></span><span class=“tag” style=“box-sizing: inherit;”></<span class=“title” style=“box-sizing: inherit;color: rgb(38, 139, 210);”><span class=“title” style=“box-sizing: inherit;”>details</span></span>></span></p>
CSS的主要工作就是繪製選單前面的加號和減號圖形,例如我們可以藉助background
線性漸變,具體可參見文章“CSS3 linear-gradient線性漸變實現虛線等簡單實用圖形”,相關CSS如下:
details {
padding-left: 20px;
}
summary::before {
content: ``;
display: inline-block;
width: 12px; height: 12px;
border: 1px solid #999;
background: linear-gradient(to right, #999, #999) no-repeat center, linear-gradient(to top, #999, #999) no-repeat center;
background-size: 2px 10px, 10px 2px;
vertical-align: -2px;
margin-right: 6px;
margin-left: -20px;
}
[open] > summary::before {
background: linear-gradient(to right, #999, #999) no-repeat center;
background-size: 10px 2px;
}
效果即達成!
您可以狠狠地點選這裡:HTML5 details/summary樹形選單Demo。
如果只想要details/summary的語義不要行為
如果只想要<details>
元素,<summary>
元素的語義,但是並不需要點選展開收起的行為,該怎麼處理呢?
例如,某評論,或者某帖子有標題和正文,非常符合詳情-概要-內容的語義,但是希望是純展示的,點選時候不收起,可以這麼處理:
<summary>
標籤設定tabindex="-1"
讓鍵盤無法訪問;設定CSS:
summary {
outline: 0;
pointer-events: none;
}
這樣就不能點,也不會有outline
輪廓。
相容性以及Polyfill
除了IE和Edge瀏覽器,大好河山一片綠,至少移動端可以用得比較開心。
如果想要在桌面Web網頁使用<details>
元素的棒棒噠特性,我們可以對其進行Polyfill,可以參見此專案。
對鍵盤訪問,事件toggle
都做了相容。
如果開發策略是對不支援的IE進行特異處理,則下面的JS判斷是否支援<details>
元素的指令碼可能對你有用:
var isSupportDetails = `open` in document.createElement(`details`);
結束語麼麼噠
無JS實現的好處有:
-
省了程式碼,載入快了;
-
實現更簡單了,開發快了;
-
JS還沒載入互動也能進行,體驗好了;
-
鍵盤無障礙和
aria
閱讀裝置無障礙天然支援,體驗檔次高了。 -
可以跟同事炫,逼格上去了。
-
好,感謝閱讀,行為倉促,歡迎糾錯。
相關文章
- 重寫ViewGroup並藉助ViewDragHelper實現各種拖拽互動效果(二)View
- 利用HTML5,無JS實現各種互動效果HTMLJS
- 藉助babel理解jsxBabelJS
- JS互動 點選WKWebView中的圖片實現預覽效果JSWebView
- js、jQuery實現文字上下無縫輪播、滾動效果JSjQuery
- Seam無縫整合 JSF: 藉助 Seam 進行對話JS
- iOS後臺模式藉助位置更新實現iOS模式
- 【新手指南】App原型設計:如何快速實現這6種互動效果?APP原型
- 互動投影的幾種實現方式
- 藉助 Webpack 靜態分析能力實現程式碼動態載入Web
- 「譯」如何實現互動式 WebGL 懸停效果Web
- Android一種翻板式互動效果Android
- 藉助XPopup,用50行程式碼實現更好的抖音評論彈窗效果!行程
- 實現抖音 “影片無限滑動“效果
- js實現的單行文字不間斷無縫滾動效果JS
- 藉助 :has 實現3d輪播圖3D
- 短影片app原始碼,藉助輪詢最佳化互動體驗APP原始碼
- UIButton實現各種圖文結合的效果以及原理UI
- python藉助web3py與以太坊區塊鏈節點互動的幾種方式PythonWeb區塊鏈
- 企業展廳互動能實現什麼效果
- 資料驅動模式藉助react的實踐探索模式React
- AngularJS中Directive間互動實現合成AngularJS
- echart 各種圖實現
- Java 藉助ImageMagic實現圖片編輯服務Java
- python與mysql互動中的各種坑PythonMySql
- Python(一)Android藉助Python實現打包自動上傳firPythonAndroid
- Android 的滑動分析以及各種實現Android
- 互動滑軌屏的幾種實現形式
- js實現打字效果JS
- js實現的模擬滾動條效果JS
- 大屏報表元件間的聯動互動效果實現方法元件
- JS的各種排序演算法實現--暫時不懂JS排序演算法
- 記錄---實現抖音 “影片無限滑動“效果
- 食品企業如何藉助SAP實現高效智慧化管理
- 【.NET】控制檯應用程式的各種互動玩法
- JS 與 OC 互動的三種方法JS
- js滑鼠懸浮字串實現字串跳動效果JS字串
- js拖動實現左右圖片對比效果JS