使用css3绘制冒热气的杯子动画特效

113 阅读2分钟

"```markdown

使用CSS3绘制冒热气的杯子动画特效

在这篇文章中,我们将使用CSS3绘制一个冒热气的杯子动画特效。这种效果可以用于咖啡店网站或任何想要展示饮品的网页。

HTML结构

首先,我们需要一个简单的HTML结构来表示杯子和热气的效果。

<div class=\"cup\">
    <div class=\"steam\"></div>
</div>

CSS样式

接下来,我们将为这个杯子和热气添加样式。我们使用CSS3的@keyframes来创建动画效果。

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

.cup {
    position: relative;
    width: 100px;
    height: 60px;
    background-color: #8B4513; /* 咖啡杯的颜色 */
    border-radius: 20px 20px 0 0;
    overflow: hidden;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}

.steam {
    position: absolute;
    bottom: 60px;
    left: 50%;
    width: 10px;
    height: 10px;
    background-color: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    animation: steam 1s infinite ease-in-out;
}

@keyframes steam {
    0% {
        transform: translate(-50%, 0) scale(0.5);
        opacity: 1;
    }
    50% {
        transform: translate(-50%, -20px) scale(1);
        opacity: 0.5;
    }
    100% {
        transform: translate(-50%, -40px) scale(0.5);
        opacity: 0;
    }
}

/* 添加多个蒸汽效果 */
.steam:nth-child(1) {
    animation-delay: 0s;
}

.steam:nth-child(2) {
    left: 40%;
    animation-delay: 0.2s;
}

.steam:nth-child(3) {
    left: 60%;
    animation-delay: 0.4s;
}

完整HTML和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>冒热气的杯子动画</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }

        .cup {
            position: relative;
            width: 100px;
            height: 60px;
            background-color: #8B4513;
            border-radius: 20px 20px 0 0;
            overflow: hidden;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }

        .steam {
            position: absolute;
            bottom: 60px;
            left: 50%;
            width: 10px;
            height: 10px;
            background-color: rgba(255, 255, 255, 0.7);
            border-radius: 50%;
            animation: steam 1s infinite ease-in-out;
        }

        @keyframes steam {
            0% {
                transform: translate(-50%, 0) scale(0.5);
                opacity: 1;
            }
            50% {
                transform: translate(-50%, -20px) scale(1);
                opacity: 0.5;
            }
            100% {
                transform: translate(-50%, -40px) scale(0.5);
                opacity: 0;
            }
        }

        .steam:nth-child(1) { animation-delay: 0s; }
        .steam:nth-child(2) { left: 40%; animation-delay: 0.2s; }
        .steam:nth-child(3) { left: 60%; animation-delay: 0.4s; }
    </style>
</head>
<body>
    <div class=\"cup\">
        <div class=\"steam\"></div>
        <div class=\"steam\"></div>
        <div class=\"steam\"></div>
    </div>
</body>
</html>

通过上述代码,你将能够创建一个简单而有趣的冒热气的杯子动画特效。可以根据需要调整颜色、大小和动画速度,以适应你的设计需求。