I want this type of Animated Progress Button : https://dribbble.com/shots/16005120-Energy-Bar
there will be a heart image of left section of button and right section will empty for progress bar, if we click on the heart image the image should be changed and the prgress bar will load from the right section.
I have tried many times but i cant , Here is my code
document.getElementById('heartButton').addEventListener('click', function() {
const heartIcon = document.getElementById('heartIcon');
const brokenHeartIcon = document.getElementById('brokenHeartIcon');
const progressBar = document.querySelector('.progress-bar');
// Hide the heart image and display the broken heart image
heartIcon.style.display = 'none';
brokenHeartIcon.style.display = 'block';
// Animate progress bar
progressBar.style.width = '100%';
// Reset button after animation
setTimeout(function() {
progressBar.style.width = '0';
// Toggle back to the original state
heartIcon.style.display = 'block';
brokenHeartIcon.style.display = 'none';
}, 1000); // Change the duration as needed
});
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
button {
position: relative;
display: flex;
align-items: center;
background-color: #3498db;
border: none;
padding: 10px 20px;
color: #fff;
font-size: 16px;
cursor: pointer;
overflow: hidden;
}
.left-section {
display: flex;
align-items: center;
}
#heartIcon,
#brokenHeartIcon {
width: 30px;
height: 30px;
margin-right: 10px;
}
#brokenHeartIcon {
display: none;
}
.right-section {
width: 80px;
height: 30px;
background-color: #000;
overflow: hidden;
}
.progress-bar {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 0;
background-color: #e74c3c;
transition: width 1s linear;
/* Use linear transition for smooth animation */
}
<button id="heartButton">
<div class="left-section">
<img src="love.png" alt="Heart Icon" id="heartIcon">
<img src="heartbroken.png" alt="Broken Heart Icon" id="brokenHeartIcon">
</div>
<div class="right-section">
<div class="progress-bar"></div>
</div>
</button>
2
Answers
Looks like
.progress-bar
hasposition: absolute
and the closest parent with relative position is the button element.If you don’t want the progress bar to overlap the whole button, adding
postition: relative
to.right-section
might just do the trickin progress bar change ‘left’ to ‘right’