• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

前端例程20221024流光效果按钮

武飞扬头像
Naisu Xu
帮助1

演示

学新通

原理

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

代码

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgehjhe
系列文章
更多 icon
同类精品
更多 icon
继续加载