3分钟快速理解并实现CSS轮播图

css轮播图实现

首先我们要理解这样一个问题,在css轮播图的移动中,是图片移动还是所有图片一起移动呢?答案是后者,我们通过下面的图可以了解到,通过集体向translateX移动所有照片X轴的总和,比如,我们每一张图片是400px,4张图片,那麽就是400*4 = 1600px;translateX移动1600px,因为是向左滑行,所以移动的值是负值。

​ 功能:当我们鼠标有一个移入的动作时,轮播停止;

我们下面这个案例会帮助我们更快的理解到:

在这里插入图片描述

 <main>
        <section>
            <div><img src="./img/a.jpg" alt=""></div>
            <div><img src="./img/b.jpg" alt=""></div>
            <div><img src="./img/c.jpg" alt=""></div>
            <div><img src="./img/d.jpg" alt=""></div>
        </section>
</main>

通过使用css中的animation中的元素就可以完成,在这里需要注意的是,每次移动的大小一定是我们设置图片的宽(width)值:

* {
            margin: 0;
            padding: 0;
        }
        
        body {
            width: 100vw;
            height: 100vh;
            display: grid;
            align-self: center;
            justify-self: center;
            background: #2c3e50;
        }
        
        main {
            width: 400px;
            height: 200px;
            align-self: center;
            justify-self: center;
            overflow: hidden;
            position: relative;
        }
        
        section {
            width: 1600px;
            height: 200px;
            display: grid;
            grid-template: 1fr/repeat(4, 400px);
            animation-name: dong;
            animation-duration: 4s;
            animation-timing-function: steps(4, end);
            animation-iteration-count: infinite;
        }
        
        section:hover {
            animation-play-state: paused;
        }
        
        section div {
            overflow: hidden;
        }
        
        section div img {
            width: 100%;
        }
 
        @keyframes dong {
            to {
                transform: translateX(-1600px);
            }
        }

效果图:

在这里插入图片描述

轮播拓展

我们常见的就是轮播下面有几个小点,这几个小点需要和上面的照片出现一个同步的效果,也就说在css中他们执行的时间是相同的,而且每个小点移动的距离我们也要算好。在这里我们要一个无序标签作为每一个小点,通过定位的方式完成。

​ 标签的布局:

<main>
        <section>
            <div><img src="./img/a.jpg" alt=""></div>
            <div><img src="./img/b.jpg" alt=""></div>
            <div><img src="./img/c.jpg" alt=""></div>
            <div><img src="./img/d.jpg" alt=""></div>
        </section>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </main>

通过定位方式以及层级的方式使得下面的小点点定位到这个轮播照片下面,同样当鼠标移入照片中,图片和小点同时静止,我们给div、section、ul 同时加一个鼠标移入时候,照片停止播放。

  * {
            margin: 0;
            padding: 0;
        }
        
        body {
            width: 100vw;
            height: 100vh;
            display: grid;
            align-self: center;
            justify-self: center;
            background: #2c3e50;
        }
        
        main {
            width: 400px;
            height: 200px;
            align-self: center;
            justify-self: center;
            overflow: hidden;
            position: relative;
        }
        
        section {
            width: 1600px;
            height: 200px;
            display: grid;
            grid-template: 1fr/repeat(4, 400px);
            animation-name: dong;
            animation-duration: 4s;
            animation-timing-function: steps(4, end);
            animation-iteration-count: infinite;
        }
        
        section:hover,
        section:hover+ul::after {
            animation-play-state: paused;
        }
        
        section div {
            overflow: hidden;
        }
        
        section div img {
            width: 100%;
        }
        
        ul {
            position: absolute;
            width: 120px;
            left: 50%;
            bottom: 80px;
            transform: translateX(-50%);
            display: grid;
            grid-template: 1fr/repeat(4, 1fr);
            list-style: none;
        }
        
        ul::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0px;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background-color: red;
            z-index: -1;
            animation-name: num;
            animation-duration: 4s;
            animation-timing-function: steps(4, end);
            animation-iteration-count: infinite;
        }
        
        li {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background: rgba(0, 0, 0, .3);
            display: grid;
            justify-items: center;
            align-items: center;
            color: white;
        }
        
        @keyframes dong {
            to {
                transform: translateX(-1600px);
            }
        }
        
        @keyframes num {
            to {
                transform: translateX(120px);
            }
        }

实现效果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值