当前位置:   article > 正文

CSS实现卡片浮动效果_3d名片悬浮css

3d名片悬浮css

效果演示

在这里插入图片描述

实现原理

给卡片添加 css3 新的过度属性(transition),鼠标移入和移出时都会触发这个过度属性,使卡片向上偏移一段距离,同时扩大卡片的背景阴影,从而实现卡片的浮动效果。

关键代码

.card-box .card {
    transition: transform .2s, box-shadow .2s;
    /* 省略部分代码 */
}
.card-box .card:hover {
    transition: transform .2s, box-shadow .2s;
    transform: translateY(-4px);
    box-shadow: 4px 4px 8px rgba(0, 0, 0, .1), -4px -4px 8px rgba(0, 0, 0, .1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

源代码

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <title>卡片浮动效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-size: 20px;
            background-color: #e3e3e3;
        }
        .card-box {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            align-content: space-between;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 40em;
            height: 22em;
            padding: 1.5em;
            background-color: #e3e3e3;
        }
        .card-box .card {
            transition: transform .2s, box-shadow .2s;
            display: flex;
            justify-content: center;
            align-items: center;
            width: 5.5em;
            height: 3.5em;
            margin-bottom: 1em;
            border-radius: .5em;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, .1), -2px -2px 4px rgba(0, 0, 0, .1);
            background-color: #fff;
            color: #3e3e3e;
            font-size: 1.5em;
            font-weight: 700;
            letter-spacing: 4px;
            cursor: pointer;
        }
        .card-box .card:hover {
            transition: transform .2s, box-shadow .2s;
            transform: translateY(-4px);
            box-shadow: 4px 4px 8px rgba(0, 0, 0, .1), -4px -4px 8px rgba(0, 0, 0, .1);
        }
    </style>
</head>
<body>
    <div class="card-box">
        <div class="card"><span>富强</span></div>
        <div class="card"><span>民主</span></div>
        <div class="card"><span>文明</span></div>
        <div class="card"><span>和谐</span></div>
        <div class="card"><span>自由</span></div>
        <div class="card"><span>平等</span></div>
        <div class="card"><span>公正</span></div>
        <div class="card"><span>法治</span></div>
        <div class="card"><span>爱国</span></div>
        <div class="card"><span>敬业</span></div>
        <div class="card"><span>诚信</span></div>
        <div class="card"><span>友善</span></div>
    </div>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/96739
推荐阅读