Building a Horizontal Timeline With CSS and JavaScript

In a previous post, I showed you how to build a responsive vertical timeline from scratch. Today, I’ll cover the process of creating the associated horizontal timeline.

As usual, to get an initial idea of what we’ll be building, take a look at the related CodePen demo (check out the larger version for a better experience):

We have a lot to cover, so let’s get started!

1. HTML Markup

The markup is identical to the markup we defined for the vertical timeline, apart from three small things:

  • We use an ordered list instead of an unordered list as that’s more semantically correct.
  • There’s an extra list item (the last one) which is empty. In an upcoming section, we’ll discuss the reason.
  • There’s an extra element (i.e. .arrows) which is responsible for the timeline navigation.

Here’s the required markup:

The initial state of the timeline looks like this:

2. Adding Initial CSS Styles

After some basic font styles, color styles, etc. which I’ve omitted here for the sake of simplicity, we specify some structural CSS rules:

Most importantly here, you’ll notice two things:

  • We assign large top and bottom paddings to the list. Again, we’ll explain why that happens in the next section. 
  • As you’ll notice in the following demo, at this point we cannot see all the list items because the list has width: 100vw and its parent has overflow-x: hidden. This effectively “masks” the list items. Thanks to the timeline navigation, however, we’ll be able to navigate through the items later.

With these rules in place, here’s the current state of the timeline (without any actual content, to keep things clear):

3. Timeline Element Styles

At this point we’ll style the div elements (we’ll call them “timeline elements” from now on) which are part of the list items as well as their ::before pseudo-elements.

Additionally, we’ll use the :nth-child(odd) and :nth-child(even) CSS pseudo-classes to differentiate the styles for the odd and even divs.

Here are the common styles for the timeline elements:

Then some styles for the odd ones:

And finally some styles for the even ones:

Here’s the new state of the timeline, with content added again:

As you’ve probably noticed, the timeline elements are absolutely positioned. That means they are removed from the normal document flow. With that in mind, in order to ensure that the whole timeline appears, we have to set large top and bottom padding values for the list. If we don’t apply any paddings, the timeline will be cropped:

How the timeline looks like without paddingsHow the timeline looks like without paddingsHow the timeline looks like without paddings

4. Timeline Navigation Styles

It’s now time to style the navigation buttons. Remember that by default we disable the previous arrow and give it the class of disabled.

Here are the associated CSS styles:

The rules above give us this timeline:

5. Adding Interactivity

The basic structure of the timeline is ready. Let’s add some interactivity to it!

Variables

First things first, we set up a bunch of variables which we’ll use later. 

Initializing Things

When all page assets are ready, the init function is called.

This function triggers four sub-functions:

As we’ll see in a moment, each of these functions accomplishes a certain task.

Equal-Height Timeline Elements

If you jump back to the last demo, you’ll notice that the timeline elements don’t have equal heights. This doesn’t affect the main functionality of our timeline, but you might prefer it if all elements had the same height. To achieve this, we can give them either a fixed height via CSS (easy solution) or a dynamic height which corresponds to the height of the tallest element via JavaScript.

The second option is more flexible and stable, so here’s a function that implements this behavior:

This function retrieves the height of the tallest timeline element and sets it as the default height for all the elements.

Here’s how the demo’s looking:

6. Animating the Timeline

Now let’s focus on the timeline animation. We’ll build the function that implements this behavior step-by-step.

First, we register a click event listener for the timeline buttons:

Each time a button is clicked, we check the disabled state of the timeline buttons and if they aren’t disabled, we disable them. This ensures that both buttons will be clicked only once until the animation finishes.

So, in terms of code, the click handler initially contains these lines:

The next steps are as follows:

  • We check to see if it’s the first time we’ve clicked on a button. Again, keep in mind that the previous button is disabled by default, so the only button that can be clicked initially is the next one.
  • If indeed it’s the first time, we use the transform property to move the timeline 280px to the right. The value of the xScrolling variable determines the amount of movement. 
  • On the contrary, if we’ve already clicked on a button, we retrieve the current transform value of the timeline and add or remove to that value, the desired amount of movement (i.e. 280px). So, as long as we click on the previous button, the value of the transform property decreases and the timeline is moved from left to right. However, when the next button is clicked, the value of the transform property increases and the timeline is moved from right to left.

The code that implements this functionality is as follows:

Great job! We’ve just defined a way of animating the timeline. The next challenge is to figure out when this animation should stop. Here’s our approach:

  • When the first timeline element becomes fully visible, it means that we’ve already reached the beginning of the timeline, and thus we disable the previous button. We also ensure that the next button is enabled.
  • When the last element becomes fully visible, it means that we’ve already reached the end of the timeline, and thus we disable the next button. We also, therefore, ensure that the previous button is enabled.

Remember that the last element is an empty one with width equal to the width of the timeline elements (i.e. 280px). We give it this value (or a higher one) because we want to make sure that the last timeline element will be visible before disabling the next button.

To detect whether the target elements are fully visible in the current viewport or not, we’ll take advantage of the same code we used for the vertical timeline. The required code which comes from this Stack Overflow thread is as follows:

Beyond the function above, we define another helper:

This function adds or removes the disabled class from an element based on the value of the flag parameter. In addition, it can change the disabled state for this element.

Given what we’ve described above, here’s the code we define for checking whether the animation should stop or not:

Notice that there’s a 1.1 second delay before executing this code. Why does this happen?

If we go back to our CSS, we’ll see this rule:

So, the timeline animation needs 1 second to complete. As long as it completes, we wait for 100 milliseconds and then, we perform our checks.

Here’s the timeline with animations:

7. Adding Swipe Support

So far, the timeline doesn’t respond to touch events. It would be nice if we could add this functionality though. To accomplish it, we can write our own JavaScript implementation or use one of the related libraries (e.g. Hammer.js, TouchSwipe.js) that exist out there.

For our demo, we’ll keep this simple and use Hammer.js, so first, we include this library in our pen:

How to include Hammerjs in our penHow to include Hammerjs in our penHow to include Hammerjs in our pen

Then we declare the associated function:

Inside the function above, we do the following:

  • Create an instance of Hammer. 
  • Register handlers for the swipeleft and swiperight events. 
  • When we swipe over the timeline in the left direction, we trigger a click to the next button, and thus the timeline is animated from right to left.
  • When we swipe over the timeline in the right direction, we trigger a click to the previous button, and thus the timeline is animated from left to right.

The timeline with swipe support:

Adding Keyboard Navigation

Let’s further enhance the user experience by providing support for keyboard navigation. Our goals:

  • When the left or right arrow key is pressed, the document should be scrolled to the top position of the timeline (if another page section is currently visible). This ensures that the whole timeline will be visible.
  • Specifically, when the left arrow key is pressed, the timeline should be animated from left to right.
  • In the same way, when the right arrow key is pressed, the timeline should be animated from right to left.

The associated function is the following:

The timeline with keyboard support:

8. Going Responsive

We’re almost done! Last but not least, let’s make the timeline responsive. When the viewport is less than 600px, it should have the following stacked layout:

As we’re using a desktop-first approach, here are the CSS rules that we have to overwrite:

Note: For two of the rules above, we had to use the !important rule to override the related inline styles applied through JavaScript. 

The final state of our timeline:

Browser Support

The demo works well in all recent browsers and devices. Also, as you’ve possibly noticed, we use Babel to compile our ES6 code down to ES5.

The only small issue I encountered while testing it is the text rendering change that happens when the timeline is being animated. Although I tried various approaches proposed in different Stack Overflow threads, I didn’t find a straightforward solution for all operating systems and browsers. So, keep in your mind that you might see small font rendering issues as the timeline is being animated.

Conclusion

In this fairly substantial tutorial, we started with a simple ordered list and created a responsive horizontal timeline. Without doubt, we covered a lot of interesting things, but I hope you enjoyed working towards the final result and that it’s helped you gain some new knowledge.

If you have any questions or if there’s anything you didn’t understand, let me know in the comments below!

Next Steps

If you want to further improve or extend this timeline, here are a few things you can do:

  • Add support for dragging. Instead of clicking the timeline buttons for navigating, we could just drag the timeline area. For this behavior, you could use either the native Drag and Drop API (which unfortunately doesn’t support mobile devices at the time of writing) or an external library like Draggable.js.
  • Improve the timeline behavior as we resize the browser window. For instance, as we resize the window, the buttons should be enabled and disabled accordingly.
  • Organize the code in a more manageable way. Perhaps, use a common JavaScript Design Pattern.


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos

In a previous post, I showed you how to build a responsive vertical timeline from scratch. Today, I’ll cover the process of creating the associated horizontal timeline.

As usual, to get an initial idea of what we’ll be building, take a look at the related CodePen demo (check out the larger version for a better experience):

We have a lot to cover, so let’s get started!

1. HTML Markup

The markup is identical to the markup we defined for the vertical timeline, apart from three small things:

  • We use an ordered list instead of an unordered list as that’s more semantically correct.
  • There’s an extra list item (the last one) which is empty. In an upcoming section, we’ll discuss the reason.
  • There’s an extra element (i.e. .arrows) which is responsible for the timeline navigation.

Here’s the required markup:

The initial state of the timeline looks like this:

2. Adding Initial CSS Styles

After some basic font styles, color styles, etc. which I’ve omitted here for the sake of simplicity, we specify some structural CSS rules:

Most importantly here, you’ll notice two things:

  • We assign large top and bottom paddings to the list. Again, we’ll explain why that happens in the next section. 
  • As you’ll notice in the following demo, at this point we cannot see all the list items because the list has width: 100vw and its parent has overflow-x: hidden. This effectively “masks” the list items. Thanks to the timeline navigation, however, we’ll be able to navigate through the items later.

With these rules in place, here’s the current state of the timeline (without any actual content, to keep things clear):

3. Timeline Element Styles

At this point we’ll style the div elements (we’ll call them “timeline elements” from now on) which are part of the list items as well as their ::before pseudo-elements.

Additionally, we’ll use the :nth-child(odd) and :nth-child(even) CSS pseudo-classes to differentiate the styles for the odd and even divs.

Here are the common styles for the timeline elements:

Then some styles for the odd ones:

And finally some styles for the even ones:

Here’s the new state of the timeline, with content added again:

As you’ve probably noticed, the timeline elements are absolutely positioned. That means they are removed from the normal document flow. With that in mind, in order to ensure that the whole timeline appears, we have to set large top and bottom padding values for the list. If we don’t apply any paddings, the timeline will be cropped:

How the timeline looks like without paddingsHow the timeline looks like without paddingsHow the timeline looks like without paddings

4. Timeline Navigation Styles

It’s now time to style the navigation buttons. Remember that by default we disable the previous arrow and give it the class of disabled.

Here are the associated CSS styles:

The rules above give us this timeline:

5. Adding Interactivity

The basic structure of the timeline is ready. Let’s add some interactivity to it!

Variables

First things first, we set up a bunch of variables which we’ll use later. 

Initializing Things

When all page assets are ready, the init function is called.

This function triggers four sub-functions:

As we’ll see in a moment, each of these functions accomplishes a certain task.

Equal-Height Timeline Elements

If you jump back to the last demo, you’ll notice that the timeline elements don’t have equal heights. This doesn’t affect the main functionality of our timeline, but you might prefer it if all elements had the same height. To achieve this, we can give them either a fixed height via CSS (easy solution) or a dynamic height which corresponds to the height of the tallest element via JavaScript.

The second option is more flexible and stable, so here’s a function that implements this behavior:

This function retrieves the height of the tallest timeline element and sets it as the default height for all the elements.

Here’s how the demo’s looking:

6. Animating the Timeline

Now let’s focus on the timeline animation. We’ll build the function that implements this behavior step-by-step.

First, we register a click event listener for the timeline buttons:

Each time a button is clicked, we check the disabled state of the timeline buttons and if they aren’t disabled, we disable them. This ensures that both buttons will be clicked only once until the animation finishes.

So, in terms of code, the click handler initially contains these lines:

The next steps are as follows:

  • We check to see if it’s the first time we’ve clicked on a button. Again, keep in mind that the previous button is disabled by default, so the only button that can be clicked initially is the next one.
  • If indeed it’s the first time, we use the transform property to move the timeline 280px to the right. The value of the xScrolling variable determines the amount of movement. 
  • On the contrary, if we’ve already clicked on a button, we retrieve the current transform value of the timeline and add or remove to that value, the desired amount of movement (i.e. 280px). So, as long as we click on the previous button, the value of the transform property decreases and the timeline is moved from left to right. However, when the next button is clicked, the value of the transform property increases and the timeline is moved from right to left.

The code that implements this functionality is as follows:

Great job! We’ve just defined a way of animating the timeline. The next challenge is to figure out when this animation should stop. Here’s our approach:

  • When the first timeline element becomes fully visible, it means that we’ve already reached the beginning of the timeline, and thus we disable the previous button. We also ensure that the next button is enabled.
  • When the last element becomes fully visible, it means that we’ve already reached the end of the timeline, and thus we disable the next button. We also, therefore, ensure that the previous button is enabled.

Remember that the last element is an empty one with width equal to the width of the timeline elements (i.e. 280px). We give it this value (or a higher one) because we want to make sure that the last timeline element will be visible before disabling the next button.

To detect whether the target elements are fully visible in the current viewport or not, we’ll take advantage of the same code we used for the vertical timeline. The required code which comes from this Stack Overflow thread is as follows:

Beyond the function above, we define another helper:

This function adds or removes the disabled class from an element based on the value of the flag parameter. In addition, it can change the disabled state for this element.

Given what we’ve described above, here’s the code we define for checking whether the animation should stop or not:

Notice that there’s a 1.1 second delay before executing this code. Why does this happen?

If we go back to our CSS, we’ll see this rule:

So, the timeline animation needs 1 second to complete. As long as it completes, we wait for 100 milliseconds and then, we perform our checks.

Here’s the timeline with animations:

7. Adding Swipe Support

So far, the timeline doesn’t respond to touch events. It would be nice if we could add this functionality though. To accomplish it, we can write our own JavaScript implementation or use one of the related libraries (e.g. Hammer.js, TouchSwipe.js) that exist out there.

For our demo, we’ll keep this simple and use Hammer.js, so first, we include this library in our pen:

How to include Hammerjs in our penHow to include Hammerjs in our penHow to include Hammerjs in our pen

Then we declare the associated function:

Inside the function above, we do the following:

  • Create an instance of Hammer. 
  • Register handlers for the swipeleft and swiperight events. 
  • When we swipe over the timeline in the left direction, we trigger a click to the next button, and thus the timeline is animated from right to left.
  • When we swipe over the timeline in the right direction, we trigger a click to the previous button, and thus the timeline is animated from left to right.

The timeline with swipe support:

Adding Keyboard Navigation

Let’s further enhance the user experience by providing support for keyboard navigation. Our goals:

  • When the left or right arrow key is pressed, the document should be scrolled to the top position of the timeline (if another page section is currently visible). This ensures that the whole timeline will be visible.
  • Specifically, when the left arrow key is pressed, the timeline should be animated from left to right.
  • In the same way, when the right arrow key is pressed, the timeline should be animated from right to left.

The associated function is the following:

The timeline with keyboard support:

8. Going Responsive

We’re almost done! Last but not least, let’s make the timeline responsive. When the viewport is less than 600px, it should have the following stacked layout:

As we’re using a desktop-first approach, here are the CSS rules that we have to overwrite:

Note: For two of the rules above, we had to use the !important rule to override the related inline styles applied through JavaScript. 

The final state of our timeline:

Browser Support

The demo works well in all recent browsers and devices. Also, as you’ve possibly noticed, we use Babel to compile our ES6 code down to ES5.

The only small issue I encountered while testing it is the text rendering change that happens when the timeline is being animated. Although I tried various approaches proposed in different Stack Overflow threads, I didn’t find a straightforward solution for all operating systems and browsers. So, keep in your mind that you might see small font rendering issues as the timeline is being animated.

Conclusion

In this fairly substantial tutorial, we started with a simple ordered list and created a responsive horizontal timeline. Without doubt, we covered a lot of interesting things, but I hope you enjoyed working towards the final result and that it’s helped you gain some new knowledge.

If you have any questions or if there’s anything you didn’t understand, let me know in the comments below!

Next Steps

If you want to further improve or extend this timeline, here are a few things you can do:

  • Add support for dragging. Instead of clicking the timeline buttons for navigating, we could just drag the timeline area. For this behavior, you could use either the native Drag and Drop API (which unfortunately doesn’t support mobile devices at the time of writing) or an external library like Draggable.js.
  • Improve the timeline behavior as we resize the browser window. For instance, as we resize the window, the buttons should be enabled and disabled accordingly.
  • Organize the code in a more manageable way. Perhaps, use a common JavaScript Design Pattern.


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos


Print Share Comment Cite Upload Translate Updates
APA

George Martsoukos | Sciencx (2017-03-04T08:12:13+00:00) Building a Horizontal Timeline With CSS and JavaScript. Retrieved from https://www.scien.cx/2017/03/04/building-a-horizontal-timeline-with-css-and-javascript/

MLA
" » Building a Horizontal Timeline With CSS and JavaScript." George Martsoukos | Sciencx - Saturday March 4, 2017, https://www.scien.cx/2017/03/04/building-a-horizontal-timeline-with-css-and-javascript/
HARVARD
George Martsoukos | Sciencx Saturday March 4, 2017 » Building a Horizontal Timeline With CSS and JavaScript., viewed ,<https://www.scien.cx/2017/03/04/building-a-horizontal-timeline-with-css-and-javascript/>
VANCOUVER
George Martsoukos | Sciencx - » Building a Horizontal Timeline With CSS and JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/03/04/building-a-horizontal-timeline-with-css-and-javascript/
CHICAGO
" » Building a Horizontal Timeline With CSS and JavaScript." George Martsoukos | Sciencx - Accessed . https://www.scien.cx/2017/03/04/building-a-horizontal-timeline-with-css-and-javascript/
IEEE
" » Building a Horizontal Timeline With CSS and JavaScript." George Martsoukos | Sciencx [Online]. Available: https://www.scien.cx/2017/03/04/building-a-horizontal-timeline-with-css-and-javascript/. [Accessed: ]
rf:citation
» Building a Horizontal Timeline With CSS and JavaScript | George Martsoukos | Sciencx | https://www.scien.cx/2017/03/04/building-a-horizontal-timeline-with-css-and-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.