How is Your Year Loading?

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Let's hope so, I think everybody is about done with the year!
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

This year has been everything but normal; I think we can all agree on that right!
I see so many people struggling even to remember if it's March or August (It's August π€), and I totally feel you!
So, today, let's make a percentage year counter! It will show us how 2020 is loading.
<div class="container">
<h1>2020 Loading</h1>
<h2>We are <i id="percentage">54%</i> there</h2>
<div class="loader" id="loader">
<span></span>
</div>
</div>
We don't need much, we are going to use the I tag to place the percentage in and the loader to show our actual loading bar.
Now on to our CSS, we use some basic flex center for our container.
.container {
display: flex;
height: 100vh;
align-items: center;
justify-content: center;
flex-direction: column;
background: #00bbf9;
}
And this is our loader:
.loader {
margin-top: 25px;
width: 200px;
height: 25px;
background: #efefef;
position: relative;
border-radius: 10px;
overflow: hidden;
}
.loader span {
position: absolute;
height: 100%;
width: 0%;
background: #f15bb5;
left: 0;
top: 0;
transition: width 5s ease-in;
}
As you can see, the div itself is relative, and we position the span absolute, the span will then receive a width and a cool transition so we can see it loading!
Onto our actual JavaScript. To start we need to know how far our day is in the year.
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff =
now - start + (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
Oke, what?
So let's start with now we are defining today as an object!
Then we need to get the first day of the year, which will be Jan 1st.
If we have both, we can calculate the difference between now and our starting date; we have to add a timezone offset in milliseconds since daylight saving time will not work.
Then we define what one day is "worth" in days. This will return one-day millisecond value.
Then we can simply get our difference / one-day and receive 231 on the 18th of August.
So now we can calculate the percentage of a whole year!
var percentage = Math.floor((day / 365) * 100);
Pretty easy our (day / 365 * 100) = percentage.
Then we need to get all our elements and set our variables.
var loader = document.getElementById('loader');
var loaderBar = loader.querySelector('span');
var percentageText = document.getElementById('percentage');
percentageText.innerHTML = percentage + '%';
setTimeout(function() {
loaderBar.style.width = percentage + '%';
}, 100);
We set our percentage text and our loader bar width to be our actual percentage.
That's it, see it in action on this Codepen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter