Hello Guys welcome to the new blog where we are going to create a Cool Animated Favorite Button using HTML, CSS, JavaScript with free source code. In this blog you will get a download button of the whole Source code file at bottom of page. You can also follow me on Instagram (https://instagram.com/coding.ayush) and Join Our Telegram Channel (https://t.me/codingayush). Enjoy the code !!
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Favorite Button - CodingAyush</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Inter:400,500,600,700&display=swap'> <link rel="stylesheet" href="./style.css"> </head> <body> <button class="favorite-button"> <div class="icon"> <div class="star"></div> </div> <span>Favorite</span> </button> <script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/3.4.0/gsap.min.js'></script> <script src="./script.js"></script> </body> </html>
CSS
html { box-sizing: border-box; -webkit-font-smoothing: antialiased; } * { box-sizing: inherit; } *:before, *:after { box-sizing: inherit; } body { min-height: 100vh; display: flex; font-family: "Inter", Arial; justify-content: center; align-items: center; background: #11131e; } .favorite-button { --background-default: #313440; --text-color-default: #FAFBFF; --star-color-default: #62677C; --star-face-color-default: #1F2128; --star-color-active: #F6C206; --star-face-color-active: #845901; --star-hole: #16181E; --star-hole-inner: #20232C; --button-y: 0px; --star-y: 0px; --star-scale: 1; --star-rotate: 0deg; --star-hole-scale: 0; --star-face-scale: 1; --text-x: 0px; --text-o: 1; -webkit-tap-highlight-color: transparent; -webkit-appearance: none; outline: none; border: none; background: none; min-width: 125px; padding: 12px 24px 12px 16px; margin: 0; font-family: inherit; font-size: 14px; font-weight: 500; line-height: 19px; display: flex; align-items: center; cursor: pointer; position: relative; color: var(--text-color-default); transform: translateY(var(--button-y)) translateZ(0); } .favorite-button:before { content: ""; position: absolute; left: 0; top: 0; right: 0; bottom: 0; border-radius: 9px; transition: transform 0.2s; transform: scale(var(--background-scale-x, 1), var(--background-scale-y, 1)) translateZ(0); background: var(--background-default); } .favorite-button:active { --background-scale-x: .98; --background-scale-y: .96; } .favorite-button span { display: block; opacity: var(--text-o); transform: translateX(var(--text-x)); } .favorite-button .icon { width: 36px; height: 119px; display: flex; pointer-events: none; position: relative; -webkit-clip-path: ellipse(150% 50% at 50% 50%); clip-path: ellipse(150% 50% at 50% 50%); margin: -100px 2px 0 -8px; } .favorite-button .icon:before { content: ""; margin-top: auto; display: block; width: 36px; height: 12px; background: var(--star-hole); box-shadow: inset 0 3px 0 0 var(--star-hole-inner); border-radius: 100px/30px; transform: scale(var(--star-hole-scale)); transform-origin: 50% 100%; } .favorite-button .icon .star { width: 20px; height: 19px; position: absolute; left: 8px; bottom: 0; transform: translateY(var(--star-y)) rotate(var(--star-rotate)) scale(var(--star-scale)); border-radius: var(--star-radius, 0px); background: var(--star-color, var(--star-color-default)); -webkit-clip-path: var(--star-clip, polygon(10px 0, 13px 6px, 20px 7px, 15px 12px, 16px 19px, 10px 15px, 4px 19px, 5px 12px, 0 7px, 7px 6px)); clip-path: var(--star-clip, polygon(10px 0, 13px 6px, 20px 7px, 15px 12px, 16px 19px, 10px 15px, 4px 19px, 5px 12px, 0 7px, 7px 6px)); transition: border-radius 0.2s, background 0.2s, -webkit-clip-path 0.2s; transition: clip-path 0.2s, border-radius 0.2s, background 0.2s; transition: clip-path 0.2s, border-radius 0.2s, background 0.2s, -webkit-clip-path 0.2s; } .favorite-button .icon .star:before, .favorite-button .icon .star:after { content: ""; position: absolute; background: var(--star-face-color, var(--star-face-color-default)); transition: background 0.2s, box-shadow 0.2s; } .favorite-button .icon .star:before { width: 2px; height: 2px; border-radius: 50%; left: 7px; top: 8px; box-shadow: 4px 0 0 0 var(--star-face-color, var(--star-face-color-default)); transform: scaleY(var(--star-face-scale)); } .favorite-button .icon .star:after { width: 4px; height: 2px; border-radius: var(--star-face-radius, 2px 2px 0 0); left: 8px; top: 11px; transition: border-radius 0.2s; } .favorite-button.star-round { --star-clip: polygon(10px 0, 20px 0, 20px 7px, 20px 12px, 20px 19px, 10px 19px, 0 19px, 0 12px, 0 7px, 0 0); --star-radius: 50%; } .favorite-button.active { --star-color: var(--star-color-active); --star-face-color: var(--star-face-color-active); --star-face-radius: 0 0 2px 2px; }
JS
document.querySelectorAll('.favorite-button').forEach(button => { button.addEventListener('click', e => { e.preventDefault() if(button.classList.contains('animated')) { return } button.classList.add('animated') gsap.to(button, { keyframes: [{ '--star-y': '-36px', duration: .3, ease: 'power2.out' }, { '--star-y': '48px', '--star-scale': .4, duration: .325, onStart() { button.classList.add('star-round') } }, { '--star-y': '-64px', '--star-scale': 1, duration: .45, ease: 'power2.out', onStart() { button.classList.toggle('active') setTimeout(() => button.classList.remove('star-round'), 100) } }, { '--star-y': '0px', duration: .45, ease: 'power2.in' }, { '--button-y': '3px', duration: .11 }, { '--button-y': '0px', '--star-face-scale': .65, duration: .125 }, { '--star-face-scale': 1, duration: .15 }], clearProps: true, onComplete() { button.classList.remove('animated') } }) gsap.to(button, { keyframes: [{ '--star-hole-scale': .8, duration: .5, ease: 'elastic.out(1, .75)' }, { '--star-hole-scale': 0, duration: .2, delay: .2 }] }) gsap.to(button, { '--star-rotate': '360deg', duration: 1.55, clearProps: true }) }) })