gpt4 book ai didi

css - 如何制作对 Angular 圆边框渐变?

转载 作者:技术小花猫 更新时间:2023-10-29 11:06:06 25 4
gpt4 key购买 nike

我对 CSS3 有疑问。我不知道如何制作这样的对 Angular 圆形渐变边框: circle example

我找到了类似 this 的内容:

.box {
width: 250px;
height: 250px;
margin: auto;
background: #eee;
border: 20px solid transparent;
-moz-border-image: -moz-linear-gradient(top left, #3acfd5 0%, #3a4ed5 100%);
-webkit-border-image: -webkit-linear-gradient(top left, #3acfd5 0%, #3a4ed5 100%);
border-image: linear-gradient(to bottom right, #3acfd5 0%, #3a4ed5 100%);
border-image-slice: 1;
}
<div class="box"></div>

但不幸的是,这仅适用于正方形。

如有任何帮助,我们将不胜感激。

最佳答案

圆锥形渐变是沿着围绕中心的圆弧的渐变。这就是我们在色轮中看到的。正如 Paulie_D 所指出的,这些目前在 CSS 中是不可能的,但是 Lea Verou has developed a polyfill for it .

话虽如此,您正在寻找的似乎不是圆锥形渐变,它是正常 Angular 线性渐变,但仅应用于边框。这不能通过 CSS border-image 属性来实现,因为它是按照 specs 的方式工作的。 .

A box's backgrounds, but not its border-image, are clipped to the appropriate curve


如果圆的中心部分是纯色,则可以使用 Vitorino 的回答中提到的方法。如果它不是纯色(即页面背景是渐变或需要显示的图像)则无济于事。以下方法可用于这种情况。

使用 mask 图像:

此方法使用圆形蒙版图像来蒙版圆的内部。这使得它看起来好像只有边框应用了渐变。 缺点是此功能目前仅在支持 Webkit 的浏览器中受支持

.border-gradient-mask {
height: 200px;
width: 200px;
border-radius: 50%;
background-image: linear-gradient(to bottom left, #7B73A4 0%, #150E5E 100%);
-webkit-mask-image: radial-gradient(circle at center, transparent 57%, white 58%);
mask-image: radial-gradient(circle at center, transparent 57%, white 58%);
}
body {
background: radial-gradient(circle at center, sandybrown, chocolate);
}
<div class="border-gradient-mask"></div>


使用 SVG 形状或蒙版:

另一种方法是使用 SVG circle 元素创建圆,然后将渐变分配给 stroke 属性。渐变还应用了 gradientTransform,因为这是使用 SVG 生成 Angular 线性渐变的唯一方法。

.border-gradient-svg {
position: relative;
height: 200px;
width: 200px;
border-radius: 50%;
}
.border-gradient-svg svg {
position: absolute;
top: 0px;
left: 0px;
height: 100%;
width: 100%;
}
.border-gradient-svg circle {
fill: transparent;
stroke: url(#grad);
stroke-width: 8;
}
body {
background: radial-gradient(circle at center, sandybrown, chocolate);
}
<div class="border-gradient-svg">
<svg viewBox="0 0 100 100">
<defs>
<linearGradient id="grad" gradientUnits="objectBoundingBox" gradientTransform="rotate(135 0.5 0.5)">
<stop offset="0%" stop-color="#7B73A4" />
<stop offset="100%" stop-color="#150E5E" />
</linearGradient>
</defs>
<circle r="46" cx="50" cy="50" />
</svg>
</div>

同样可以通过使用 SVG mask 来实现。所需要做的就是创建一个包含两个 circle 元素的蒙版,用白色填充较大的圆圈,用黑色填充较小的圆圈,然后将蒙版应用于我们原来的 circle 元素。小圆圈(黑色填充)占据的区域将是透明的。

.border-gradient-svg {
position: relative;
height: 200px;
width: 200px;
border-radius: 50%;
}
.border-gradient-svg svg {
position: absolute;
top: 0px;
left: 0px;
height: 100%;
width: 100%;
}
.border-gradient-svg .grad-border {
fill: url(#grad);
mask: url(#masker);
}
body {
background: radial-gradient(circle at center, sandybrown, chocolate);
}
<div class="border-gradient-svg">
<svg viewBox="0 0 100 100">
<defs>
<linearGradient id="grad" gradientUnits="objectBoundingBox" gradientTransform="rotate(135 0.5 0.5)">
<stop offset="0%" stop-color="#7B73A4" />
<stop offset="100%" stop-color="#150E5E" />
</linearGradient>
<mask id="masker" x="0" y="0" width="100" height="100">
<circle r="50" cx="50" cy="50" fill="#fff" />
<circle r="42" cx="50" cy="50" fill="#000" />
</mask>
</defs>
<circle r="50" cx="50" cy="50" class="grad-border"/>
</svg>
</div>


使用剪辑路径:

创建它的另一种方法是使用 clip-path(带有内联 SVG)和 clip-rule set to evenodd .剪辑路径解决方案优于其他解决方案的优势是,这只会在悬停在填充区域(而不是透明区域)上时触发悬停效果。 缺点是 IE 不支持剪辑路径(即使是 SVG)。

.border-gradient-clip {
height: 200px;
width: 200px;
border-radius: 50%;
background-image: linear-gradient(to bottom left, #7B73A4 0%, #150E5E 100%);
-webkit-clip-path: url(#clipper);
clip-path: url(#clipper);
}
body {
background: radial-gradient(circle at center, sandybrown, chocolate);
}
<svg width="0" height="0">
<defs>
<clipPath id="clipper" clipPathUnits="objectBoundingBox">
<path d="M0,0.5 a0.5,0.5 0 1,0 1,0 a0.5,0.5 0 1,0 -1,0z M0.08,0.5 a0.42,0.42 0 1,0 0.84,0 a0.42,0.42 0 1,0 -0.84,0z" clip-rule="evenodd" />
</clipPath>
</defs>
</svg>
<div class="border-gradient-clip"></div>

关于css - 如何制作对 Angular 圆边框渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com