【问题标题】:CSS animate a conical gradient as border imageCSS动画一个圆锥渐变作为边框图像
【发布时间】:2021-05-31 12:24:54
【问题描述】:

我正在尝试使用 css 作为边框图像为锥形渐变设置动画。我使用code pan from another developer 作为样板并尝试更改它。他的动画是使用自定义属性来旋转边框图像:

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

我尝试animate the actual gradien which did not work。我想以某种方式得到这样的工作:

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

[更新]

通过使用剪辑路径选项,我现在的结果是:

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;
  }
}
<div>
    <a href="#">Some button</a>
</div>

【问题讨论】:

  • 你在找什么样的动画?
  • 其他开发者的第一个动画甚至在 Mac Firefox 中都不起作用。
  • @TemaniAfif 我试图让边框消失。但以一种通用的方式。我只想向锚添加一个类,锚应该成为一个轮廓按钮。有点像这样:codepen.io/seanmccaffery/pen/xBpbG
  • @RickardElimää 在 firefox 上不支持 @property

标签: css css-animations css-variables


【解决方案1】:

如下所示更新您的代码。选择正确的类型很重要,您使用的是百分比而不是角度:

div {
  --angle: 0%;
  width: 50vmin;
  height: 50vmin;
  border: 10vmin solid;
  border-image: conic-gradient(transparent var(--angle), rgba(0, 154, 177, 1) 0 100%) 1;
  animation: 10s rotate linear infinite;
}

@keyframes rotate {
  to {
    --angle: 100%;
  }
}

@property --angle {
  syntax: '<percentage>'; /* this one is important */
  initial-value: 0%;
  inherits: false;
}

input[type="checkbox"] {
  position: fixed;
  top: 1em;
  left: 1em;
}

input[type="checkbox"]:after {
  content: 'Toggle Fill';
  white-space: nowrap;
  padding-left: 1.5em;
}

input[type="checkbox"]:checked+div {
  border-image-slice: 1 fill;
}

body {
  margin: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  display: grid;
  place-items: center;
  background: #fff;
}

footer {
  text-align: center;
  font-style: italic;
}
<input type="checkbox" />
<div></div>

或者像这样:

div {
  --angle: 0deg;
  width: 50vmin;
  height: 50vmin;
  border: 10vmin solid;
  border-image: conic-gradient(from calc(-1*var(--angle)),transparent calc(2*var(--angle)), rgba(0, 154, 177, 1) 0 100%) 1;
  animation: 10s rotate linear infinite;
}

@keyframes rotate {
  to {
    --angle: 180deg;
  }
}

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

input[type="checkbox"] {
  position: fixed;
  top: 1em;
  left: 1em;
}

input[type="checkbox"]:after {
  content: 'Toggle Fill';
  white-space: nowrap;
  padding-left: 1.5em;
}

input[type="checkbox"]:checked+div {
  border-image-slice: 1 fill;
}

body {
  margin: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  display: grid;
  place-items: center;
  background: #fff;
}

footer {
  text-align: center;
  font-style: italic;
}
<input type="checkbox" />
<div></div>

另一个使用clip-path没有CSS变量的想法:

div {
  width: 50vmin;
  height: 50vmin;
  position:relative;
}
div::before {
  content:"";
  position:absolute;
  inset:-10vmin;
  padding: 10vmin;
  background:rgba(0, 154, 177, 1);
  -webkit-mask:
    linear-gradient(#000  0 0) content-box,
    linear-gradient(#000  0 0);
   -webkit-mask-composite:destination-out;
   mask-composite:exclude;
  animation: 5s rotate linear infinite;
  clip-path:polygon(0 0, 50% 0,50% 50%,50% 0,100% 0,100% 100%,0 100%);  
}

@keyframes rotate {
  20% {
    clip-path:polygon(0 0, 0 0,50% 50%,100% 0,100% 0,100% 100%,0 100%);  
  }
  75% {
    clip-path:polygon(0 100%, 0 100%,50% 50%,100% 100%,100% 100%,100% 100%,0 100%);  
  }
  100% {
    clip-path:polygon(50% 100%, 50% 100%,50% 50%,50% 100%,50% 100%,50% 100%,50% 100%);  
  }
}

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

input[type="checkbox"] {
  position: fixed;
  top: 1em;
  left: 1em;
}

input[type="checkbox"]:after {
  content: 'Toggle Fill';
  white-space: nowrap;
  padding-left: 1.5em;
}

input[type="checkbox"]:checked+div:before {
  -webkit-mask:none;
}

body {
  margin: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  display: grid;
  place-items: center;
  background: #fff;
}
<input type="checkbox" />
<div></div>

【讨论】:

  • @RichardBurkhardt 更新了另一个演示。在你的代码中,variabee 的类型是错误的,你的渐变有点复杂,所以我优化了
  • 你有没有办法在没有财产登记的情况下完成这项工作?
  • @RichardBurkhardt 是第一个还是最后一个例子?
  • 最后一个例子
  • @RichardBurkhardt 好的,将使用不带 CSS 变量的示例进行更新
猜你喜欢
  • 2022-10-15
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多