【问题标题】:Make border-image round使边框图像变圆
【发布时间】:2021-04-13 12:53:07
【问题描述】:

我正在尝试设计一个具有自定义边框效果的圆形按钮。这是我到目前为止得到的:

body {
  display: flex;
  height: 100vh;
  overflow: hidden;
  justify-content: center;
  align-items: center;
}
button {
  height: 80px;
  width: 80px;
  border-radius: 50%;
  border-image: url("data:image/svg+xml;charset=utf-8,%3Csvg width='100' height='100' viewBox='0 0 100 100' fill='none' xmlns='http://www.w3.org/2000/svg'%3E %3Cstyle%3Epath%7Banimation:stroke 5s infinite linear%3B%7D%40keyframes stroke%7Bto%7Bstroke-dashoffset:776%3B%7D%7D%3C/style%3E%3ClinearGradient id='g' x1='0%25' y1='0%25' x2='0%25' y2='100%25'%3E%3Cstop offset='0%25' stop-color='%232d3561' /%3E%3Cstop offset='25%25' stop-color='%23c05c7e' /%3E%3Cstop offset='50%25' stop-color='%23f3826f' /%3E%3Cstop offset='100%25' stop-color='%23ffb961' /%3E%3C/linearGradient%3E %3Cpath d='M1.5 1.5 l97 0l0 97l-97 0 l0 -97' stroke-linecap='square' stroke='url(%23g)' stroke-width='3' stroke-dasharray='388'/%3E %3C/svg%3E") 1;
}
<button>TEST</button>

但我也可以制作边框图像圆圈吗?这可能吗?

【问题讨论】:

标签: html css svg


【解决方案1】:

把它做成一个圆形元素,然后简单地用它作为背景:

body {
  display: flex;
  height: 100vh;
  overflow: hidden;
  justify-content: center;
  align-items: center;
}
button {
  height: 80px;
  width: 80px;
  border-radius: 50%;
  border:5px solid transparent;
  background: url('data:image/svg+xml;charset=utf-8,%3Csvg width="100" height="100" viewBox="0 0 100 100" fill="none" xmlns="http://www.w3.org/2000/svg"%3E%3Cstyle%3E circle %7B animation: stroke 5s infinite linear; %7D @keyframes stroke %7B to %7B stroke-dashoffset: 600; %7D %7D %3C/style%3E%3ClinearGradient id="g" x1="0%25" y1="0%25" x2="0%25" y2="100%25"%3E%3Cstop offset="0%25" stop-color="%232d3561" /%3E%3Cstop offset="25%25" stop-color="%23c05c7e" /%3E%3Cstop offset="50%25" stop-color="%23f3826f" /%3E%3Cstop offset="100%25" stop-color="%23ffb961" /%3E%3C/linearGradient%3E%3Ccircle class="circle" cx="50" cy="50" r="45" fill="transparent" stroke="url(%23g)" stroke-width="3" stroke-dasharray="300" /%3E%3C/svg%3E') center/100% 100% border-box,
  #f2f2f2 padding-box;
}
<button>TEST</button>

这是我使用的 SVG:

<svg width='100' height='100' viewBox='0 0 100 100' fill='none' xmlns='http://www.w3.org/2000/svg'>
  <style>
    circle {
      animation: stroke 5s infinite linear;
    }

    @keyframes stroke {
      to {
        stroke-dashoffset: 600;
      }
    }

  </style>
  <linearGradient id='g' x1='0%' y1='0%' x2='0%' y2='100%'>
    <stop offset='0%' stop-color='#2d3561' />
    <stop offset='25%' stop-color='#c05c7e' />
    <stop offset='50%' stop-color='#f3826f' />
    <stop offset='100%' stop-color='#ffb961' />
  </linearGradient>
  
  <circle class="circle" cx="50" cy="50" r="45" fill="transparent" stroke='url(#g)' stroke-width="3" stroke-dasharray='300' />
</svg>

【讨论】:

  • 所以你制作了 SVG 圆,这是主要的事情。你能解释一下你是怎么做到的吗?
  • 非常好!就一个问题。有没有可能做出这样的效果:codepen.io/AlexWarnes/pen/jXYYKL,第一个 GRADIENT SPINNER。
  • @HassenCh。我只是使用circle 而不是path
  • @Camilla 你不需要 SVG,检查一下:jsfiddle.net/pmL1k0co/1
猜你喜欢
  • 2021-08-28
  • 2016-05-07
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 2023-03-18
  • 2017-10-31
  • 2014-12-06
  • 2011-10-27
相关资源
最近更新 更多