前端例程20220815:拟物风格复选按钮

演示

在这里插入图片描述

原理

本文要实现的按钮大致示意如下:
在这里插入图片描述
观察者从正上方观看,写代码时主要处理光照以及近大远小等现象。

代码

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>拟物风格复选按钮</title>

    <style>
        * {
            padding: 0;
            margin: 0;
            user-select: none;
        }

        html,
        body {
            height: 100vh;
        }
    </style>

    <style>
        body {
            display: flex;
            background-color: #181818;
            align-items: center;
            justify-content: center;
        }

        /* 按钮区域 */
        .chk {
            position: relative;
            height: 100px;
            width: 100px;
            border-radius: 50%;
            background: #000000;
            box-shadow: 0 0 0 2px rgba(0, 0, 0, 1);
        }

        /* 隐藏默认checkbox */
        .chk>input {
            appearance: none;
        }

        /* 按钮本身 */
        .chk>span {
            position: absolute;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background: #222222;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 1),
                inset 0 -2px 4px rgba(0, 0, 0, 1),
                inset 0 2px 4px rgba(255, 255, 255, 0.5);
            transition: 0.2s;
        }

        /* 按钮按下后因高度下降造成的缩放 */
        .chk>input:checked~span {
            box-shadow: 0 1px 4px rgba(0, 0, 0, 1),
                inset 0 -1px 2px rgba(0, 0, 0, 1),
                inset 0 1px 2px rgba(255, 255, 255, 0.5);
            transform: scale(0.95);
            transition: 0.2s;
        }

        /* 按钮中间图形 */
        .chk>svg {
            position: absolute;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            stroke: #111111;
            stroke-width: 6px;
            stroke-linecap: round;
            transition: 0.2s;
            transition-delay: 0.1s;
        }

        /* 按钮按下后中间图形点亮 */
        .chk>input:checked~svg {
            stroke: #00ffff;
            filter: drop-shadow(0 0 12px #00ffff);
            transform: scale(0.95);
            transition: 0.2s;
            transition-delay: 0.1s;
        }
    </style>
</head>

<body>
    <!-- 使用label包裹,可以使内部所有元素和checkbox联动 -->
    <label class="chk">
        <input type="checkbox">
        <span></span>
        <svg id="btn-more" viewBox="0 0 100 100">
            <line x1="50" y1="25" x2="50" y2="50" />
            <circle cx="8" cy="70" r="20" fill="none" stroke-dasharray="90" transform="rotate(-39)" />
        </svg>
    </label>
</body>

</html>

更多例程

更多例程可以参考下面代码仓库:
https://github.com/NaisuXu/front-end-web-examples

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Naisu Xu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值