search
HomeWeb Front-endCSS TutorialHow to write carousel effect using CSS?

How to write carousel effect using CSS?

I believe that many of the projects that my friends have done have a need for carousels. Some friends may make their own wheels, and some may directly use Google Carousel. Picture plug-in

But if you don’t use javascript, can you achieve the effect of carousel pictures? Maybe friends have not considered this issue, so let's use css to implement a simple carousel chart

Basic demand analysis

Because css cannot To achieve the same precise control as js, some effects cannot be achieved, such as supporting users to slide left and right while carousel, so use css to intelligently achieve basic effects. The content listed below is what we have implemented:

1. In a fixed area, the internal content slides and switches by itself to form a playback effect

2. When switching to the last piece of content, it will Reverse playback or return to the starting point to replay

3. Each content will stay for a period of time so that users can see it clearly

4. The content can be clicked/operated

dom structure construction

First of all, there must be a container as a container for the carousel. At the same time, because sliding switching is to be implemented, there needs to be an internal sub-container that loads all the content to be switched

If the content in the sub-container is switched left and right, the content needs to be arranged left and right.

The following takes the carousel image as an example, the above code

<div class="loop-wrap">
    <div class="loop-images-container">
        <img class="loop-image lazy"  src="/static/imghwm/default1.png"  data-src="darksky.jpg"   alt="" >
        <img class="loop-image lazy"  src="/static/imghwm/default1.png"  data-src="starsky.jpg"   alt="" >
        <img class="loop-image lazy"  src="/static/imghwm/default1.png"  data-src="whiteland.jpg"   alt="" >
        <img class="loop-image lazy"  src="/static/imghwm/default1.png"  data-src="darksky.jpg"   alt="" >
        <img class="loop-image lazy"  src="/static/imghwm/default1.png"  data-src="starsky.jpg"   alt="" >
    </div>
</div>

.loop-wrap is the main container

.loop-images-container is the sub-container that carries internal images

.loop-image is the image content , if you need to display other content, you can customize

css to achieve static effects

The width and height of each page in the carousel should be the same and equal to the main container .loop-wrapWidth and height

.loop-images-container must have one width and height greater than the external main container, and the overflow attribute should Set to hidden. Then why not set it to auto? I won’t tell you, you can try it yourself???

.loop-wrap {
    position: relative;
    width: 500px;
    height: 300px;
    margin: 100px auto;
    overflow: hidden;
}
.loop-images-container{
    position: absolute;
    left: 0; top: 0;
    width: 500%; /* 横向排列 5张图片 宽度应为主容器5倍 */
    height: 100%;
    font-size: 0;
}
.loop-image{
    width: 500px;
    height: 300px;
}

Open the page in the browser below, and you can see a static page without carousel effect. Except for the first picture, other pictures All are invisible

css to achieve carousel effect

The carousel effect is ultimately an animation effect, and through the new attributes of css3animation we You can customize an animation to achieve the carousel effect. Let’s first take a look at animation The

name

value of this attribute <pre class='brush:php;toolbar:false;'>/* animation: name duration timing-function delay iteration-count direction name: 动画名 duration: 动画持续时间 设置为0则不执行 timing-function:动画速度曲线 delay:动画延迟开始时间 设置为0则不延迟 iteration-count:动画循环次数 设置为infinite则无限次循环 direction:是否应该轮流反向播放动画 normal 否 alternate 是 */</pre>animation is the animation name. The animation name can be passed @keyframes Create custom animation rules

Analyze animation

To implement carousel, essentially make a sub-container that carries content inside.loop-images -container Displace, so that content in different locations can be displayed in front of the user at one time

There are five pictures that need to be displayed. If the carousel takes 10s in total, then each picture should have 2s ( 20%), and the time-consuming component of each picture is the switching time and the display time. If the switching takes 500ms (5%), the display time should be 1500ms (15%)

So it was transformed like this css

.loop-images-container{
    position: absolute;
    left: 0; top: 0;
    width: 500%;
    height: 100%;
    font-size: 0;
    transform: translate(0,0); /* 初始位置位移 */
    animation: loop 10s linear infinite;
}
/* 创建loop动画规则 */
/* 
   轮播5张,总耗时10s,单张应为2s(20%)
   单张切换动画耗时500ms,停留1500ms
*/
@keyframes loop {
    0% {transform: translate(0,0);}
    15% {transform: translate(0,0);} /* 停留1500ms */
    20% {transform: translate(-20%,0);} /* 切换500ms 位移-20% */
    35% {transform: translate(-20%,0);}
    40% {transform: translate(-40%,0);}
    55% {transform: translate(-40%,0);}
    60% {transform: translate(-60%,0);}
    75% {transform: translate(-60%,0);}
    80% {transform: translate(-80%,0);}
    95% {transform: translate(-80%,0);}
    100% {transform: translate(0,0);} /* 复位到第一张图片 */
}

Dangdangdang~~~ Pure css implementation of carousel effect is completed

Friends who want to see the effect directly can click on the link below

Pure css implementation Carousel effect

This is a carousel effect in one direction. If you want to achieve a carousel effect in the round-trip direction, friends can try alternate of direction , but the time interval of the custom animation rules also needs to be recalculated!

Recommended tutorial: "CSS Tutorial"

The above is the detailed content of How to write carousel effect using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:zhihu. If there is any infringement, please contact admin@php.cn delete
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment