一、clip-path作用
属性使用裁剪方式创建元素的可显示区域。区域内的部分显示,区域外的隐藏。兼容性页蛮高的。
-webkit-clip-path解决兼容性问题
clip-path可以通过专有名词属性值很容易的画出一些简单的图形,例如圆(circle),椭圆(ellipse),圆角矩形(inset):
绘制圆形设置悬停后,按钮变大的效果案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 320px;
height: 320px;
border: 1px solid red;
background: url(./yuanquan.png) no-repeat;
/* background: url(./flower.jpg) no-repeat; */
background-size: cover;
/* 裁剪出圆形区域 */
/* clip-path: circle(40px at 100px 100px); */
clip-path: circle(40px at center);
/* 控制变化时间 */
transition: clip-path .8s;
}
.div1:hover{
/* clip-path: circle(80px at 100px 100px); */
clip-path: circle(80px at center);
}
</style>
</head>
<body>
<div class="div1"></div>
</body>
</html>
注意1使用clip-path要从同一个方向绘制,如果顺时针绘制就一律顺时针,逆时针就一律逆时针,因为polygon是一个连续的线段,若线段彼此有交集,面积区域就会有相减的状况发生(当然如果这是你要的效果就无妨了)。
注意2如果绘制时采用「比例」的方式绘制,长宽就必须要先行设定,不然有可能绘制出来的长宽和我们想像的就会有落差,使用「像素」绘制就没有这种问题。
二、案例
圆
div.item_cont .item:nth-child(7){
background: #00CCCE;
clip-path:circle(50% at 50% 50%);
}
椭圆ellipse,
div.item_cont .item:nth-child(8){
clip-path:ellipse(25% 50% at 50% 50%);
background: #C4C4C4;
}
圆角矩形
div.item_cont .item:nth-child(9){
clip-path:inset(20% 0 round 0 20%);
background: #9A99FF;
}
以下使用polygon绘制
五角星1
div{
width: 200px;
height: 200px;
background: url(../jsbase/瀑布流/picture/9.jpg) no-repeat center / cover;
margin: 10px;
clip-path: polygon(100% 39%,66% 32%,50% 3%,32% 32%,0 39%,24% 64%,19% 97%,50% 84%,81% 97%,76% 64%);
}
五角星2正五角星
div.item_cont .item:nth-child(6){
clip-path:polygon(50% 0%, 63% 38%, 100% 38%, 69% 59%, 82% 100%, 50% 75%, 18% 100%, 31% 59%, 0 38%, 37% 38%);
background: url(../jsbase/瀑布流/picture/8.jpg) no-repeat center / cover;
}
菱形
div.item_cont .item:nth-child(4){
clip-path: polygon(100% 50%, 50% 0,0 50%,50% 100%);
background: url(../jsbase/瀑布流/picture/6.jpg) no-repeat center / cover;
}
X形状
div.item_cont .item:nth-child(5){
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%);
background: url(../jsbase/瀑布流/picture/1.jpg) no-repeat center / cover;
animation-duration: 2s;
}
三角形
div.item_cont .item:nth-child(1){
clip-path: polygon(100% 100%,50% 0,0 100%);
background: url(../yuan.jpg) no-repeat center / cover;
animation-duration: 1s;
clip-path: rec;
}
div.item_cont .item:nth-child(2){
clip-path: polygon(100% 0,0 100%,100% 100%);
background: url(../yuan.jpg) no-repeat center / cover;
animation-duration: 4s;
}
提示框
div.item_cont .item:nth-child(10){
clip-path:polygon(0% 0%, 100% 0%, 100% 75%, 75% 75%, 75% 100%, 50% 75%, 0% 75%);
background: url(../jsbase/瀑布流/picture/1.jpg) no-repeat center / cover;
animation-duration: 1.2s;
}