|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en-US"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>ARIA tabbed info box</title> |
| 6 | + <style> |
| 7 | + |
| 8 | + /* General setup */ |
| 9 | + |
| 10 | + html { |
| 11 | + font-family: sans-serif; |
| 12 | + } |
| 13 | + |
| 14 | + * { |
| 15 | + box-sizing: border-box; |
| 16 | + } |
| 17 | + |
| 18 | + body { |
| 19 | + margin: 0; |
| 20 | + } |
| 21 | + |
| 22 | + /* info-box setup */ |
| 23 | + |
| 24 | + .info-box { |
| 25 | + width: 450px; |
| 26 | + height: 400px; |
| 27 | + margin: 0 auto; |
| 28 | + } |
| 29 | + |
| 30 | + /* styling info-box tabs */ |
| 31 | + |
| 32 | + ul[role="tablist"] { |
| 33 | + padding-left: 0; |
| 34 | + margin-top: 0; |
| 35 | + } |
| 36 | + |
| 37 | + li[role="tab"] { |
| 38 | + float: left; |
| 39 | + list-style-type: none; |
| 40 | + width: 150px; |
| 41 | + display: inline-block; |
| 42 | + line-height: 3; |
| 43 | + background-color: red; |
| 44 | + color: black; |
| 45 | + text-align: center; |
| 46 | + } |
| 47 | + |
| 48 | + li[role="tab"]:focus, li[role="tab"]:hover { |
| 49 | + background-color: #a60000; |
| 50 | + color: white; |
| 51 | + } |
| 52 | + |
| 53 | + li[role="tab"].active { |
| 54 | + background-color: #a60000; |
| 55 | + color: white; |
| 56 | + } |
| 57 | + |
| 58 | + /* styling info-box panels */ |
| 59 | + |
| 60 | + .info-box .panels { |
| 61 | + clear: both; |
| 62 | + position: relative; |
| 63 | + height: 352px; |
| 64 | + } |
| 65 | + |
| 66 | + .info-box article { |
| 67 | + background-color: #a60000; |
| 68 | + color: white; |
| 69 | + position: absolute; |
| 70 | + padding: 10px; |
| 71 | + height: 352px; |
| 72 | + top: 0; |
| 73 | + left: 0; |
| 74 | + } |
| 75 | + |
| 76 | + .info-box .active-panel { |
| 77 | + z-index: 1; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + </style> |
| 83 | +</head> |
| 84 | +<body> |
| 85 | + |
| 86 | +<section class="info-box"> |
| 87 | + <ul role="tablist"> |
| 88 | + <li class="active" role="tab" aria-selected="true" aria-setsize="3" aria-posinset="1" tabindex="0">Tab 1</li> |
| 89 | + <li role="tab" aria-selected="false" aria-setsize="3" aria-posinset="2" tabindex="0">Tab 2</li> |
| 90 | + <li role="tab" aria-selected="false" aria-setsize="3" aria-posinset="3" tabindex="0">Tab 3</li> |
| 91 | + </ul> |
| 92 | + <div class="panels"> |
| 93 | + <article class="active-panel" role="tabpanel" aria-hidden="false"> |
| 94 | + <h2>The first tab</h2> |
| 95 | + |
| 96 | + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque turpis nibh, porttitor nec venenatis eu, pulvinar in augue. Vestibulum et orci scelerisque, vulputate tellus quis, lobortis dui. Vivamus varius libero at ipsum mattis efficitur ut nec nisl. Nullam eget tincidunt metus. Donec ultrices, urna maximus consequat aliquet, dui neque eleifend lorem, a auctor libero turpis at sem. Aliquam ut porttitor urna. Nulla facilisi.</p> |
| 97 | + </article> |
| 98 | + <article role="tabpanel" aria-hidden="true"> |
| 99 | + <h2>The second tab</h2> |
| 100 | + |
| 101 | + <p>This tab hasn't got any Lorem Ipsum in it. But the content isn't very exciting all the same.</p> |
| 102 | + </article> |
| 103 | + <article role="tabpanel" aria-hidden="true"> |
| 104 | + <h2>The third tab</h2> |
| 105 | + |
| 106 | + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque turpis nibh, porttitor nec venenatis eu, pulvinar in augue. And now an ordered list: how exciting!</p> |
| 107 | + |
| 108 | + <ol> |
| 109 | + <li>dui neque eleifend lorem, a auctor libero turpis at sem.</li> |
| 110 | + <li>Aliquam ut porttitor urna.</li> |
| 111 | + <li>Nulla facilisi</li> |
| 112 | + </ol> |
| 113 | + </article> |
| 114 | + </div> |
| 115 | +</section> |
| 116 | + |
| 117 | +<script> |
| 118 | + |
| 119 | +let tabs = document.querySelectorAll('.info-box li'); |
| 120 | +let panels = document.querySelectorAll('.info-box article'); |
| 121 | + |
| 122 | +for(let i = 0; i < tabs.length; i++) { |
| 123 | + let tab = tabs[i]; |
| 124 | + setTabHandler(tab, i); |
| 125 | +} |
| 126 | + |
| 127 | +function setTab(e) { |
| 128 | + if(e.type === 'keypress' && e.keyCode !== 13) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + let tabPos = Number(this.getAttribute('aria-posinset'))-1; |
| 133 | + for(let i = 0; i < tabs.length; i++) { |
| 134 | + if(tabs[i].getAttribute('class')) { |
| 135 | + tabs[i].removeAttribute('class'); |
| 136 | + } |
| 137 | + |
| 138 | + tabs[i].setAttribute('aria-selected', 'false'); |
| 139 | + } |
| 140 | + |
| 141 | + this.setAttribute('class', 'active'); |
| 142 | + this.setAttribute('aria-selected', 'true'); |
| 143 | + |
| 144 | + for(let i = 0; i < panels.length; i++) { |
| 145 | + if(panels[i].getAttribute('class')) { |
| 146 | + panels[i].removeAttribute('class'); |
| 147 | + } |
| 148 | + |
| 149 | + panels[i].setAttribute('aria-hidden', 'true'); |
| 150 | + } |
| 151 | + |
| 152 | + panels[tabPos].setAttribute('class', 'active-panel'); |
| 153 | + panels[tabPos].setAttribute('aria-hidden', 'false'); |
| 154 | +} |
| 155 | + |
| 156 | +function setTabHandler(tab) { |
| 157 | + tab.addEventListener('click', setTab); |
| 158 | + tab.addEventListener('keypress', setTab); |
| 159 | +} |
| 160 | + |
| 161 | +</script> |
| 162 | + |
| 163 | +</body> |
| 164 | +</html> |
0 commit comments