CSS3知识汇总16:clip-path

2021年2月19日学习笔记

【椭圆】

div{

    background:#fb3;

    width: 200px;

    height: 150px;

    border-radius: 50% / 50%;  /*半径*/

}

【半椭圆】

沿纵轴对称,如果传4个值,分别从左上角开始以顺时针应用到各个拐角,如果提供3个值,意味着第4个值与第2个值相同

4个角还可以有不同的水平和垂直半径,在斜杠前指定1~4个值,在斜杠后指定1~4个值

当border-radius: 10px / 5px 20px时,相当于

border-radius: 10px 10px 10px 10px / 5px 20px 5px 20px;

div{

    background:#fb3;

    width: 200px;

    height: 150px;

    border-radius: 50% / 100% 100% 0 0;

}

【四分之一椭圆】

其中一个角的水平和垂直半径值都需要是100%,而其他三个角都不能设为圆角

div{margin-top:5px}

div:nth-child(1){

    background:#fb3;

    width: 200px;

    height: 150px;

    border-radius: 100% 0 0 0;

}

div:nth-child(2){

    background:#fb3;

    width: 200px;

    height: 150px;

    border-radius: 0 100% 0 0;

}

div:nth-child(3){

    background:#fb3;

    width: 200px;

    height: 150px;

    border-radius: 0 0 100% 0;

}

div:nth-child(4){

    background:#fb3;

    width: 200px;

    height: 150px;

    border-radius: 0 0 0 100%;

}

【扩展练习】

div{margin-top:5px}

div:nth-child(1){

    background:pink;

    width: 300px;

    height: 100px;

    border-radius: 50% 0 50% 0 / 100% 0 100% 0;

    text-align: center;

    line-height: 100px;

}

div:nth-child(2){

    background:chartreuse;

    width: 100px;

    height: 100px;

    border-radius:30px 30px 15px 15px / 100px;

}

【平行四边形】

.button{

    padding:0 .5em;

    transform: skewX(-45deg);

    display: inline-block;

    background:#58a;

    margin:.5em;

}

.button > div{

    transform: skewX(45deg); /*将字摆正*/

}

   

Click me

【平行四边形--伪元素】

.button{

    position: relative;

    padding:0 .5em;

    display: inline-block;

    margin:.5em;

}

.button::before{

    content:'';

    position: absolute;

    background:#58a;

    transform: skewX(-45deg);

    top:0;right:0;bottom: 0;left:0;

    z-index: -1;

}

【八角形】

.pic{

    width: 400px;

    margin-top:150px;

    transform: rotate(45deg);

    overflow: hidden;

}

.pic > img{

    max-width: 100%;

    transform: rotate(-45deg);

}

   

【菱形】

需要图片的宽度与容器的对角线相等,而max-width: 100%是边长相等

scale()变形样式,是以它的中心点进行缩放的,除非额外指定了transform-origin

通过width属性来放大图片时,只会以它的左上角为原点进行缩放,需要负外边距调整

.pic{

    width: 400px;

    margin-top:150px;

    transform: rotate(45deg);

    overflow: hidden;

}

.pic > img{

    max-width: 100%;

    transform: rotate(-45deg) scale(1.42);

}

【裁切路径方案clip-path菱形】

img{

    width: 400px;

    margin-top:150px;

    clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);

    transition: 1s clip-path;

    overflow: hidden;

}

img:hover{

    clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);

}

【相关知识点】

clip-path属性可以防止部分元素通过定义的剪切区域来显示(目前兼容性较差,IE和Edge不支持)

生成器https://www.html.cn/tool/css-clip-path/

一、基本图形

inset()矩形(上右下左的边距round上右下左圆角)

inset()可以传入5个参数,分别对应top,right,bottom,left的裁剪位置,round radius(可选,圆角)

.pic{

    width: 100px;

    height:100px;

    background:green;

    clip-path: inset(10px 20px 30px 10px round 20px 5px 50px 0);

}

circle圆形

circle()可以传人2个可选参数:

1. 圆的半径,默认元素宽高中短的那个为直径,支持百分比

2. 圆心位置,默认为元素中心点

半径公式

如果半径使用百分比:圆的半径 = (sqrt(width^2+height^2)/sqrt(2)) * 百分比

.pic{

    width: 100px;

    height: 100px;

    background: red;

    clip-path: circle(30% at 50px 50px);

}

ellipse椭圆

ellipse()可以传人3个可选参数;

1. 椭圆的X轴半径,默认是宽度的一半,支持百分比

2. 椭圆的Y轴半径,默认是高度的一半,支持百分比

3. 椭圆中心位置,默认是元素的中心点

.pic{

    width: 100px;

    height: 100px;

    background: red;

    clip-path: ellipse(25% 50% at 50% 50%);

}

二、多边形polygon--正三角形

.pic{

    width: 100px;

    height: 87px;

    background: red;

    clip-path: polygon(0% 100%, 50% 0%, 100% 100%);

}

x: 0, y:100% 从元素的左上角开始,并从那里开始移动

x: 50%, y: 0

x: 100%, y: 100%  元素右边,元素底部

从左下角x: 0, y:100%开始,水平移动到50%,然后垂直向上到达顶部的坐标点(第二个点),接着水平移动到100%的位置,最后垂直向下回到底部,到达第三个坐标点

正方形

.pic{

    width: 100px;

    height: 100px;

    background: red;

    clip-path: polygon(0% 0%, 0% 100%, 100% 100%, 100% 0);

}

从左上角开始,垂直向下100%第二个点,平移100%第三个点,垂直向上0第四个点

正五边形  59/(59+95)=38.31%,31/(81*2)=19.14%

162/2 = 81

59是上面三角形的高度

95是下面三角形的高度

31是下面四边形的高度

.pic{

    width: 162px;

    height: 154px;

    background: red;

    clip-path: polygon(0% 38.31%, 50% 0%, 100% 38.31%, 80.86% 100%, 19.14% 100%);

}

正六边形  50/(100+502)=25%,150/(100+502)=75%

50 是左边三角形的高度

100 是边长

.pic{

    width: 200px;

    height: 174px;

    background: red;

    clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);

}

正七边形 

22/(100+622)=10.09%

202/(100+622)=90.18%

43/(43+97+78)=19.72%

(43+97)/(43+97+78)=64.22%

62/(100+622)=27.68%

(100+62)/(100+622)=72.32%

.pic{

    width: 224px;

    height: 218px;

    background: red;

    clip-path: polygon(50% 0%, 90.18% 19.72%, 100% 64.22%, 72.32% 100%, 27.68% 100%, 0% 64.22%, 10.09% 19.72%);

}

正七边形计算方法

正八边形

71/(100+712)=29.34%

(71+100)/(100+712)=70.66%

.pic{

    width: 200px;

    height: 174px;

    background: red;

    clip-path:polygon(29.34% 0%, 70.66% 0%,100% 29.34%,100% 70.66%,70.66% 100%,29.34% 100%,0% 70.66%,0% 29.34%);

}

正八边形计算方法

五角星

.pic{

    width: 200px;

    height: 174px;

    background: red;

    clip-path:polygon(50% 0%, 63% 38%, 100% 38%, 69% 59%, 82% 100%, 50% 75%, 18% 100%, 31% 59%, 0 38%, 37% 38%);

}

红叉叉

.pic{

    width: 200px;

    height: 174px;

    background: red;

    clip-path:polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);

}

标签

.pic{

    width: 200px;

    height: 174px;

    background: red;

    clip-path:polygon(50% 20%, 70% 0, 100% 0, 100% 100%, 70% 100%, 50% 80%, 30% 100%, 0 100%, 0 0, 30% 0);

}

也可用于动画中

div{

    width:800px;

    height:400px;

    position: absolute;

    background: black;

    animation: move-0 1s linear infinite;

}

@keyframes move-0 {

    0%{

        clip-path: polygon(35.00% 29.75%,21.00% 71.75%,63.38% 36.25%,35.13% 30.00%)

    }

    100%{

        clip-path: polygon(82.88% 21.25%,65.25% 70.00%,86.88% 63.25%,83.00% 21.00%)

    }

}

如果觉得百分比不好计算,可以换算成px

下面就是第一个点的计算方法

35.00%*800=280px

29.75%*400=119px

只要两个 clip-path,其中包含的点个数相同,在animation的帧内部就可以线性切换了

如果不同是没有效果的

小技巧,如果点不够,可以将两个坐标点进行重合即可


推荐网址:

http://species-in-pieces.com/

30个动物全部使用CSS3写的。超牛

你可能感兴趣的:(CSS3知识汇总16:clip-path)