使用HTML+CSS实现拟物耳机充电盒

阅读量:19
更新时间:2025-04-13 23:27:08

效果预览:效果预览
源码下载:关注公众号【程序员新视界】,回复【css20】可获取源码

HTML

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拟物耳机盒效果</title>
    <link rel="stylesheet" href="./css/index.css">
</head>

<body>
    <!-- 主要卡片容器 -->
    <div class="card">
        <!-- 凹槽结构 -->
        <div class="lc cavity"></div>
        <!-- 中心分割线 -->
        <div class="lc line"></div>
        <!-- LED指示灯 -->
        <div class="led"></div>
        <div class="text">Hello World!</div>
    </div>
</body>

</html>

CSS

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

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

.card {
    width: 270px;
    height: 220px;
    background-color: #e9e9e9;
    border-radius: 70px;
    box-shadow:
        inset 0px 35px 25px #ffffffe0,
        inset 10px 0px 25px #0000004b,
        inset 40px 0px 20px #ffffff,
        inset -10px 0px 25px #0000004b,
        inset -40px 0px 20px #fff,
        inset 0px 10px 10px #000000e0,
        inset 0px -15px 25px #00000036,
        10px 25px 40px -10px #00000060;
    position: relative;
}

.card .line {
    width: 100%;
    height: 2px;
    background-color: #bbb;
    margin-top: 30%;
    position: relative;
}

.line::after,
.line::before {
    content: "";
    position: absolute;
    width: 5%;
    height: 2px;
    background-color: #fff;
}

.line::before {
    right: 0;
}

.card .cavity {
    width: 150px;
    height: 20px;
    background: linear-gradient(180deg, #d6d6d6, #fff);
    border-radius: 200px;
    margin: auto;
    position: absolute;
    top: 30%;
    left: 50%;
    transform: translate(-50%, 30%);
}

.card .led {
    width: 7px;
    aspect-ratio: 1;
    background-color: #00ff00;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, 30%);
    border-radius: 100px;
    box-shadow: 0 0 6px #00ff00;
    animation: colorChange 2s ease-in-out infinite;
}

.card .text {
    text-align: center;
    margin-top: 70px;
    color: #00000036;
    font-weight: bolder;
}

@keyframes colorChange {
    0% {
        background-color: #ff0000;
        box-shadow: 0 0 6px #ff0000;
    }
    50% {
        background-color: #00ff00;
        box-shadow: 0 0 6px #00ff00;
    }
    100% {
        background-color: #ff0000;
        box-shadow: 0 0 6px #ff0000;
    }
}