前端例程20221024:流光效果按钮

演示

在这里插入图片描述

原理

  • 使用多色渐变作为背景,拉伸背景,这样默认就只显示其中一部分;
  • 通过移动背景位置来改变呈现部分,实现颜色变化效果;
  • 放置一个元素块在按钮背部,使用模糊来实现外发光效果;

代码

<!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;
        }

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

    <style>
        /* 全局色表 */
        :root {
            --color-1: #2486d8;
            --color-2: #f84077;
            --color-3: #fcd217;
        }

        button {
            width: 280px;
            height: 80px;
            border-radius: 40px;
            border: none;
            outline: none;
            color: white;
            font-size: 32px;
            text-align: center;
            line-height: 80px;
            letter-spacing: 4px;
            font-family: sans-serif;
            /* 渐变背景,首尾颜色要一样,以配合动画实现平滑过渡 */
            background: linear-gradient(to right, var(--color-1), var(--color-2), var(--color-3), var(--color-1));
            /* 拉长背景宽度*/
            background-size: 600% 100%;
        }

        button:hover {
            animation: anime 12s linear infinite;
        }

        /* 动画通过改变背景位置来改变显示出的颜色 */
        @keyframes anime {
            0% {
                background-position: 0%;
            }

            100% {
                background-position: -600% 0;
            }
        }

        button {
            position: relative;
        }

        /* ::before元素用户设置外发光效果 */
        button::before {
            content: '';
            position: absolute;
            z-index: -1;
            top: -5px;
            bottom: -5px;
            left: -5px;
            right: -5px;
            border-radius: 45px;
            background: linear-gradient(to right, var(--color-1), var(--color-2), var(--color-3), var(--color-1));
            background-color: #cad3c3;
            background-size: 600% 100%;
            filter: blur(15px);
            opacity: 0;
            transition: 1s;
        }

        button:hover::before {
            animation: anime 12s linear infinite;
            opacity: 1;
        }
    </style>
</head>

<body>
    <button>BUTTON</button>
</body>

</html>

更多例程

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

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Naisu Xu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值