自适应的椭圆
- 关键实现: border-radius
- 给任何正方形元素设置一个足够大(边长一半)的border-radius,就可以把它变成一个圆形。
具体实现:地址
关键实现: border-radius
- 具体实现:border-radius是一个简写属性,为元素的每个角指定不同的值:地址
- border-top-left-radius
- border-top-right-radius
- border-bottom-right-radius
- border-bottom-left-radius ```typescript
border-radius: 50% / 100% 100% 0 0; border-radius: 50% / 0 0 100% 100%; border-radius: 100% 0 0 100% / 50%; border-radius: 0 100% 100% 0 / 50%;
<a name="vsZpw"></a>## **四分之一椭圆**- 关键实现: border-radius- 具体实现:其中一个角的水平和垂直半径值都需要是100%,而其他三个角都不能设为圆角:[地址](https://dabblet.com/gist/2b75df0e72c9804e8abe)```typescriptborder-radius: 100% 0 0 0;border-radius: 0 100% 0 0;border-radius: 0 0 100% 0;border-radius: 0 0 0 100%;
平行四边形
- 关键实现:transform: skewX()
具体实现:有一个普通矩形,通过skew()的变形属性来对这个矩形进行斜向拉伸:地址
transform: skewX(45deg);
伪元素方案
把所有样式(背景、边框等)应用到伪元素上,然后再对伪元素进行变形。因为我们的内容并不是包含在伪元素里的,所以内容并不会受到变形的影响:地址。技巧:当我们想变形一个元素而不想变形它的内容时就可以用伪元素方案。菱形图片
关键实现:rotate()、clip-path
- 具体实现:
- 基于变形的方案:地址
需要把图片用一个包裹起来,然后对其应用相反的rotate()变形样式 ```typescript
- 基于变形的方案:地址
.diamond { width: 250px; height: 250px; transform: rotate(45deg); overflow: hidden; margin: 100px; }
.diamond img { max-width: 100%; transform: rotate(-45deg) scale(1.42); z-index: -1; position: relative; }
裁切路径方案: [地址](https://dabblet.com/gist/c62456fed36a524b8273)```typescriptclip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
切角效果
- 具体实现:
- 方案一:css渐变:地址 ```typescript //切一个角 background:linear-gradient(-45deg, transparent 15px,#58a 0);
//切四个角 background: #58a; background: linear-gradient(135deg, transparent 15px, #58a 0) top left, linear-gradient(-135deg, transparent 15px, #58a 0) top right, linear-gradient(-45deg, transparent 15px, #58a 0) bottom right, linear-gradient(45deg, transparent 15px, #58a 0) bottom left; background-size: 50% 50%; background-repeat: no-repeat;
SCSS的mixin中为各个参数指定了默认值,而且这些默认值也可以引用其他参数的值。- 弧形切角**,**用径向渐变来替代上述线性渐变:[地址](https://dabblet.com/gist/24484257bc6cf7076a8e)- **方案二:内联SVG与border-image方案: **[地址](https://dabblet.com/gist/365c867441f3ee298212)- **方案三:裁切路径方案: **[地址](https://dabblet.com/gist/4a88c463afdb0778e973)<a name="sfmgl"></a>## **梯形标签页**- 关键实现:transform 3D scale()- 具体实现:[地址](https://dabblet.com/gist/1345dc9399dc8e794502)- 把变形效果作用在伪元素上- 指定transform-origin:bottom;,当它在3D空间中旋转时,可以把它的底边固定住。- CSS 函数 scale() 用于修改元素的大小。```typescriptnav a::before {content: ''; /* To generate the box */position: absolute;top: 0; right: 0; bottom: 0; left: 0;z-index: -1;transform: scale(1.1, 1.3) perspective(.5em) rotateX(5deg);transform-origin: bottom;}
简单的饼图
- 关键实现
- 具体实现:
基于transform的解决方案:地址- 通过创建一个阴阳图,左边为饼状图默认颜色,右边为饼状图显示颜色。
- 0~50%通过创建伪元素(颜色为默认颜色),挡住右边的显示颜色,通过旋转(0~0.5turn),一点点将显示颜色显示出来。
- 50%~100%通过创建伪元素(颜色为显示颜色),通过旋转(0~0.5turn),一点点覆盖左边的默认颜色。
- 通过animation切换伪元素背景色与重置旋转角度。
- 通过animation-delay将直接跳转到动画的指定时间点。
```typescript
// html50%
// css .pie { display: inline-block; position: relative; width: 100px; line-height: 100px; border-radius: 50%; background: yellowgreen; background-image: linear-gradient(to right, transparent 50%, #655 0); color: transparent; text-align: center; }
@keyframes spin { to { transform: rotate(.5turn); } } @keyframes bg { 50% { background: #655; } }
.pie::before { content: ‘’; position: absolute; top: 0; left: 50%; width: 50%; height: 100%; border-radius: 0 100% 100% 0 / 50%; background-color: inherit; transform-origin: left; animation: spin 50s linear infinite, bg 100s step-end infinite; animation-play-state: paused; animation-delay: inherit; }
// js var _dom = document.querySelector(‘.pie’) _dom.style.animationDelay = ‘-‘ + parseFloat(_dom.textContent) + ‘s’;
- ** svg方案:**[地址](https://dabblet.com/gist/4696e4c6700fe9f346d8)1. 使用SVG中的虚线描边,将圆形描边的大小设置为半径的2倍,能够完全覆盖圆形。2. 设置虚线的间隔为圆形的周长,能够保证只显示一块虚线。3. 设置虚线的长度为所占比例,显示出我们所需要的饼状图。```typescript// html<svg viewBox='0 0 32 32'><circle r='16' cx='16' cy='16'</svg>// csssvg {width:100px;height:100px;background:yellowgreen;border-radius:50%;transform:rotate(-90deg);}circle{fill:yellowgreen;stroke:#655;stroke-width:32;stroke-dasharray:20 100; /* 半径为16,会使的周长刚刚好为100,通过设置虚线长度,直接就能够显示为百分比饼状图 */}
