HTML&CSS:动态背景卡片,放大与移动特效拉满

326 阅读2分钟

这段代码创建了一个具有动态背景效果的卡片,通过 CSS 技术实现了背景的移动和放大效果,为页面添加了视觉吸引力和用户交互体验。

演示效果

HTML&CSS

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公众号关注:前端Hardy</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #e8e8e8;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }

        .container {
            width: 190px;
            height: 254px;
            background: transparent;
            position: relative;
            box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.438);
            overflow: hidden;
            border-radius: 10px;
        }

        .card {
            cursor: default;
            width: 100%;
            height: 100%;
            position: relative;
            z-index: 2;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 2px;
            color: #212121;
            background-color: rgba(255, 255, 255, 0.074);
            border: 1px solid rgba(255, 255, 255, 0.222);
            -webkit-backdrop-filter: blur(20px);
            backdrop-filter: blur(20px);
            border-radius: 10px;
            transition: all ease .3s;
        }

        .container::after,
        .container::before {
            width: 100px;
            height: 100px;
            content: '';
            position: absolute;
            border-radius: 50%;
            transition: .5s linear;
        }

        .container::after {
            top: -20px;
            left: -20px;
            background-color: #2879f3;
            animation: animFirst 5s linear infinite;
        }

        .container::before {
            background-color: rgb(226, 223, 54);
            top: 70%;
            left: 70%;
            animation: animSecond 5s linear infinite;
            animation-delay: 3s;
        }

        .container:hover {
            box-shadow: 0px 0px 10px rgba(0, 77, 32, 0.632)
        }

        .container:hover::after {
            left: 80px;
            transform: scale(1.2);
        }

        .container:hover::before {
            left: -10px;
            transform: scale(1.2);
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="card">
            鼠标移入
        </div>
    </div>
</body>

</html>

HTML 结构

  • container: 创建一个类名为container的 div 元素,用于包含卡片。
  • card: 创建一个类名为card的 div 元素,用于显示卡片内容。

CSS 样式

  • body: 设置页面的边距、填充、背景色、显示方式和高度。
  • .container: 设置卡片容器的样式,包括尺寸、背景、位置、阴影、溢出隐藏和边框半径。
  • .card: 设置卡片的样式,包括尺寸、位置、对齐方式、字体大小、权重、文本转换、字母间距、颜色、背景色、边框、背景模糊效果和过渡效果。
  • .container::after, .container::before: 设置卡片容器的伪元素,用于创建背景效果。
  • .container::after: 设置第一个伪元素的样式,包括位置、尺寸、背景色和动画效果。
  • .container::before: 设置第二个伪元素的样式,包括位置、尺寸、背景色、动画效果和延迟。
  • .container:hover: 设置鼠标悬停在卡片容器上时的阴影效果。
  • .container:hover::after: 设置鼠标悬停在卡片容器上时第一个伪元素的样式,使其移动和放大。
  • .container:hover::before: 设置鼠标悬停在卡片容器上时第二个伪元素的样式,使其移动和放大。