用css做一枚拟物风格的按钮

示例代码仓库:leixq1024/FrontEndSnippetHub: ✨html+css+js的前端特效合集

前言

不得不说小红书真不错, 复刻上面的一些小组件,对于练习css也很有帮助

​ 前两天刷小红书的时候看到有人用figma做了一个拟物的cmd按钮,效果如下

最终效果

我用css+svg复刻了这个按钮,效果如下

​ 其中svg是用来做按钮上面的噪点效果的,剩下主要是一些阴影的应用,具体就不做过多解释,可以看代码.

完整代码

html 复制代码
<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>拟物按钮</title>
    <style>
      :root {
        --btn-top: 13px;
      }
      html,
      body {
        position: relative;
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(180deg, #dfe2e4 50%, #657178 100%);
      }
      .btn {
        position: relative;
        width: 135px;
        height: 138px;
        border-radius: 30px;
        cursor: pointer;
        background: linear-gradient(
          90deg,
          #7c8288 0%,
          #4d5357 20%,
          #54595d 74%,
          #878c92 100%
        );
        box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
      }
      .btn-1 {
        position: absolute;
        top: -16px;
        width: 100%;
        height: 100%;
        border: 1px solid rgba(139, 138, 159, 0.5);
        border-radius: 30px;
        background: linear-gradient(180deg, #c7c8cb 0%, #88959f 100%);
        box-shadow: inset 0 -1px 3px rgba(255, 255, 255, 0.6);
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .btn-2 {
        position: absolute;
        width: 111px;
        height: 111px;
        border-radius: 15px;
        background: #272727;
      }
      .btn-2__warpper {
        position: absolute;
        top: var(--btn-top);
        width: 105px;
        height: 105px;
        border-radius: 13px;
        background: linear-gradient(
          90deg,
          #8f8f8f 0%,
          #626463 13%,
          #979c9d 89%,
          #797b7c 100%
        );
        border: 1px solid rgba(0, 0, 0, 0.5);
        transition: all 0.15s ease-in-out;
      }
      .btn-2__body {
        position: absolute;
        border-radius: 13px;
        width: 105px;
        height: 95px;
        background: linear-gradient(180deg, #fff 0%, #bfc4c7 100%);
        display: flex;
        align-items: end;
        justify-content: space-between;
        padding: 4px 12px;
        box-sizing: border-box;
        color: #929798;
        text-shadow: -1px -1px 1px #000, 1px 1px 1px rgba(255, 255, 255, 0.5);
        user-select: none;
      }
      /* 噪点背景 */
      .noise {
        position: absolute;
        top: -16px;
        left: 0;
        border-radius: 30px;
        opacity: 0.4;
        z-index: 999;
        display: block;
        transform: scaleY(2.15);
        transform-origin: top;
        pointer-events: none;
      }
    </style>
  </head>
  <body>
    <div class="btn" onmousedown="btnDown()" onmouseup="btnUp()">
      <!-- 噪点背景 -->
      <svg
        class="noise"
        width="135px"
        height="70px"
        viewBox="0 0 135 70"
        xmlns="http://www.w3.org/2000/svg"
      >
        <filter id="noise">
          <feTurbulence
            type="turbulence"
            baseFrequency="3"
            numOctaves="1"
            stitchTiles="stitch"
          />
        </filter>
        <rect width="100%" height="100%" filter="url(#noise)" />
      </svg>
      <div class="btn-1">
        <!-- 按键 -->
        <div class="btn-2"></div>
        <!-- 按键下层圆角矩形 -->
        <div class="btn-2__warpper">
          <!-- 按键白色部分 -->
          <div class="btn-2__body">
            <!-- 文字部分 -->
            <span>cmd</span>
            <span>⌘</span>
          </div>
        </div>
      </div>
    </div>
  </body>
  <script>
    // 按键按下
    const btnDown = () => {
      document.documentElement.style.setProperty('--btn-top', '17px')
    }
    // 按键抬起
    const btnUp = () => {
      document.documentElement.style.setProperty('--btn-top', '13px')
    }
  </script>
</html>
相关推荐
粥里有勺糖17 分钟前
安利一下最近用的桌面“Agent” | T3 Code
前端·github·ai编程
xy345323 分钟前
Axure 9.0 动态面板基本操作
前端·ui·html·axure·原型·产品设计
不停喝水31 分钟前
【前端转全栈java速通课】 项目实战④7-11节 操作数据库-登录-注册-修改密码-注销-mubatis-plus简化crud-
java·前端·数据库
一水鉴天1 小时前
统一对象拓扑(UOT)设计全链定稿:从三件事分离到三继承 之1 20261010(豆包“查看聊天内容”继续)
前端·css
栖凤2 小时前
Java 的 try-catch 在 Agent 里失效了,我用了 5 个模式才兜住
java·前端·python
粥里有勺糖2 小时前
视野修炼第136期 | 前端小恐龙"Deno"被收购
前端·github·ai编程
修炼的dance2 小时前
页面塞了13.88 MB字体,导出的PDF只有71 KB
前端
qq_369173632 小时前
如何将 AI 生成的 HTML 网页发布成在线链接?不用自己搭建服务器
前端·人工智能·html·效率工具·html 发布
风花一世月2 小时前
⚡ 我把一座含氢能源园区搬进了浏览器(五类能流实时算守恒,在线直接玩)
前端·人工智能
羲云2 小时前
Claude 越界事件之后:Agent 权限设计为什么不能只靠提示词
前端