前端例程20220818:边框跑马霓虹灯效按钮

演示

在这里插入图片描述

原理

  • 按钮使用阴影实现外发光效果;
  • 按钮设置倒影效果;
  • 使用四个块元素以按钮为基础绝对定位到上下左右四边作为边框;
  • 通过给边框元素设置动画,并设置动画时间差以实现边框跑马效果;

代码

<!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;
            box-sizing: border-box;
        }

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

    <style>
        /* 全局色表 */
        :root {
            --color-bg: #202020;
            --color-btn1: #f556e0;
            --color-btn2: #5182f7;
        }

        body {
            display: flex;
            background: var(--color-bg);
            align-items: center;
            justify-content: center;
        }

        .btn {
            width: 200px;
            height: 50px;
            margin: 20px;
            background: transparent;
            border: none;
            font-size: 24px;
            letter-spacing: 4px;
            transition: 0.3s;
            /* 倒影。通过遮罩透明度变化实现近处明亮远处黯淡的效果 */
            -webkit-box-reflect: below 1px linear-gradient(to bottom, transparent, rgba(0, 0, 0, 0.3));
        }

        .btn:nth-child(1) {
            color: var(--color-btn1);
        }

        .btn:nth-child(2) {
            color: var(--color-btn2);
        }

        .btn:focus {
            outline: none;
        }
    </style>

    <!-- 按钮悬停样式 -->
    <style>
        .btn:hover {
            color: var(--color-bg);
        }

        .btn:nth-child(1):hover {
            background: var(--color-btn1);
            /* 多层阴影增加发光效果的层次感 */
            box-shadow: 0 0 10px var(--color-btn1),
                0 0 25px var(--color-btn1),
                0 0 50px var(--color-btn1),
                0 0 100px var(--color-btn1);
        }

        .btn:nth-child(2):hover {
            background: var(--color-btn2);
            box-shadow: 0 0 10px var(--color-btn2),
                0 0 25px var(--color-btn2),
                0 0 50px var(--color-btn2),
                0 0 100px var(--color-btn2);
        }
    </style>

    <!-- 边框跑马样式 -->
    <style>
        .btn {
            position: relative;
            overflow: hidden;
        }

        .btn span {
            position: absolute;
            display: block;
        }

        /* 下面是各条边框位置与动画设置,通过位置和动画时间差实现跑马效果 */

        .btn span:nth-child(1) {
            top: 0;
            left: -100%;
            width: 100%;
            height: 2px;
            animation: animate1 1s linear infinite;
            animation-delay: 0s;
        }

        @keyframes animate1 {
            0% {
                left: -100%;
            }

            50%,
            100% {
                left: 100%;
            }
        }

        .btn span:nth-child(2) {
            top: -100%;
            right: 0;
            width: 2px;
            height: 100%;
            animation: animate2 1s linear infinite;
            animation-delay: 0.25s;
        }

        @keyframes animate2 {
            0% {
                top: -100%;
            }

            50%,
            100% {
                top: 100%;
            }
        }

        .btn span:nth-child(3) {
            bottom: 0;
            right: -100%;
            width: 100%;
            height: 2px;
            animation: animate3 1s linear infinite;
            animation-delay: 0.5s;
        }

        @keyframes animate3 {
            0% {
                right: -100%;
            }

            50%,
            100% {
                right: 100%;
            }
        }

        .btn span:nth-child(4) {
            bottom: -100%;
            left: 0;
            width: 2px;
            height: 100%;
            animation: animate4 1s linear infinite;
            animation-delay: 0.75s;
        }

        @keyframes animate4 {
            0% {
                bottom: -100%;
            }

            50%,
            100% {
                bottom: 100%;
            }
        }

        /* 下面是各条边框颜色设置 */

        .btn:nth-child(1) span:nth-child(1) {
            background: linear-gradient(to right, transparent, var(--color-btn1));
        }

        .btn:nth-child(2) span:nth-child(1) {
            background: linear-gradient(to right, transparent, var(--color-btn2));
        }

        .btn:nth-child(1) span:nth-child(2) {
            background: linear-gradient(to bottom, transparent, var(--color-btn1));
        }

        .btn:nth-child(2) span:nth-child(2) {
            background: linear-gradient(to bottom, transparent, var(--color-btn2));
        }

        .btn:nth-child(1) span:nth-child(3) {
            background: linear-gradient(to left, transparent, var(--color-btn1));
        }

        .btn:nth-child(2) span:nth-child(3) {
            background: linear-gradient(to left, transparent, var(--color-btn2));
        }

        .btn:nth-child(1) span:nth-child(4) {
            background: linear-gradient(to top, transparent, var(--color-btn1));
        }

        .btn:nth-child(2) span:nth-child(4) {
            background: linear-gradient(to top, transparent, var(--color-btn2));
        }
    </style>
</head>

<body>
    <!-- button中四个span元素作为上下左右四条流动的边框 -->
    <button class="btn">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        BUTTON
    </button>
    <button class="btn">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        BUTTON
    </button>
</body>

</html>

更多例程

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Naisu Xu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值