3D立体卡片动效(附源码)

3D立体卡片动效

效果展示


思路分析

  需求含有立体 这种关键词,我们第一反应是采用动画中的平移 、倾斜 等实现。如果是立体 ,必然产生阴影,应用的处理采用滤镜处理。

页面布局

html 复制代码
<div class="container">
	<div class="card">
		<div class="img_box">
			<img class="avatar" src="./img/1.jpg" alt="">
			<h3 class="name">卫庄</h3>
		</div>
		<div class="content">
			<p>韩国人,鬼谷派横剑术传人,现任"鬼谷先生,""剑圣"盖聂的师弟。"流沙"组织的创立者之一,曾任韩国大将军,韩国灭亡后统领"流沙"暗中对抗秦国和罗网组织,在他的经营下,流沙成为了令天下人闻风丧胆的职业刺客团。</p>
		</div>
	</div>
	<div class="card">此处省略相同布局</div>
	<div class="card">此处省略相同布局</div>
</div>

1. 基础样式

css 复制代码
* {
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f3f3f3;
  overflow: hidden;
}

.container{
  position: relative;
  width: 1050px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .card {
    position: relative;
    width: 300px;
    height: 400px;
    background: #fff;
    transition: 0.5s;
    .img_box {
      position: relative;
      width: 300px;
      height: 200px;
      background-color: #00c7ff;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .name {
        position: relative;
        color: #ffffff;
        margin-top: 10px;
      }

      .avatar {
        max-width: 100px;
      }
    }

    .content {
      box-sizing: border-box;
      position: relative;
      width: 100%;
      height: 200px;
      padding: 20px;
      color: #777;
      background: #ffffff;

      .desc{
        text-indent: 2rem;
        font-size: 15px;
        line-height: 24px;
      }
    }
  }
}

2. 实现3D效果

2.1 顶面布局

css 复制代码
card::before {
  content: "";
  position: absolute;
  top: -15px;
  left: 0;
  width: 100%;
  height: 15px;
  background-color: #00c0f6;
  transform-origin: bottom;
  transform: skewX(45deg);
  transition: 0.5s;
}

特别注意

  • 为了实现 3D效果 ,我们可以采用 伪元素 的方式。
  • card::before 用于制作卡片的 顶部切面;
  • transform-origin: bottom; 用于改变元素的 基点位置 ;
  • transform: skewX(45deg); 用于定义沿着 X 轴的 2D 倾斜转换;
  • 其中 top值 与 height值 要结合倾斜角度计算,或者通过控制台手动微调;

2.2 侧面布局

css 复制代码
card::after {
  content: "";
  position: absolute;
  top: -15px;
  left: -15px;
  width: 15px;
  height: 50%;
  background-color: #00c0f6;
  transform-origin: left;
  transform: skewY(45deg);
  transition: 0.5s;
  border-bottom: 200px solid #d9d9d9;
}

特别注意

  • 同上
  • 思考:height:50%; 是怎么计算出来的;

组合效果


3. 阴影效果

css 复制代码
.content::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 400px;
  background: linear-gradient(transparent, transparent, rgba(0, 0, 0, 0.1));
  transform-origin: bottom;
  transform: skewX(45deg);
  transition: 0.5s;
  pointer-events: none;
  z-index: -1;
}

4. 鼠标移入效果

css 复制代码
.card:hover {
  transform: translateY(-40px);

  .content {
    &::before {
      transform: translateY(40px) skewX(45deg);
      filter: blur(5px);
      opacity: 0.5;
    }
  }
}

5. 细节补充

css 复制代码
.card:nth-child(1) {
  z-index: 3;
}

.card:nth-child(2) {
  z-index: 2;
}

.card:nth-child(3) {
  z-index: 1;
}

  此处,处理定义的 层级问题 ,是为了处理 阴影的遮挡问题。


温馨提示

  • 更多博文,请关注:xssy5431 小拾岁月,回复 "3D" ,获取源码;
相关推荐
Setsuna_F_Seiei6 小时前
性能优化 - 列表分页查询的优化
前端·vue.js·性能优化
Setsuna_F_Seiei6 小时前
性能优化 - Vue.js 的懒加载的优化封装
前端·vue.js·性能优化
excel7 小时前
Prisma 查询不到 SQL 更新后的字段?先别急着重新打包
前端
狗头大军之江苏分军9 小时前
《潮水漫过十七岁》倒计时
前端·javascript·后端
小码哥06810 小时前
租房平台源码部署验收:房源样例、区域字典与看房预约逐项验证
前端·javascript·vue.js·租房小程序·租房系统·租房软件
htzyl20611 小时前
JS原型链与继承:class 底下到底是什么
前端·javascript·ecmascript 6
一条Go11 小时前
15 套品牌色 + 双模式:把颜色关进三层 token
前端·github
htzyl20611 小时前
JS-关于闭包捕获与释放
前端·javascript
狗头大军之江苏分军11 小时前
《潮水漫过十七岁》好久没写了
前端·javascript·后端
console.log('npc')11 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习