如何为CSS绘制的三角形添加渐变效果?
如何为通过border绘制的三角形应用渐变颜色?
当前实现三角形的代码片段:
.spinner::before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 0; height: 0; border-top: 20px solid transparent; border-bottom: 20px solid transparent; border-left: 27px solid #B76BF0; transform: translateX(4px); z-index: 0; }
需要修改以下完整代码来实现渐变效果:
原始完整代码
CSS
.spinner { -webkit-appearance: none; appearance: none; border: none; padding: 0; display: block; cursor: pointer; position: fixed; left: 0; top: 0; bottom: 0; right: 0; margin: auto; box-sizing: border-box; background-clip: padding-box; width: 90px; height: 90px; border-radius: 50%; transform-origin: 50% 55%; transform: perspective(90px) rotateX(30deg); animation: spinner-wiggle 5.2s infinite; } .spinner::before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 0; height: 0; border-top: 20px solid transparent; border-bottom: 20px solid transparent; border-left: 27px solid #B76BF0; transform: translateX(4px); z-index: 0; } @keyframes spinner-wiggle { 30% { transform: perspective(90px) rotateX(10deg); } 40% { transform: perspective(90px) rotateX(7deg); } 50% { transform: perspective(90px) rotateX(10deg); } 60% { transform: perspective(90px) rotateX(8deg); } } .color-circle { position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 90px; height: 90px; transform-origin: 50% 50%; background: linear-gradient(100deg, #24A4EA, #379DEB 25%, #B76BF0); border-radius: 50%; z-index: 0; box-shadow: inset 0 24px 18px -10px rgba(0, 0, 140, 0.3), inset 0 0 22px -2px rgba(0, 0, 0, 0.4); cursor: pointer; -webkit-mask-image: radial-gradient(circle, transparent 0 36px, black 36px); mask-image: radial-gradient(circle, transparent 0 50px, black 20px); } .spinner:hover .color-circle { background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, rgba(34, 204, 0, 0.9)), color-stop(100%, #55ff00)); animation: spin 1.7s infinite linear; } .color-circle:before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 72px; height: 72px; border-radius: 50%; background-color: #353198; z-index: 0; } @-webkit-keyframes spin { 0% { transform: rotateZ(0deg); } 100% { transform: rotateZ(360deg); } }
HTML
<div class=" spinner"> <span class="color-circle"></span> </div>
解决方案
CSS的border属性无法直接设置渐变背景,我们可以用背景渐变+形状裁剪的方式替代原border实现,得到渐变三角形:
修改.spinner::before的代码如下:
.spinner::before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 27px; /* 对应原border-left的宽度 */ height: 40px; /* 对应原border-top + border-bottom的总高度 */ background: linear-gradient(100deg, #24A4EA, #B76BF0); /* 和color-circle一致的渐变 */ clip-path: polygon(0 0, 100% 50%, 0 100%); /* 裁剪出向左的三角形 */ transform: translateX(4px); z-index: 0; }
原理说明:
- 用矩形元素替代原border绘制:宽度设为原border-left的27px,高度设为上下border的总高度40px(20px+20px)。
- 给矩形设置需要的渐变背景,这里和
.color-circle的渐变保持一致。 - 通过
clip-path: polygon(0 0, 100% 50%, 0 100%)将矩形裁剪成向左的三角形,和原border实现的形状完全匹配。
修改后的完整代码
CSS
.spinner { -webkit-appearance: none; appearance: none; border: none; padding: 0; display: block; cursor: pointer; position: fixed; left: 0; top: 0; bottom: 0; right: 0; margin: auto; box-sizing: border-box; background-clip: padding-box; width: 90px; height: 90px; border-radius: 50%; transform-origin: 50% 55%; transform: perspective(90px) rotateX(30deg); animation: spinner-wiggle 5.2s infinite; } .spinner::before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 27px; height: 40px; background: linear-gradient(100deg, #24A4EA, #B76BF0); clip-path: polygon(0 0, 100% 50%, 0 100%); transform: translateX(4px); z-index: 0; } @keyframes spinner-wiggle { 30% { transform: perspective(90px) rotateX(10deg); } 40% { transform: perspective(90px) rotateX(7deg); } 50% { transform: perspective(90px) rotateX(10deg); } 60% { transform: perspective(90px) rotateX(8deg); } } .color-circle { position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 90px; height: 90px; transform-origin: 50% 50%; background: linear-gradient(100deg, #24A4EA, #379DEB 25%, #B76BF0); border-radius: 50%; z-index: 0; box-shadow: inset 0 24px 18px -10px rgba(0, 0, 140, 0.3), inset 0 0 22px -2px rgba(0, 0, 0, 0.4); cursor: pointer; -webkit-mask-image: radial-gradient(circle, transparent 0 36px, black 36px); mask-image: radial-gradient(circle, transparent 0 50px, black 20px); } .spinner:hover .color-circle { background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, rgba(34, 204, 0, 0.9)), color-stop(100%, #55ff00)); animation: spin 1.7s infinite linear; } .color-circle:before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; width: 72px; height: 72px; border-radius: 50%; background-color: #353198; z-index: 0; } @-webkit-keyframes spin { 0% { transform: rotateZ(0deg); } 100% { transform: rotateZ(360deg); } }
HTML
<div class=" spinner"> <span class="color-circle"></span> </div>
内容的提问来源于stack exchange,提问作者user17631451
相关产品推荐
相关产品推荐

