Inspired by https://dribbble.com/shots/3945978-Alignment-button
A Pen by Tobias Reich on CodePen.
<div class="toolbar"> | |
<div class="icon icon--absolute"> | |
<div class="bar"></div> | |
<div class="bar"></div> | |
<div class="bar bar--short"></div> | |
</div> | |
<div class="icon icon--left"> | |
<div class="bar"></div> | |
<div class="bar"></div> | |
<div class="bar bar--short"></div> | |
</div> | |
<div class="space"></div> | |
<div class="icon icon--center"> | |
<div class="bar"></div> | |
<div class="bar"></div> | |
<div class="bar bar--short"></div> | |
</div> | |
<div class="space"></div> | |
<div class="icon icon--right"> | |
<div class="bar"></div> | |
<div class="bar"></div> | |
<div class="bar bar--short"></div> | |
</div> | |
</div> |
document.querySelectorAll('.icon').forEach((elem, i) => { | |
elem.onclick = () => { | |
const toolbarLeft = document.querySelector('.toolbar').getBoundingClientRect().left | |
const elemLeft = elem.getBoundingClientRect().left | |
document.documentElement.style.setProperty('--icon', `${ elemLeft - toolbarLeft }px`) | |
document.documentElement.style.setProperty('--bar', `${ (i - 1) * 25 }%`) | |
} | |
}) |
body { | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
height: 100vh; | |
background: #f1f1f1; | |
} | |
.toolbar { | |
display: flex; | |
position: relative; | |
align-items: center; | |
padding: 1em 1.4em; | |
background: white; | |
border-radius: 4px; | |
box-shadow: 0 2px 8px rgba(0, 0, 0, .1); | |
} | |
.space { | |
width: 2em; | |
} | |
.icon { | |
position: relative; | |
display: flex; | |
flex-direction: column; | |
width: 28px; | |
opacity: .2; | |
cursor: pointer; | |
&--left { | |
align-items: flex-start; | |
} | |
&--center { | |
align-items: center; | |
} | |
&--right { | |
align-items: flex-end; | |
} | |
&--absolute { | |
position: absolute; | |
// Don't animate left in production! | |
left: var(--icon, 1.4em); | |
opacity: .6; | |
transition: left .6s cubic-bezier(.51, .92, .24, 1); | |
pointer-events: none; | |
} | |
&--absolute .bar--short { | |
position: relative; | |
// Don't animate left in production! | |
left: var(--bar); | |
transition: left .6s cubic-bezier(.51, .92, .24, 1); | |
} | |
} | |
.bar { | |
margin: 2px 0; | |
width: 100%; | |
height: 4px; | |
background: black; | |
border-radius: 2px; | |
&--short { | |
width: 50%; | |
} | |
} |
Inspired by https://dribbble.com/shots/3945978-Alignment-button
A Pen by Tobias Reich on CodePen.