569239602 发表于 2022-7-13 14:45:54

使用 CSS 轻松实现一些高频出现的奇形怪状按钮

<p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">怎么样使用 CSS 实现一个内切角按钮呢、怎么样实现一个带箭头的按钮呢?</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">本文基于一些高频出现在设计稿中的,使用 CSS 实现稍微有点难度和技巧性的按钮,讲解使用 CSS 如何尽可能的实现它们。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">先让我们来看看这些经常会出现的按钮形状:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p><strong>矩形与圆角按钮</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">正常而言,我们遇到的按钮就这两种 -- 矩形和圆角:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">它们非常的简单,宽高和圆角和背景色。</span></span></span></p><blockquote>&lt;div class=btn rect&gt;rect&lt;/div&gt;<br/>&nbsp; &nbsp; &lt;div class=btn circle&gt;circle&lt;/div&gt;</blockquote><blockquote>.btn {<br/>&nbsp; &nbsp; margin: 8px auto;<br/>&nbsp; &nbsp; flex-shrink: 0;<br/>&nbsp; &nbsp; width: 160px;<br/>&nbsp; &nbsp; height: 64px;<br/>}<br/>.rect {<br/>&nbsp; &nbsp; background: #f6ed8d;<br/>}<br/><br/>.circle {<br/>&nbsp; &nbsp; border-radius: 64px;<br/>&nbsp; &nbsp; background: #7de3c8;<br/>}</blockquote><p><strong>梯形与平行四边形</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">接下来,基于矩形的变形,经常会出现梯形与平行四边形的按钮。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">实现它们主要使用 transform 即可,但是要注意一点,使用了 transform 之后,标签内的文字也会同样的变形,所以,我们通常使用元素的伪元素去实现造型,这样可以做到不影响按钮内的文字。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">平行四边形</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">使用 transform: skewX() 即可,注意上述说的,利用元素的伪元素实现平行四边形,做到不影响内部的文字。</span></span></span></p><blockquote>&lt;div class=btn parallelogram&gt;Parallelogram&lt;/div&gt;</blockquote><blockquote>.parallelogram {<br/>&nbsp; &nbsp; position: relative;<br/>&nbsp; &nbsp; width: 160px;<br/>&nbsp; &nbsp; height: 64px;<br/><br/>&nbsp; &nbsp; &::before {<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;content: ;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;position: absolute;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;top: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;left: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;bottom: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;right: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;background: #03f463;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;transform: skewX(15deg);<br/>&nbsp; &nbsp; }<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">如果不想使用伪元素,除了 transform: skewX(),平行四边形使用渐变也是可以实现的。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">大概就是这样:</span></span></span></p><blockquote>{<br/>&nbsp; &nbsp; background: linear-gradient(45deg, transparent 22%, #04e6fb 22%, #9006fb 78%, transparent 0);<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">梯形</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">梯形比平行四边形稍微复杂一点,它多借助了 perspective,其实是利用了一定的 3D 变换。原理就是一个矩形,绕着 X 轴旋转,像是这样:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">使用 perspective 和 transform: rotateX() 即可,当然,它们可以合在一起写:</span></span></span></p><blockquote>&lt;div class=btn trapezoid&gt;Trapezoid&lt;/div&gt;</blockquote><blockquote>.parallelogram {<br/>&nbsp; &nbsp; position: relative;<br/>&nbsp; &nbsp; width: 160px;<br/>&nbsp; &nbsp; height: 64px;<br/><br/>&nbsp; &nbsp; &::after {<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; content:;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; position: absolute;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; top: 0; right: 0; bottom: 0; left: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; transform: perspective(40px) rotateX(10deg);<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; transform-origin: bottom;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; background: #ff9800;<br/>&nbsp; &nbsp; }<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p><strong>切角 -- 纯色背景与渐变色背景</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">接下来是切角图形,最常见的方法主要是借助渐变 linear-gradient 实现,来看这样一个图形</span></span></span></p><blockquote>&lt;div&gt;&lt;/div&gt;</blockquote><blockquote>.notching {<br/>&nbsp; &nbsp; background: linear-gradient(135deg, transparent 10px, #ff1493 0);<br/>&nbsp; &nbsp; background-repeat: no-repeat;<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">结果如下,</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">基于此,我们只需要利用多重渐变,实现 4 个这样的图形即可,并且,利用 background-position 定位到四个角:</span></span></span></p><blockquote>&lt;div class=notching&gt;notching&lt;/div&gt;</blockquote><blockquote>.notching {<br/>&nbsp; &nbsp; background: <br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;linear-gradient(135deg, transparent 10px, #ff1493 0) top left,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;linear-gradient(-135deg, transparent 10px, #ff1493 0) top right,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;linear-gradient(-45deg, transparent 10px, #ff1493 0) bottom right,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;linear-gradient(45deg, transparent 10px, #ff1493 0) bottom left;<br/>&nbsp; &nbsp; background-size: 50% 50%;<br/>&nbsp; &nbsp; background-repeat: no-repeat;<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p><strong>利用 clip-path 实现渐变背景的切角图形</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">当然,这个技巧有个问题,当要求底色是渐变色的时候,这个方法就比较笨拙了。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">好在,我们还有另外一种方式,借助 clip-path 切出一个切角图形,这样,背景色可以是任意定制的颜色,无论是渐变还是纯色都不在话下:</span></span></span></p><blockquote>&lt;div class=clip-notching&gt;notching&lt;/div&gt;</blockquote><blockquote>.clip-notching {<br/>&nbsp; &nbsp; background: linear-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;45deg,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#f9d9e7,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#ff1493<br/>&nbsp; &nbsp; );<br/>&nbsp; &nbsp; clip-path: polygon(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;15px 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;calc(100% - 15px) 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;100% 15px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;100% calc(100% - 15px),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;calc(100% - 15px) 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;15px 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0 calc(100% - 15px),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0 15px<br/>&nbsp; &nbsp; );<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">简单的实现一个渐变背景,接着核心就是,在渐变矩形图形的基础上,利用 clip-path: polygon() 切出我们想要的形状(一个 8 边形):</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">当然,上述代码非常容易联想到下述这种 6 边形,使用渐变和 clip-path 都可以轻松得到:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p><strong>箭头按钮</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">接下来是箭头按钮,仔细观察上面的切角按钮,当两边的角被切掉的足够多的时候,就变成了一个箭头的形状。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">我们可以利用两重渐变,实现一个单箭头按钮:</span></span></span></p><blockquote>&lt;div class=arrow&gt;arrow&lt;/div&gt;</blockquote><blockquote>&.arrow {<br/>&nbsp; &nbsp; background: linear-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; -135deg,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; transparent 22px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #04e6fb 22px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #65ff9a 100%<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;)<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;top right,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;linear-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; -45deg,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; transparent 22px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #04e6fb 22px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #65ff9a 100%<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;)<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;bottom right;<br/>&nbsp; &nbsp; background-size: 100% 50%;<br/>&nbsp; &nbsp; background-repeat: no-repeat;<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">一个箭头就出来了:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">它是由上下两个渐变块组合得到的,换个颜色立马就能明白:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">那如果是这样一个箭头造型呢?</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">一样的,它也是两个渐变的叠加,渐变的颜色是透明 --&gt; 颜色A --&gt; 颜色B --&gt; 透明。当然,同样在这里也可以使用 clip-path:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">这里给出 clip-path 的解法:</span></span></span></p><blockquote>{<br/>&nbsp; &nbsp; background: linear-gradient(45deg, #04e6fb, #65ff9a);<br/>&nbsp; &nbsp; clip-path: polygon(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;30px 50%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;calc(100% - 30px) 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;100% 50%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;calc(100% - 30px) 0<br/>&nbsp; &nbsp; );<br/>}</blockquote><p><strong>内切圆角</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">下面这个按钮形状,多出现于优惠券,最常见的解法,也是使用渐变,当然,与切角不同,这里使用的径向渐变。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">首先,看这样一个简单的例子:</span></span></span></p><blockquote>&lt;div&gt;&lt;/div&gt;</blockquote><blockquote>div {<br/>&nbsp; &nbsp; background-image: radial-gradient(circle at 100% 100%, transparent 0, transparent 12px, #2179f5 12px);<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">可以得到这样一个图形:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">所以,只需控制下 background-size,在 4 个角实现 4 个这样的图形即可:</span></span></span></p><blockquote>&lt;div class=inset-circle&gt;inset-circle&lt;/div&gt;</blockquote><blockquote>&.inset-circle {<br/>&nbsp; &nbsp; background-size: 70% 70%;<br/>&nbsp; &nbsp; background-image: radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 100% 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 0 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 100% 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 0 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;);<br/>&nbsp; &nbsp; background-repeat: no-repeat;<br/>&nbsp; &nbsp; background-position: right bottom, left top, right top, left bottom;<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p><strong>借助 mask 实现渐变的内切圆角按钮</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">如果背景色要求渐变怎么办呢?</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">假设我们有一张矩形背景图案,我们只需要使用 mask 实现一层遮罩,利用 mask 的特性,把 4 个角给遮住即可。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">mask 的代码和上述的圆角切角代码非常类似,简单改造下即可得到渐变的内切圆角按钮:</span></span></span></p><blockquote>&lt;div class=mask-inset-circle&gt;inset-circle&lt;/div&gt;</blockquote><blockquote>.mask-inset-circle {<br/>&nbsp; &nbsp; background: linear-gradient(45deg, #2179f5, #e91e63);<br/>&nbsp; &nbsp; mask: radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 100% 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 0 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 100% 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;),<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;radial-gradient(<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;circle at 0 100%,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 0,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;transparent 12px,<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#2179f5 13px<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;);<br/>&nbsp; &nbsp; mask-repeat: no-repeat;<br/>&nbsp; &nbsp; mask-position: right bottom, left top, right top, left bottom;<br/>&nbsp; &nbsp; mask-size: 70% 70%;<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">这样,我们就得到了这样一个图形:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">当然,读懂上述代码,你需要首先弄清楚 CSS mask 属性的原理,如果你对它还有些陌生,可以看看我的这篇文章:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"><span style="color:#428bca"><a href="https://github.com/chokcoco/iCSS/issues/80" target="_blank">奇妙的 CSS MASK</a></span></span></span></span></p><p><strong>圆角不规则矩形</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">下面这个按钮形状,也是最近被问到最多的,先来看看它的造型:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">不太好给它起名,一侧是规则的带圆角直角,另外一侧则是带圆角的斜边。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">其实,它就是由圆角矩形 + 圆角平行四边形组成:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">所以,借助两个伪元素,可以轻松的实现它们:</span></span></span></p><blockquote>&lt;div class=skew&gt;Skew&lt;/div&gt;</blockquote><blockquote>.skew {<br/>&nbsp; &nbsp; position: relative;<br/>&nbsp; &nbsp; width: 120px;<br/><br/>&nbsp; &nbsp; &::after {<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;content: ;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;position: absolute;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;top: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;left: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;right: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;bottom: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;border-radius: 10px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;background: orange;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;transform: skewX(15deg);<br/>&nbsp; &nbsp; }<br/>&nbsp; &nbsp; &::before {<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;content: ;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;position: absolute;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;top: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;right: -13px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;width: 100px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;height: 64px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;border-radius: 10px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;background: orange;<br/>&nbsp; &nbsp; }<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">由于一个伪元素叠加在另外一个之上,所以对其中一个使用渐变,一个则是纯色,其颜色是可以完美衔接在一起的,这样就实现了渐变色的该图形:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p><strong>外圆角按钮</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">接下来这个按钮形状,常见于 Tab 页上,类似于 Chrome 的分页:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">我们对这个按钮形状拆解一下,这里其实是 3 块的叠加:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">只需要想清楚如何实现两侧的弧形三角即可。这里还是借助了渐变 -- 径向渐变,其实他是这样,如下图所示,我们只需要把黑色部分替换为透明即可,使用两个伪元素即可:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">代码如下:</span></span></span></p><blockquote>&lt;div class=outside-circle&gt;outside-circle&lt;/div&gt;</blockquote><blockquote>.outside-circle {<br/>&nbsp; &nbsp; position: relative;<br/>&nbsp; &nbsp; background: #e91e63;<br/>&nbsp; &nbsp; border-radius: 10px 10px 0 0;<br/><br/>&nbsp; &nbsp; &::before {<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;content: ;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;position: absolute;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;width: 20px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;height: 20px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;left: -20px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;bottom: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;background: #000;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;background:radial-gradient(circle at 0 0, transparent 20px, #e91e63 21px);<br/>&nbsp; &nbsp; }<br/>&nbsp; &nbsp; &::after {<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;content: ;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;position: absolute;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;width: 20px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;height: 20px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;right: -20px;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;bottom: 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;background: #000;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;background:radial-gradient(circle at 100% 0, transparent 20px, #e91e63 21px);<br/>&nbsp; &nbsp; }<br/>}</blockquote><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">即可得到:</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: center;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px"></span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">上述的所有图形的完整代码,你可以在这里看到:<span style="color:#428bca"><a href="https://codepen.io/Chokcoco/pen/QWMoBGO?editors=1100" target="_blank">CodePen Demo -- CSS Various Button Shapes | CSS 各种造型按钮</a></span></span></span></span></p><p><strong>总结一下</strong></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">基于上述的实现,我们不难发现,一些稍微特殊的按钮,无非都通过拼接、障眼法、遮罩等方式实现。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">而在其中:</span></span></span></p><ul class=" list-paddingleft-2"><li><p>渐变(线性渐变 linear-gradient、径向渐变 radial-gradient、多重渐变)</p></li><li><p>遮罩 mask</p></li><li><p>裁剪 clip-path</p></li><li><p>变形 transform<br/></p></li></ul><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">发挥了重要的作用,熟练使用它们,我们对于这些图形就可以信手拈来,基于它们的变形也能从容面对。</span></span></span></p><p style="line-height: 30px; text-indent: nullem; text-align: left;"><span style="color:rgb(34, 34, 34)"><span style="font-family:tahoma, arial, "><span style="font-size: 16px">上述的图形,再配合 filter: drop-shadow(),基本都能实现不规则阴影。</span></span></span></p><p><br/></p><link rel="stylesheet" href="//www.zhidianyun.cn/source/plugin/wcn_editor/public/wcn_editor_fit.css?v134_auL" id="wcn_editor_css"/>
页: [1]
查看完整版本: 使用 CSS 轻松实现一些高频出现的奇形怪状按钮