DEV Community

Cover image for Building a carousel from scratch using Vue.js

Building a carousel from scratch using Vue.js

- on July 19, 2021

Instead of going through a complex third-party library docs, I tried to figure out how to build a "multi-card" carousel from scratch. For the fi...
Collapse
 
efraimla profile image
Efraim • Edited

Hellouu i'm trying to center the cards modifying the step value but when i'm scrolling it gets lagged, idk why

Image description

Here is the example
imgur.com/a/qbrGe7V

Collapse
 
luvejo profile image
- • Edited

It could be useful for you to re-enable the reset transition by commenting this line:

function resetTranslate() {
  innerStyles.value = {
    // transition: 'none',
    transform: `translateX(-${step.value})`
  }
}
Enter fullscreen mode Exit fullscreen mode

Also, increase the transition duration:

.inner {
  transition: transform 2s;
  /* ... */
}
Enter fullscreen mode Exit fullscreen mode

That way you can see what happens after you tweak translation values.

Collapse
 
azilyahia profile image
Yahia

Thank you so much!

Collapse
 
jpaez profile image
Jorge Páez Minaya

Many thanks for the tutorial Luis, it was really useful. Just wondering what would be your aproach when you have cards with different widths.

I'm basically trying to do a chip group with Vue 3:

vuetifyjs.com/en/components/chip-g...

While testing your approach everything works fine but the two bugs that I find is that the first card is allways cut-off from the container, and the transitions are kinda weird.

Will really appreciate your feedback, and thanks in advance !

Collapse
 
luvejo profile image
-

Hey, Jorge 👋 I'm pleased to read it was useful to you.

If the order of the cards is important to your project, I think you can initially shift the array so the first element becomes the second one.

Regarding the animations, did you tried using different CSS transition properties? In production I would probably try something slower. Or what do you mean by "the transitions are kinda weird"?

Finally, you would probably need both the width of the hidden card and the with of the one next to it to calculate the .inner translation. Not sure. Did you solved it yet?

Collapse
 
jpaez profile image
Jorge Páez Minaya • Edited

Thanks a lot for your reply. So I downloaded your code and tried it and the main problem is with the static width of the cards. If you don't have a static with on every card you will see that the transitions when you go "prev" or "next" looks strange. The carousel will make the animation and right after that it will add the next element to the inner container visible pushing the others. I cam to that conclusion because I just change your cards to a variable width and have exactly the same problem with the carousel I'm trying to code.

Also, I noticed when testing your code that as soon as you load the component the first card that you see in the carousel is the number "2" card not the number "1" is this correct ?

Really hope you can give me a hand here since I find your explanation super useful but just want it to make it work with that variable width approach.

Can you develop a little bit more about "you would probably need both the width of the hidden card and the with of the one next to it to calculate the .inner translation".

Really appreciated Luis !

Collapse
 
it_will_pass profile image
wide awake

Hi, thank you so much for elaborating step by step! However, when the quantity of the carousel items is less than the number of items that are showing -- e.g. mine is showing 3 per view, but has 4 in quantity) -- whenever the carousel is looping, the last item is shown like appearing instantly (not sliding), is this expected?

And when the quantity is 3 (and should showing 3 per view), it always take the last item shown and put it at the start so there's a 1 item gap. So it's only showing 2 (when it's should show 3 but it shows 2 with 1 gap at the end), like the entire carousel is translatedX(-${this.step})

do you have any suggesting how to tackle this? I feel like it's given because of the small quantity of total items compares to quantity of items shown. cmiiw

Collapse
 
jamols09 profile image
jamols09

This is the most descriptive carousel tutorial I've ever read!

Collapse
 
lisabee224 profile image
Lisa Buch

This was so helpful! However, I am getting some flickering (on images only) right after resetTranslate fires -- any ideas on how to fix this?

Collapse
 
rafalbochniewic profile image
Rafał Bochniewicz • Edited

I had the same flickering problem in Nuxt3 with . So I replaced nuxt-img with backgroundImage in styles and it's fine. But I haven't really checked what's causing it.
You can also try to add css to image:
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;

Collapse
 
luvejo profile image
-

That's strange. Did you see the flickering happening in the demo?