Skip to content

Instantly share code, notes, and snippets.

@pestbarn
Last active January 4, 2019 10:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pestbarn/ec41e18bedd6f8456704 to your computer and use it in GitHub Desktop.
Save pestbarn/ec41e18bedd6f8456704 to your computer and use it in GitHub Desktop.
Menu to cross (pseudo elements and CSS transforms)
ul#hitbox {
list-style: none;
padding: 20px 10px; // padding to make a hitbox
height: 20px;
width: 20px;
cursor: pointer;
}
li#cross {
background: #e04681;
height: 2px;
transition: .25s all;
}
#cross::before,
#cross::after {
content: '';
background: #e04681;
height: 2px;
position: relative;
display: block;
transform-origin: 50% 50%;
transform: rotate(0deg) translate(0,0);
transition: .5s all;
}
#cross::before {
top: -5px;
}
#cross::after {
top: 3px;
}
#cross.active {
background: transparent;
}
#cross.active::before {
transform: rotate(45deg) translate(4px, 4px);
}
#cross.active::after {
transform: rotate(135deg) translate(-3px, 3px);
}
<ul id="hitbox">
<li id="cross"></li>
</ul>
// Using jQuery:
$('#hitbox').click(function(){
$('#cross').toggleClass('active');
});
// Using vanilla JS:
const el = e => document.getElementById(e);
// This is just a shorthand func for getElementById
el('hitbox').addEventListener('click', () => {
el('cross').classList.toggle('active');
});
@LukyVj
Copy link

LukyVj commented Feb 12, 2015

Love it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment