Hello Coders! Welcome to another JavaScript Blog. In this, we're going to see how to create a responsive Vertical Progress Indicator using Javascript. You might have seen animation like this in many websites where you have to complete a course or the steps that you have completed. This is a better user-friendly approach to show users how much they completed and how much are yet to be finished.
Here's a preview -
That being said, let us get started.
Step - 1: Like always, create 3 files - index.html, style.css and script.js.
Step - 2: Copy the below HTML code and paste it into your code editor.
HTML
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Responsive Vertical Progress Indicator</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="outer">
<div class="progress">
<div class="left">
<div>Start</div>
<div></div>
<div></div>
<div class="current"></div>
<div>Framework</div>
<div></div>
<div></div>
</div>
<div class="right">
<div>Intro</div>
<div>HTML</div>
<div>CSS</div>
<div class="current">Javascript</div>
<div>React</div>
<div>Node</div>
<div class="done">Done</div>
</div>
</div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script><script src="./script.js"></script>
</body>
</html>
Step - 3: Below is the CSS code for styling.
CSS
@import url("https://fonts.googleapis.com/css?family=Lato");
body {
margin: 0;
padding: 0;
font-family: "Lato", Arial, sans-serif;
display: flex;
}
.outer {
height: 90vh;
min-width: 20vw;
flex: 1;
}
.progress {
display: inline-flex;
height: 100%;
padding: 5vh 10%;
}
.progress > div {
display: flex;
flex-direction: column;
color: #333;
}
.progress > div.left {
padding-right: 20px;
text-align: right;
}
.progress > div.left div:last-of-type:after {
display: none;
}
.progress > div.left div:after {
content: "";
background: rgba(51, 51, 51, 0.1);
border-radius: 2px;
position: absolute;
right: -20px;
top: 10px;
height: 101%;
width: 1px;
transform: translateX(50%);
}
.progress > div.right {
padding-left: 20px;
}
.progress > div.right div.prev:after {
transition: none;
}
.progress > div.right div.current {
color: #333;
font-weight: bold;
}
.progress > div.right div.current:before {
background: #333;
padding: 10px;
transition: all 0.2s 0.15s cubic-bezier(0.175, 0.885, 0.32, 2);
}
.progress > div.right div.current:after {
height: 0%;
transition: height 0.2s ease-out;
}
.progress > div.right div.current ~ div {
color: #666;
}
.progress > div.right div.current ~ div:before {
background: #666;
padding: 2.5px;
}
.progress > div.right div.current ~ div:after {
height: 0%;
transition: none;
}
.progress > div.right div:before {
content: "";
background: #333;
padding: 5px;
border-radius: 50%;
position: absolute;
left: -20px;
top: 10px;
transform: translateX(-50%) translateY(-50%);
transition: padding 0.2s ease;
}
.progress > div.right div:after {
content: "";
background: #333;
border-radius: 2px;
position: absolute;
left: -20px;
top: 10px;
height: 101%;
width: 2px;
transform: translateX(-50%);
transition: height 0.2s ease;
}
.progress > div div {
flex: 1;
position: relative;
line-height: 20px;
cursor: default;
min-height: 30px;
}
.progress > div div:last-of-type {
flex: 0;
}
.done.current {
color: #62af0b !important;
}
.done.current:before {
background: #62af0b !important;
}
Step - 4: Below is the JavaScript code which is the most important part in this animation.
JS
"use strict";
$('.progress').each((_, progress) => {
const steps = $('> div.right > div', progress);
steps.each((i, el) => $(el).mouseenter(e => onHover(el)));
const onHover = (el) => {
steps.removeClass(['current', 'prev']);
el.classList.add('current');
$(el).prevAll().slice(1).addClass('prev');
};
});
And that's it. You're done.
Let me know in the comments if you have any doubt related to this.
Follow @creocodigo for more projects and web related content.
If you find this useful, below are some other posts that I am sure you'll love.