css动画-卡片滚动效果

动画效果

效果如下:


当卡片进行左右滚动时,每次都恰好滑动出来下一个卡片,将下一个卡片完整的展现在容器中间。

核心代码

1.设置容器和卡片的样式

.wrapper {
    display: flex;
    gap: 20px;
    width: 300px;
    margin: 100px auto;
    overflow-x: scroll;
}

.card {
    box-sizing: border-box;
    padding: 20px 30px;
    flex-shrink: 0;
    width: 300px;
    background-color: white;
    border-radius: 14px;
    text-align: center;
}

2.为容器设置 scroll-snap-type属性

.wrapper {
    scroll-snap-type: x mandatory;
}

x是指沿着X轴滚动,mandatory是若滚动容器当前未在滚动,则其可见视口必须吸附至吸附位置。。

3.为卡片设置scroll-snap-align属性

.card {
    scroll-snap-align: center;
}

完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=
    , initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-color: #e8e7ee;

        }

        .wrapper {
            display: flex;
            gap: 20px;
            width: 300px;
            margin: 100px auto;
            overflow-x: scroll;
            scroll-snap-type: x mandatory;

        }

        .card {
            scroll-snap-align: center;

            box-sizing: border-box;
            padding: 20px 30px;
            flex-shrink: 0;
            width: 300px;
            background-color: white;
            border-radius: 14px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="card">
            <h2>Card 1</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 2</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 3</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 4</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 5</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 6</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 7</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 8</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 9</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
        <div class="card">
            <h2>Card 10</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim tempore commodi a recusandae aliquam est
                saepe eaque molestiae blanditiis amet suscipit iure, magnam facere, consectetur nam dolorum error
                aliquid reprehenderit.</p>
        </div>
    </div>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joyce Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值