Animated card slider with Vue & GSAP - WotW
April 18, 2018
This is the third installment of the **Widget of the Week** series.
Today I’ll show you the process to make a styled card slider from scratch using Vue.
The inspiration for this widget is this and looks like this:
Preparations
Similarly to last widget, today’s widget we will be using vue.js for the interactions, and tweenlite for animations.
The HTML structure
Basically the elements of the slider are the cards and the info container, I’ll start by adding them along with some classes to be able to style them in the next step:
Styling!
Right now it doesn’t look anything close to the final product. First I’ll simulate the mobile viewport with this rule:
For the cards, I’ll be using a margin in the container to center the first card, then the cards will separate each other with a right margin. Also we need the cards container to be relative and have a z-index to be on top of the slider-info
div.
The cards should be inline
so they can be aside of each other, but for that too work, the container should be wide enough. Each card in this case is roughly 300px wide, so the container will be 900px wide because we have 3 cards (in case we had more cards we would need to calculate the total width needed).
Lastly we will add a box shadow to give the impression that the card floats.
We’re getting closer
Now it’s the turn of the slider-info
to get its makeover. We will add a background color, dimensions and margins to centered the info.
It is important that it overlaps with the cards container, in order to do that, the margin-top
will be negative and to compensate we add some padding-top
.
We need to make sure that the overflow
property is hidden to make the button at the bottom have the same rounded corners as the info container. After that is just a matter of styling the title, description and the button in the following way:
Much better.
Filling with data
We are ready to start using Vue, let’s create an instance and also set some data from The Movie DB:
Now to be able to show the data, we need to define the default selected movie. That can be accomplished with another variable in our data called selectedIndex
and a computed property that can give us the data from the slides according to that selected index:
Then in our template we will bind the cards with a v-for
, and the info to the corresponding data:
This looks almost finished, at least aesthetically, but we still need…
The interactions
If we breakdown the interactions for the slider, they are basically 3, when we press down the card, moving the card and letting the card go. To track those actions we will need to bind @mouseDown
, @mouseUp
and @mouseMove
to methods inside the Vue instance. Also to prevent the images to ghost they should have the property draggable=false
.
Now we need to create those methods in the Vue side, also we’ll add a couple of variables inside our data object:
All three methods receive an event (in this case we call it e
) but we will just need it in the startDrag
and mouseMoving
methods.
On the next code snippets I’ll break down the process step by step to fill those 3 methods, so I’ll ignore the rest of the code.
First we need to set dragging
to true or false depending on the mouse actions:
Pretty straightforward, now we want to only be able to move our cards if we are dragging them, so inside the mouseMoving
method we will add this conditional:
Alright now things will get interesting, we need to track what’s the position of both the cards and the mouse when we start dragging, the pageX
property will tell us about the mouse position, and the cardsX
inside our data will be tracking the cards’ container position:
After storing the initial X for cards and mouse, we can deduct the target position of the cards’ container by calculating the mouse position difference when the mouseMoving
method executes like this:
Our component is almost ready to move, we just need to find a way to bind the cards’ container to the cardsX
property, this can be done adding this property to the HTML:
You might ask “Why are you using translate3d instead of just a regular 2d translate?”, the reason is that translate3d
is hardware accelerated, and most of the times has a better performance. You can check by yourself in this site.
The slides are moving now, but there’s one little problem, when we let go they stay wherever we drop them, also the movie info is not changing. What we actually need is for them to find what’s the nearest slide and center it.
To find the nearest slide we just need to divide the current position with the width of the card and round the result. Then with TweenLite we will animate the cards to the corresponding position:
To understand better that formula, this gif shows how the cardsX
value correlates to the nearestSlide
.
And now the final result!
Right now it only works on desktop devices, but that could probably be fixed with vue-touch
, you can learn more about it in this article
That’s it for the 3rd Widget of the Week.
If you haven’t checked the previous one, here it is.