Animated Heart Like Button with HTML, CSS & JavaScript
01/09/2021
Contents
Demo
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Animated Heart Like Button</title>
<link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
<link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<div class="heart-like-button"></div>
<h1>Animated<br>Heart Like Button</h1>
</div>
</body>
</html>
CSS
@charset "utf-8";
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
}
body {
background-color: #fae5e5;
font-family: futura-pt, sans-serif;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
padding: 30px 15px;
}
.heart-like-button {
position: relative;
width: 160px;
height: 130px;
}
.heart-like-button:before {
position: absolute;
top: 0;
left: 80px;
transform: rotate(-45deg);
transform-origin: 0 100%;
width: 80px;
height: 125px;
border-radius: 40px 40px 0 0;
background-color: #574136;
content: "";
cursor: pointer;
transition: background .4s;
}
.heart-like-button:after {
position: absolute;
top: 0;
left: 0;
transform: rotate(45deg);
transform-origin :100% 100%;
width: 80px;
height: 125px;
border-radius: 40px 40px 0 0;
background-color: #574136;
content: "";
cursor: pointer;
transition: background .4s;
}
h1 {
margin: 0;
color: #574136;
font-size: 3rem;
letter-spacing: 1px;
text-align: center;
}
.heart-like-button.liked::before,
.heart-like-button.liked::after {
background-color: #d65076;
}
.heart-like-button.liked {
animation: liked .4s ease;
}
@keyframes liked {
0% {
transform: scale(.8);
}
50% {
transform: scale(1.1);
}
100% {
transform: scale(1);
}
}
@media screen and (max-width: 480px) {
#container {
height: auto;
}
h1 {
font-size: 2.5rem;
}
}
JavaScript
const button = document.querySelector(".heart-like-button");
button.addEventListener("click", () => {
if (button.classList.contains("liked")) {
button.classList.remove("liked");
} else {
button.classList.add("liked");
}
});