Making an animated slider - WotW
July 12, 2018
Welcome to the Widget of the Week series, where I take gifs or videos of awesome UI/UX components, and bring them to life with code.
This time we are going to create a temperature slider, although it can be used for anything. The inspiration comes from this submission created by ramykhuffash and looks like this:
Preparations
For today’s widget we will be using Vue.js, and for some animations we’ll use TweenMax. Also we will need a temperature icon, so we’ll be using the one from Font Awesome.
If you want to follow along you can fork this codepen template that already has the dependencies.
Matching the design
The HTML markup for this widget got a little bit more complicated than the usual, so this time I will break it down in sections using HTML + CSS until we match the original design.
Let’s start by setting an upper and lower section, the upper one will contain the numbers and the lower one the slider control.
Before styling them we need a couple of main CSS properties in the body
.
We set the margin to 0
to avoid having a gap surrounding our main-container
. The color
and font-family
is set there too to avoid repeating them all over our elements.
Now we will use CSS grid
properties to divide the screen in two parts, the upper one needs to take something like 3/4
of the vertical height, we can achieve that with fr
.
Notice the 100vh
value in the height
property, that allows us to fill the screen vertically even if our divs have no content at all.
Also the overflow-x: hidden
property will prevent our widget to show a scroll bar if our elements get out of bounds in smaller screens (Thanks to Nested Software for noticing this).
Now it is just a matter of adding a background color to the sections. For the upper one we’ll be using a gradient:
The position: relative
property set in the upper-container
will be of use when we tray to position its inner elements.
We’re just getting warmed up.
The numbers inside the upper section seem like the logical next step.
This will be the big number that shows the current temperature, let’s use some CSS to position it better:
The user-select: none
property should helps us to avoid selecting the text when we interact with the slider.
Before we adding the numbers that appear below, let’s fire up the Vue instance with some data to help us avoid repeating unnecessary markup elements:
Now we can use that temperatureGrades
array to show those elements in the design:
Notice that we are rendering for each of the numbers a |
character, now we can style them to look like a “ruler”.
For both the numbers and lines we need to center the text, we will do that inside the temperature-element
rules. We will also make the elements to be inline-blocks
so they can be next to each other. Finally the |
character needs to be smaller, font-size
will take care of that:
Inspecting the .temperature-graduation
element we can see that it’s width is 300px, in order to center it we can use a calculated value in the following way:
We also set the bottom
property to make it appear just above the lower section.
The slider
The upper part is ready, now we will add the slider control. The button is easy, we just need a div with an icon in it:
Now let’s style the button, most of the following CSS code are values “tweaked” by hand to be able to position the elements in the desired position.
The grab
values inside the button will transform the cursor to be a hand when hovering it.
The slider now is only missing a “wave” like shape, at first I tried to do it by using border-radius
values and rotating a div
, but sadly it didn’t match the design. What I ended up doing was a SVG
graphic that looks like this:
The code for that shape is this:
It’s been a bit of a ride, but we have the design ready.
The interaction
The most noticeable thing in the interaction of this widget is, by far, dragging and dropping the slider. We have done this before when we did the card slider, so I’ll follow a similar approach:
These will be the data properties that will help us to keep track when the user starts/stops dragging, mouse and the slider position.
The following methods will initialize those variables when the user interacts:
Now let’s bind them to the template
You might have noticed that the @mouseDown
action is set in the slider button, but the @mouseMove
and @mouseUp
are at the level of the main div.
The reason behind this is that users will start by pressing down the slider button, but when moving the cursor they usually get outside the slider track, and if they let go the mouse outside the button it won’t be tracked and will cause the button to follow you until you click it again.
Let’s now fill the mouseMoving
method with an algorithm that will set the sliderX
property to the desired position. We will need to declare some constrains for the slider to match the ruler we did before.
The computed property sliderStyle
as you might have guessed, stores the position for the slider, we just need to bind it to the .slider-container
:
We almost have a working slider control but it is missing an important thing, tracking the slider value. It might sound complicated, but we can calculate that value with a computed property because we already know the sliderX
position:
You can se that it works by render it inside the .temperature-text
element:
The problem now is that it is rendering float numbers. We can avoid that with a filter:
And now we can use the filter like this:
The finishing touches
We could call it a day and let the widget like this, but it is still missing a couple of details. The background should change colors when the temperature goes above 25 degrees, and also the ruler numbers should move in a wave like manner.
For the background we’ll declare a couple of constants at the top and some new data properties:
They will hold the colors needed for the gradient background. The bgStyle
computed property will generate the background every time gradientStart
and gradientEnd
change. Let’s bind it to it’s corresponding HTML element:
For now it should look the same, but that will change when we add the rules to animate it inside the mouseMoving
method:
What we are doing is change the gradient values from the cold to the hot ones, when the temperature changes to be 25 degrees or more. The transition is done with TweenLite instead of CSS transitions, because they only work with solid colors.
Finally our rulers elements need to change their Y
position if the slider is close to them.
Similarly to the upper section we’ll bind the style to be changed by a method, this method will receive each of the ruler’s values. Now it’s just a matter of doing some math to calculate the distance and generate some CSS transform props:
And now the final result!
That’s it for this Widget of the Week.
If you’re hungry for more you can check other WotW: