Skip to content

Instantly share code, notes, and snippets.

@mthomes
Created August 27, 2020 19:03
Show Gist options
  • Save mthomes/01712449515bf7c1454580abedebcc04 to your computer and use it in GitHub Desktop.
Save mthomes/01712449515bf7c1454580abedebcc04 to your computer and use it in GitHub Desktop.
Add to cart
<button type="button">
<span>+</span>
<span class="countContainer">
<span class="count"></span>
</span>
</button>
const button = document.querySelector("button");
const count = button.querySelector(".count");
button.addEventListener("click", () => {
if (button.classList.contains("isActive")) {
count.innerHTML = "";
} else {
count.innerHTML = "1";
}
button.classList.toggle("isActive");
});
$duration: 500ms;
body {
display: flex;
flex-direction: column;
min-height: 100vh;
align-items: center;
justify-content: center;
font-family: "Montserrat", sans-serif;
}
button {
font-family: "Montserrat", sans-serif;
box-sizing: border-box;
appearance: none;
border: 4px solid red;
border-radius: 30px;
width: 60px;
height: 60px;
font-size: 40px;
background-color: white;
color: red;
position: relative;
cursor: default;
transition: background-color ($duration / 4) linear;
&:focus {
outline: none;
}
&.isActive {
background-color: red;
animation: addToCart $duration linear forwards;
}
&::after {
content: "";
position: absolute;
top: -4px;
right: -4px;
bottom: -4px;
left: -4px;
border: 4px solid red;
border-radius: 30px;
}
}
.countContainer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
align-items: center;
justify-content: center;
color: white;
transform: translateY(100%);
transition: transform ($duration / 3) linear;
}
.isActive .countContainer {
transform: translateY(0);
}
@keyframes addToCart {
0% {
transform: translateY(0);
}
33.333% {
transform: translateY(5px);
animation-timing-function: cubic-bezier(0.5, 1, 0.89, 1);
}
66.666% {
transform: translateY(-15px);
animation-timing-function: cubic-bezier(0.11, 0, 0.5, 0);
}
100% {
transform: translateY(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment