A Pen by Stan Williams on CodePen.
Created
May 19, 2024 20:50
-
-
Save stanwmusic/0336d6c8fe336193d4e7845482ef0d43 to your computer and use it in GitHub Desktop.
Accessible Tabs ( Animation )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<main> | |
<header> | |
<h2><span>Table</span> of content</h2> | |
</header> | |
<section> | |
<ul class='indexes'> | |
<li data-index='0'>01</li> | |
<li data-index='1'>02</li> | |
<li data-index='2'>03</li> | |
<li data-index='3'>04</li> | |
</ul> | |
<ul class='tabs'> | |
<li class='tab'> | |
<article class='tab-content'> | |
<h3>Midnight Station</h3> | |
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. | |
Voluptas nihil sequi doloribus obcaecati. Aut vel, recusandae ipsa | |
voluptate blanditiis nemo magnam sit modi architecto officia | |
maiores magni. Necessitatibus, iste aut.</p> | |
<button>Read More</button> | |
</article> | |
<div class='tab-image'><img src='https://picsum.photos/id/345/1000/600'></div> | |
</li> | |
<li class='tab'> | |
<article class='tab-content'> | |
<h3>The Hitchhiker</h3> | |
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. | |
Voluptas nihil sequi doloribus obcaecati. Aut vel, recusandae ipsa | |
voluptate blanditiis nemo magnam sit modi architecto officia | |
maiores magni. Necessitatibus, iste aut.</p> | |
<button>Read More</button> | |
</article> | |
<div class='tab-image'><img src='https://picsum.photos/id/352/1000/600'></div> | |
</li> | |
<li class='tab'> | |
<article class='tab-content'> | |
<h3>Missing Pages</h3> | |
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. | |
Voluptas nihil sequi doloribus obcaecati. Aut vel, recusandae ipsa | |
voluptate blanditiis nemo magnam sit modi architecto officia | |
maiores magni. Necessitatibus, iste aut.</p> | |
<button>Read More</button> | |
</article> | |
<div class='tab-image'><img src='https://picsum.photos/id/444/1000/600'></div> | |
</li> | |
<li class='tab'> | |
<article class='tab-content'> | |
<h3>Uninvited Guests</h3> | |
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. | |
Voluptas nihil sequi doloribus obcaecati. Aut vel, recusandae ipsa | |
voluptate blanditiis nemo magnam sit modi architecto officia | |
maiores magni. Necessitatibus, iste aut.</p> | |
<button>Read More</button> | |
</article> | |
<div class='tab-image'><img src='https://picsum.photos/id/451/1000/600'></div> | |
</li> | |
</ul> | |
</section> | |
</main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const indexes = document.querySelectorAll('.indexes li'); | |
const tabs = document.querySelectorAll('.tab'); | |
const contents = document.querySelectorAll('.tab-content'); | |
function reset() { | |
for (let i = 0; i < tabs.length; i++) { | |
indexes[i].style.borderColor = 'transparent'; | |
tabs[i].style.zIndex = 0; | |
tabs[i].classList.remove('active'); | |
contents[i].classList.remove('active'); | |
} | |
} | |
function showTab(i) { | |
indexes[i].style.borderColor = 'rgba(211,38,38,0.6)'; | |
tabs[i].style.opacity = 1; | |
tabs[i].style.zIndex = 5; | |
tabs[i].classList.add('active'); | |
contents[i].classList.add('active'); | |
} | |
function activate(e) { | |
if (!e.target.matches('.indexes li')) return; | |
reset(); | |
showTab(e.target.dataset.index); | |
} | |
const init = () => showTab(0); | |
window.addEventListener('load',init,false); | |
window.addEventListener('click',activate,false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:root { | |
--primary: rgb(211,38,38); | |
--overlay: rgba(211,38,38,0.6); | |
} | |
* { | |
margin: 0; | |
padding: 0; | |
box-sizing: border-box; | |
} | |
body { | |
height: 100vh; | |
display: grid; | |
place-items: center; | |
background-color: black; | |
color: rgba(255,255,255,0.85); | |
} | |
main { | |
width: 600px; | |
height: 300px; | |
font: 0.7rem impact,sans-serif; | |
& header { | |
font-size: 1.2rem; | |
text-transform: uppercase; | |
margin-bottom: 2.25rem; | |
color: white; | |
& span { | |
color: var(--primary); | |
} | |
} | |
& section { | |
display: flex; | |
gap: 2rem; | |
} | |
} | |
.indexes, .tabs { list-style-type: none; } | |
.indexes { | |
font-size: 1rem; | |
& li { | |
padding: 1rem; | |
border: 1px solid transparent; | |
cursor: pointer; | |
} | |
} | |
.tabs { position: relative; } | |
.tab { | |
position: absolute; | |
display: flex; | |
width: 530px; | |
height: 225px; | |
opacity: 0; | |
background-color: black; | |
overflow: hidden; | |
} | |
.tab-content { | |
position: relative; | |
z-index: 5; | |
width: 300px; | |
display: flex; | |
flex-direction: column; | |
justify-content: space-between; | |
gap: 1rem; | |
opacity: 0; | |
transform: translateY(-5rem); | |
& h3 { | |
font-family: helvetica; | |
font-weight: 900; | |
font-size: 1rem; | |
border-bottom: 1.5px solid white; | |
padding-bottom: 1rem; | |
} | |
& p { | |
font-family: helvetica; | |
font-weight: 100; | |
line-height: 2; | |
color: rgba(255,255,255,0.7); | |
} | |
& button { | |
width: fit-content; | |
background-color: transparent; | |
color: white; | |
border: 1px solid white; | |
font-size: 0.7rem; | |
padding: 0.75rem 1rem; | |
cursor: pointer; | |
} | |
} | |
@keyframes content { | |
100% { | |
opacity: 1; | |
transform: translateY(0); | |
} | |
} | |
.tab-image { | |
position: absolute; | |
right: 1rem; | |
width: 200px; | |
height: 200px; | |
opacity: 0; | |
transform: translateX(2rem); | |
&::after { | |
content: ''; | |
position: absolute; | |
inset: 0; | |
background-color: var(--overlay); | |
mix-blend-mode: multiply; | |
} | |
& img { | |
width: inherit; | |
height: inherit; | |
object-fit: cover; | |
filter: grayscale(100%); | |
} | |
} | |
@keyframes image { | |
100% { | |
opacity: 1; | |
width: 300px; | |
transform: translateX(0); | |
} | |
} | |
.active .tab { opacity: 1; z-index: 5; } | |
.active .tab-content { animation: content 0.9s ease-out 0.4s forwards; } | |
.active .tab-image { animation: image 1s ease-out forwards; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment