如何使用CSS实现锥形渐变作为border-image的动画效果?
锥形渐变border-image动画实现记录
我正在尝试使用CSS将锥形渐变设置为border-image并添加动画效果。最初参考的基础模板通过自定义属性旋转border-image实现动画,对应代码如下:
div { --angle: 0deg; width: 50vmin; height: 50vmin; border: 10vmin solid; border-image: conic-gradient(from var(--angle), red, yellow, lime, aqua, blue, magenta, red) 1; animation: 10s rotate linear infinite; } @keyframes rotate { to { --angle: 360deg; } } @property --angle { syntax: '<angle>'; initial-value: 0deg; inherits: false; }
我尝试直接对渐变本身做动画但没有成功,我期望实现的效果对应代码如下:
div { --angle: 0%; width: 50vmin; height: 50vmin; border: 10vmin solid; border-image: conic-gradient(from 0deg at 50% 50%, rgba(0, 154, 177, 0) 0%, rgba(0, 154, 177, 0) var(--angle), rgba(0, 154, 177, 1) var(--angle), rgba(0, 154, 177, 1) 100%, rgba(0, 154, 177, 0) 100%, rgba(0, 154, 177, 0) 100%) 1; animation: 10s rotate linear infinite; } @keyframes rotate { from { --angle: 0%; } to { --angle: 25%; } } @property --angle { syntax: '<angle>'; initial-value:0%; inherits: false; }
[更新]
最终我采用clip-path方案实现了需求,效果代码如下:
CSS部分:
div { padding: 10px; } a{ position:relative; text-decoration: none; padding: 10px 20px; background-color: transparent; transition: background-color .5s ease-in; } a:after{ content:""; position:absolute; border:1px solid black; top:0; left:0; width:100%; height:100%; box-sizing: border-box; } a:hover{ background-color: rgba(0,0,0,.2); } a:hover:after{ animation: .8s clipping-border ease-in forwards; } * { box-sizing: border-box; } @keyframes clipping-border { from { clip-path:polygon(0% 100%, 100% 100%,100% 0%,50% 0%,50% 50%,50% 0%,0% 0%); } 40% { clip-path:polygon(0% 100%, 100% 100%,100% 0%,100% 0%,50% 50%,0% 0%,0% 0%); } 40.1% { clip-path:polygon(0% 100%, 100% 100%,100% 0%,50% 50%,50% 50%,50% 50%,0% 0%); } 60% { clip-path:polygon(0% 100%, 100% 100%,100% 100%,50% 50%,50% 50%,50% 50%,0% 100%); } 60.1% { clip-path:polygon(0% 100%, 100% 100%,50% 50%,50% 50%,50% 50%,50% 50%,50% 50%); } 62% { clip-path:polygon(5% 100%, 95% 100%,95% 50%,95% 50%,5% 50%,5% 50%,5% 50%); border-width: 1px; } to { clip-path:polygon(5% 100%, 95% 100%,95% 50%,95% 50%,5% 50%,5% 50%,5% 50%); border-width: 5px; } }
HTML部分:
<div> <a href="#">Some button</a> </div>
内容的提问来源于stack exchange,提问作者Richard Burkhardt
相关产品推荐
相关产品推荐

