background-image按钮渐变边框

51 阅读1分钟

直接上代码

<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body {
        padding: 20px;
        background-color: #574845;
    }
    
     #btn {
            width: 104px;
            height: 40px;
            border-radius: 20px;
            line-height: 32px;
            text-align: center;
            color: #d0d215;
            cursor: pointer;
            border: 4px solid transparent;
            background-image: linear-gradient(rgba(0, 0, 0, 0.5), black), linear-gradient(var(--deg), #ffbd13, #7000ff);
            background-clip: content-box, border-box;
            background-origin: content-box, border-box;
            animation: rotateGradient 2s linear infinite;
            animation-play-state: paused;

            &:hover {
                color: rgb(230, 255, 71);
                animation-play-state: running;
            }
        }

        @property --deg {
            syntax: '<angle>';
            inherits: false;
            initial-value: 0deg;
        }

        @keyframes rotateGradient {
            0% {
                --deg: 0deg;
            }

            100% {
                --deg: 360deg;
            }
        }
</style>
<body>
    <div id="btn">按钮</div>
</body>

效果图

线性渐变边框.gif

实现思路

其实边框都是假象,实现原理就是用渐变画两个圆,用小圆把大圆中间给掏空,大圆剩下的这一圈就是所谓的边框了。 然后我们在把渐变给旋转起来就搞定了。