惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
B
Blog
U
Unit 42
有赞技术团队
有赞技术团队
博客园 - 聂微东
GbyAI
GbyAI
宝玉的分享
宝玉的分享
F
Full Disclosure
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
Jina AI
Jina AI
Martin Fowler
Martin Fowler
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
P
Proofpoint News Feed
A
About on SuperTechFans
I
InfoQ
博客园 - 【当耐特】
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
Project Zero
Project Zero
WordPress大学
WordPress大学
小众软件
小众软件
AWS News Blog
AWS News Blog
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
I
Intezer
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks
T
Tenable Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
How to create an infinite image slider using CSS
2022-10-04 · via CSS Articles by Temani Afif

How to create an infinite image slider using CSS

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

You must have seen those sliders of images that have an infinite loop animation. They are also called infinite carousels where all the images appear and disappear indefinitely. To create such sliders, most of the tricks rely on JavaScript or on duplicating the images. In this post, we will see how to do this without JavaScript and without duplicating any of the images.

We are going to use a few CSS tricks to build something like the below:

The Markup

The HTML code is as simple as a list of images within a container:

<div class="gallery">
  <img src="" alt="">
  <img src="" alt="">
  <img src="" alt="">
  <img src="" alt="">
</div>

As I said in the introduction, we are not going to duplicate any of the images so all of them are unique. We also don’t need any complex code structure like a lot of divs.

The Grid Configuration

We are going to rely on CSS Grid for the image placement.

.gallery  {
  display: grid;
  overflow: hidden;
}
.gallery > img {
  grid-area: 1/1;
  height: 200px;
  aspect-ratio: 1.5;
  object-fit: cover;
}

Nothing complex so far. We create a grid with specific dimensions (it’s up to you to adjust it as you want) then we place all the images above each other inside the same grid track using grid-area: 1/1.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED HOSTING PLANS FOR WORDPRESS

with the discount code

SERVERS-SALE

The Animation

Let’s now tackle the important part of our code: The animation. The trickiest part here is to simulate the infinite loop without duplicating the image. We need to also consider the number of images.

In our case, we are using four images and we are going to split our animation into two steps. The first step is illustrated below:

Infinite Slider animation

The red dashes are the limit of our container. All the images need to move to the left by three times their size which will make the last image visible. This first step can be done by using transform: translate(-300%).

The next step is to move the first image right before the last one then translate it again until it becomes the visible image.

second step of the infinite slider animation

Our keyframes will look like below:

@keyframes r {
  /* Step 1 */
  0%    {transform: translate(0)}
  X%    {transform: translate(-300%)}
  /* Step 2 */
  (X + .01)% {transform: translate( 100%)}
  100%   {transform: translate(0)}
}

We first move our image to the left by -300%. Then we put it back to the right using 100%. Note that this should happen instantly that is why I am using X + .01. I am adding a tiny percentage to the previous state. Finally, we get back to the initial state.

Now we need to find the value of X. In the first step, the image is moving by three times its size (from 0% to -300%) while in the second step it’s moving by one time its size (from 100% to 0%). This means that the first step needs to stay three times longer than the second one. We have the following formulas:

T1 + T2 = 100%
T1 = 3*T2

Which give us

T1 = (4/3)*100% = 75%

The first step, need to run 75% of the keyframes so X needs to be equal to 75%. We can also omit the 0% and 100% from the keyframes since they are the default values and use only the following code:

@keyframes r {
  75%    {transform: translate(-300%)}
  75.01% {transform: translate( 100%)}
}

Oops, the animation seems to be broken and only one image is visible. Don’t worry, I didn’t make any mistakes and what you see is logical. Remember that in the Grid configuration we placed all the images above each other inside the same grid track. This means that all of them are moving together and only the top image is visible.

To fix this, we need to consider animation delays. The logic is as follows: We have fours images and the duration is defined by the variable --d so we will use var(--d)/4 and we define the delays like below:

  • The 1st image: calc(0*var(--d)/4)

  • The 2nd image: calc(1*var(--d)/4)

  • The 3rd image: calc(2*var(--d)/4)

  • The 4th image: calc(3*var(--d)/4)

Almost good but we have to wait for the animation to run. This is because we are using positive delays making the last image (the one at the top) start late. To fix this we simply make the delay negative and we are good:

The full code again:

.gallery  {
  --d: 6s; /* duration */
  
  display: grid;
  overflow: hidden;
  -webkit-mask: linear-gradient(90deg,#0000,#000 10% 90%,#0000);
}
.gallery > img {
  grid-area: 1/1;
  height: 200px;
  aspect-ratio: 1.5;
  object-fit: cover;
  animation: r var(--d) linear infinite;
}
.gallery > img:nth-child(2) {animation-delay: calc(1*var(--d)/-4)}
.gallery > img:nth-child(3) {animation-delay: calc(2*var(--d)/-4)}
.gallery > img:nth-child(4) {animation-delay: calc(3*var(--d)/-4)}

@keyframes r {
  75%    {transform: translate(-300%)}
  75.01% {transform: translate( 100%)}
}

With only a few lines of code, we get an infinite slider that works without duplicating the images.

Using N images

We can still do better and introduce another variable to make the code generic so it can work with any number of images.

For this we will rely on Sass:

$n:5; /* number of images*/

.gallery  {
  --d: 10s; /* duration */
  
  display: grid;
  overflow: hidden;
  width: 380px;
  -webkit-mask: linear-gradient(90deg,#0000,#000 10% 90%,#0000);
}
.gallery > img {
  grid-area: 1/1;
  width: 100%;
  aspect-ratio: 1.5;
  object-fit: cover;
  animation: r var(--d) linear infinite;
}
@for $i from 2 to ($n + 1) {
  .gallery > img:nth-child(#{$i}) {animation-delay: calc(#{(1 - $i)/$n}*var(--d))}
}
@keyframes r {
  #{100*($n - 1)/$n}% {transform: translate((1 - $n)*100%)}
  #{100*($n - 1)/$n + .01}% {transform: translate(100%)}
}

When we used 4 images we had to translate by -300% so with N images we need (1 - N)*100%. For the delays, we use a loop to set the value for all the images.

For the keyframes, remember the formulas we used:

T1 + T2 = 100%
T1 = 3*T2

Instead of 3, we will use N - 1 to get

T1 + T2 = 100%
T1 = (N - 1)*T2

Which will give us

T1 = 100% * N/(N - 1)

That’s it! With simple math, we transformed our code into a generic one that can work with any number of images:

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

Conclusion

Without JavaScript and without duplicating any of the images, we made an infinite image slider with an optimized code. Not to mention efficiency since we are animating only the transform property. To this, we added a touch of Sass to be able to easily control the number of images.

Frequently Asked Questions

Yes, and without issue. Regardless of how many websites you’re managing, we’ll bring all of them under the Verpex wing free of charge. Just send us the details of your websites when you’ve signed up to get started.

Our job doesn’t end once we’ve sold you the reseller space. In fact, we’re with you for the whole time you’re hosting with us, supporting both your needs and that of the customers you pick up along the way, whether you max out your cPanel account limit or not.

The Verpex team is at your disposal, acting as your essential support team 24/7. That means anything pertaining to hardware, the network, updates and configuration, security, and connectivity, is all handled by us.

Both you and your customers can focus on running your businesses while we take care of all maintenance and troubleshooting with expert technical support.

No, we make the transfer process easy and you can keep your current domain name.

No. The pricing will stay the same as you had on Sharkhosting, you'll find no hidden costs here. All payment information is transparent and made clear from the outset.