当前位置: 首页 > news >正文

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

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

  • 创作背景
  • html结构
  • css样式
  • 完整代码
    • 基础版
    • 进阶版(增加鼠标悬停查看)
  • 效果图

想直接拿走的老板,链接放在这里:https://download.csdn.net/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>

效果图

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

相关文章:

  • 2025年通信安全员考试题库及答案
  • xxl-job 执行器端服务器的简单搭建
  • OneCyber 平台
  • 杨校老师课堂之编程入门与软件安装【图文笔记】
  • 将Django连接到mysql
  • numpy学习笔记8:数组属性和基础操作的详细描述
  • C++中pow函数的作用是什么,如何使用它?
  • Etcd 服务搭建
  • 【YOLOv8】YOLOv8改进系列(8)----替换主干网络之Swin Transformer
  • 网络编程day2
  • C++中的左移(<<)、右移(>>)运算符
  • Java数据类型 Arrays VS ArraysList VS LikedList 解析
  • 从数据洪流到智能洞察:人工智能如何解锁大数据的价值?
  • C++与C的基本不同
  • 2025年最新︱ASPM态势感知平台介绍
  • react-native 踩坑
  • 【LInux进程六】命令行参数和环境变量
  • 外聘教师管理系统基于Spring BootSSM
  • 软考中级-数据库-5.3-Internet基础知识
  • Netty:java高性能网络编程的基石(下)
  • 李伟任山东省委常委、省纪委书记
  • 联合国报告:全球经济前景恶化,面临高度不确定性
  • 京东回应外卖系统崩溃:订单暴涨所致,已恢复
  • 大英博物馆展歌川广重:他是梵高最钟爱的浮世绘名家
  • 中国-拉共体成员国重点领域合作共同行动计划(2025-2027)
  • 京东一季度净利增长五成,营收增速创近三年新高,称外卖业务取得显著进展