A Pen by Mark Thomes on CodePen.
Created
August 27, 2020 19:03
-
-
Save mthomes/01712449515bf7c1454580abedebcc04 to your computer and use it in GitHub Desktop.
Add to cart
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
<button type="button"> | |
<span>+</span> | |
<span class="countContainer"> | |
<span class="count"></span> | |
</span> | |
</button> |
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 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"); | |
}); |
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
$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