0
%
1
0
0

### CSS Infinite and Circular Rotating Image Slider ## CSS Infinite and Circular Rotating Image Slider

Through a little series of articles, we are going to explore some fancy and uncommon CSS-only sliders. If you are of tired seeing the same ol’ classic sliders, then you are in the right place!

For this first article, we will start with something I call the “circular rotating image slider”:

Cool right? let’s dissect the code!

The same rule applies here — our code is nothing but a list of images in a container.

Let’s say we’re working with four images:

That’s it! Now let’s move to the interesting part of the code. But first, we’re going to dive into this to understand the logic of how our slider works.

Here is a video where I remove `overflow: hidden` from the CSS so we can better understand how the images are moving:

It’s like our four images are placed on a large circle that rotates counter-clockwise.

All the images have the same size (denoted by `S` in the figure). Note the blue circle which is the circle that intersects with the center of all the images and has a radius (`R`). We will need this value later for our animation. `R` is equal to `0.707 * S`. (I’m going to skip the geometry that gives us that equation.)

Nothing too complex so far. The tricky part is the animation.

We talked about rotating a big circle, but in reality, we will rotate each image individually creating the illusion of a big rotating circle. So, let’s define an animation, `m`, and apply it to the image elements:

Since R is equal to `0.707 * S`, we can say that `R` is equal to `70.7%` of the image size. Here’s a figure to illustrate how we got the `120.7%` value:

Let’s run the animation and see what happens:

I know, I know. The result is far from what we want, but in reality we are very close. It may looks like there’s just one image there, but don’t forget that we have stacked all the images on top of each other. All of them are rotating at the same time and only the top image is visible. What we need is to delay the animation of each image to avoid this overlap.

If we hide the overflow on the container we can already see a slider, but we will update the animation a little so that each image remains visible for a short period before it moves along.

We’re going to update our animation keyframes to do just that:

Now our slider is perfect! Well, almost perfect because we are still missing the final touch: the colorful circular border that rotates around our images. We can use a pseudo-element on the `.gallery` wrapper to make it:

We are done! We have a cool circular slider:

Working with four images is good, but it would be better if we can scale it to any number of images. After all, this is the purpose of an image slider. We should be able to consider `N` images.

For this, we are going to make the code more generic by introducing Sass. First, we define a variable for the number of images (`\$n`) and we will update every part where we hard-coded the number of images (`4`).

The formula for the delay is `(1 - \$i)*duration/\$n`, which gives us the following Sass loop:

We can make the duration a variable as well if we really want to. But let’s move on to the animation:

Let’s simplify it to get a better view of the pattern:

The step between each state is equal to `25%` — which is `100%/4` — and we add a `-90deg` angle — which is `-360deg/4`. That means we can write our loop like this instead:

Since each image takes `5%` of the animation, we change this:

…with this:

It should be noted that `5%` is an arbitrary value I choose for this example. We can also make it a variable to control how much time each image should stay visible. I am going to skip that for the sake of simplicity, but for homework, you can try to do it and share your implementation in the comments!

The last bit is to update `transform-origin`. We will need some geometry tricks. Whatever the number of images, the configuration is always the same. We have our images (small circles) placed inside a big circle and we need to find the value of the radius, `R`.

You probably don’t want a boring geometry explanation so here’s how we find `R`:

If we express that as a percentage, that gives us:

…which means the `transform-origin` value is equal to:

We’re done! We have a slider that works with any number images!

Let’s toss nine images in there:

Add as many images as you want and update the `\$n` variable with the total number of images.

With a few tricks using CSS transforms and standard geometry, we created a nice circular slider that doesn’t require a lot of code. What is cool about this slider is that we don’t need to bother duplicating the images to keep the infinite animation since we have a circle. After a full rotation, we will get back to the first image!

source

If you need help creating a digital marketing strategy for your business, don’t hesitate to contact one of Digidude’s consultants.

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.