【css酷炫效果】纯CSS实现照片堆叠效果

想直接拿走的老板,链接放在这里:https://download.youkuaiyun.com/download/u011561335/90492022

创作随缘,不定时更新。

创作背景

刚看到csdn出活动了,赶时间,直接上代码。

html结构

    <div class="photo-stack">
        <div class="photo" style="background-image: url('a.gif');"></div>
        <div class="photo" style="background-image: url('a.gif');"></div>
        <div class="photo" style="background-image: url('a.gif');"></div> 
    </div>

css样式

body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

.photo-stack {
    position: relative;
    width: 300px; /* 根据需要调整宽度 */
    height: 400px; /* 根据需要调整高度 */
    perspective: 1000px; /* 添加透视效果 */
}

.photo {
    position: absolute;
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

/* 设置每张照片的阴影和位置 */
.photo:nth-child(1) {
    z-index: 3; /* 最上层 */
    transform: translateY(0) rotateX(0deg); /* 初始位置 */
    box-shadow: 
        0 4px 8px rgba(0, 0, 0, 0.2), 
        0 8px 16px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.photo:nth-child(2) {
    z-index: 2; /* 中间层 */
    transform: translateY(10px) rotateX(5deg); /* 稍微偏移和旋转 */
    box-shadow: 
        0 6px 12px rgba(0, 0, 0, 0.2), 
        0 12px 24px rgba(0, 0, 0, 0.1); /* 阴影比上层更深 */
}

.photo:nth-child(3) {
    z-index: 1; /* 最下层 */
    transform: translateY(20px) rotateX(10deg); /* 更多偏移和旋转 */
    box-shadow: 
        0 8px 16px rgba(0, 0, 0, 0.3), 
        0 16px 32px rgba(0, 0, 0, 0.15); /* 最深的阴影 */
}

完整代码

基础版

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

<title>页面特效</title>
<style type="text/css">
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

.photo-stack {
    position: relative;
    width: 300px; /* 根据需要调整宽度 */
    height: 400px; /* 根据需要调整高度 */
    perspective: 1000px; /* 添加透视效果 */
}

.photo {
    position: absolute;
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

/* 设置每张照片的阴影和位置 */
.photo:nth-child(1) {
    z-index: 3; /* 最上层 */
    transform: translateY(0) rotateX(0deg); /* 初始位置 */
    box-shadow: 
        0 4px 8px rgba(0, 0, 0, 0.2), 
        0 8px 16px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.photo:nth-child(2) {
    z-index: 2; /* 中间层 */
    transform: translateY(10px) rotateX(5deg); /* 稍微偏移和旋转 */
    box-shadow: 
        0 6px 12px rgba(0, 0, 0, 0.2), 
        0 12px 24px rgba(0, 0, 0, 0.1); /* 阴影比上层更深 */
}

.photo:nth-child(3) {
    z-index: 1; /* 最下层 */
    transform: translateY(20px) rotateX(10deg); /* 更多偏移和旋转 */
    box-shadow: 
        0 8px 16px rgba(0, 0, 0, 0.3), 
        0 16px 32px rgba(0, 0, 0, 0.15); /* 最深的阴影 */
}
</style>

</head>
<body>
    <div class="photo-stack">
        <div class="photo" style="background-image: url('a.gif');"></div>
        <div class="photo" style="background-image: url('a.gif');"></div>
        <div class="photo" style="background-image: url('a.gif');"></div> 
    </div>

</body>
</html>

进阶版(增加鼠标悬停查看)

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

<title>页面特效</title>
<style type="text/css">
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

.photo-stack {
    position: relative;
    width: 300px; /* 根据需要调整宽度 */
    height: 400px; /* 根据需要调整高度 */
    perspective: 1000px; /* 添加透视效果 */
}

.photo {
    position: absolute;
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

/* 设置每张照片的阴影和位置 */
.photo:nth-child(1) {
    z-index: 3; /* 最上层 */
    transform: translateY(0) rotateX(0deg); /* 初始位置 */
    box-shadow: 
        0 4px 8px rgba(0, 0, 0, 0.2), 
        0 8px 16px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.photo:nth-child(2) {
    z-index: 2; /* 中间层 */
    transform: translateY(10px) rotateX(5deg); /* 稍微偏移和旋转 */
    box-shadow: 
        0 6px 12px rgba(0, 0, 0, 0.2), 
        0 12px 24px rgba(0, 0, 0, 0.1); /* 阴影比上层更深 */
}

.photo:nth-child(3) {
    z-index: 1; /* 最下层 */
    transform: translateY(20px) rotateX(10deg); /* 更多偏移和旋转 */
    box-shadow: 
        0 8px 16px rgba(0, 0, 0, 0.3), 
        0 16px 32px rgba(0, 0, 0, 0.15); /* 最深的阴影 */
}
.photo:hover {
    transform: translateX(410px) rotateX(-5deg); /* 悬停时上移并稍微旋转 */
    box-shadow: 
        0 12px 24px rgba(0, 0, 0, 0.3), 
        0 24px 48px rgba(0, 0, 0, 0.2); /* 更深的阴影 */
}
</style>

</head>
<body>
    <div class="photo-stack">
        <div class="photo" style="background-image: url('a.gif');"></div>
        <div class="photo" style="background-image: url('a.gif');"></div>
        <div class="photo" style="background-image: url('a.gif');"></div> 
    </div>

</body>
</html>

效果图

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

冰夏之夜影

赠人玫瑰,手留余香

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

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

打赏作者

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

抵扣说明:

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

余额充值