CSS3 之高级动画(6)CSS3 clip-path属性实现的几何图形变形动画

本文介绍CSS中的clip-path属性,该属性用于定义元素可见区域的形状,通过不同的多边形定义来展示元素的不同部分,支持SVG路径语法。文章提供了一个动态变换剪切区域的例子,展示了如何结合动画和背景图片创造视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

clip-path

属性介绍:

clip-path属性可以创建一个只有元素的部分区域可以显示的剪切区域。

区域内的部分显示,区域外的隐藏。

剪切区域是被引用内嵌的URL定义的路径或者外部svg的路径。

使用:

不可思议的CSS之clip-path

css中clip-path属性的运用

具体还支持SVG路径

maker工具,你画好图形,代码就在下面: http://bennettfeely.com/clippy/

主流浏览器支持情况:

 

效果实现:

 

 

html:

<div></div>

css:

html,
body {
  height: 100%;
}

body {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  background: #999999;
}

div {
  width: 75vmin;
  height: 75vmin;
  -webkit-animation: shape 10s ease-in-out infinite both;
  animation: shape 10s ease-in-out infinite both;
}

@-webkit-keyframes shape {

  from,
  to {
    -webkit-clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    background: url('./267323.jpg') center;
  }

  14.28571% {
    -webkit-clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    background: url('./267323.jpg') left top;
  }

  28.57143% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0 60%, 10% 20%);
    clip-path: polygon(50% 0, 50% 0, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0 60%, 10% 20%);
    background: url('./267323.jpg') right bottom;
  }

  42.85714% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 100% 25%, 100% 75%, 50% 100%, 50% 100%, 0 75%, 0 25%);
    clip-path: polygon(50% 0, 50% 0, 100% 25%, 100% 75%, 50% 100%, 50% 100%, 0 75%, 0 25%);
    background: url('./267323.jpg') left bottom;
  }

  57.14286% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 100% 38%, 100% 38%, 82% 100%, 18% 100%, 0 38%, 0 38%);
    clip-path: polygon(50% 0, 50% 0, 100% 38%, 100% 38%, 82% 100%, 18% 100%, 0 38%, 0 38%);
    background: url('./267323.jpg') right top;
  }

  71.42857% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 100% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 0 0);
    clip-path: polygon(50% 0, 50% 0, 100% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 0 0);
    background: url('./267323.jpg') right right;
  }

  85.71429% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 50% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 50% 0);
    clip-path: polygon(50% 0, 50% 0, 50% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 50% 0);
    background: url('./267323.jpg') left center;
  }
}

@keyframes shape {

  from,
  to {
    -webkit-clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    background: url('./267323.jpg') center;
  }

  14.28571% {
    -webkit-clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
    background: url('./267323.jpg') left top;
  }

  28.57143% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0 60%, 10% 20%);
    clip-path: polygon(50% 0, 50% 0, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0 60%, 10% 20%);
    background: url('./267323.jpg') right bottom;
  }

  42.85714% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 100% 25%, 100% 75%, 50% 100%, 50% 100%, 0 75%, 0 25%);
    clip-path: polygon(50% 0, 50% 0, 100% 25%, 100% 75%, 50% 100%, 50% 100%, 0 75%, 0 25%);
    background: url('./267323.jpg') left bottom;
  }

  57.14286% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 100% 38%, 100% 38%, 82% 100%, 18% 100%, 0 38%, 0 38%);
    clip-path: polygon(50% 0, 50% 0, 100% 38%, 100% 38%, 82% 100%, 18% 100%, 0 38%, 0 38%);
    background: url('./267323.jpg') right top;
  }

  71.42857% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 100% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 0 0);
    clip-path: polygon(50% 0, 50% 0, 100% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 0 0);
    background: url('./267323.jpg') right right;
  }

  85.71429% {
    -webkit-clip-path: polygon(50% 0, 50% 0, 50% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 50% 0);
    clip-path: polygon(50% 0, 50% 0, 50% 0, 100% 100%, 100% 100%, 0 100%, 0 100%, 50% 0);
    background: url('./267323.jpg') left center;
  }
}

 

转载于:https://www.cnblogs.com/houfee/p/10613355.html

### CSS `clip-path` 属性详解 #### 一、基本概念 `clip-path` 是一个CSS属性,允许创建一个剪切区域来决定元素的哪些部分可见,哪些部分被隐藏。通过定义这个剪切路径(clipping path),可以实现非矩形的裁剪形状,让元素内容按照特定的几何形状展示[^3]。 #### 二、基础语法 `clip-path` 的基本语法如下所示: ```css element { clip-path: shape-function(); } ``` 其中 `shape-function()` 可以是多种不同的函数之一,用于描述具体的剪辑路径。常见的有 `circle()`, `ellipse()`, `polygon()` 和内置关键字如 `inset()` 等。 #### 三、常用函数及其参数说明 - **Circle 圆形** 创建圆形剪贴区,其接受三个主要参数:半径(radius),中心位置(x y)。 ```css .example-circle { clip-path: circle(50% at center); } ``` - **Ellipse 椭圆** 定义椭圆形剪贴区,需指定两个轴向上的尺寸以及中心坐标。 ```css .example-ellipse { clip-path: ellipse(70px 40px at 50% 50%); } ``` - **Polygon 多边形** 利用一系列顶点坐标构建任意多边形作为剪贴边界 ```css .example-polygon { clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%); } ``` - **Inset 内嵌矩形** 设置四个方向上的偏移量来形成内部矩形框 ```css .example-inset { clip-path: inset(20px round 8px); /* 上下左右各留白20像素 */ } ``` #### 四、实际案例演示 下面给出几个简单的例子来帮助理解如何运用这些函数创造有趣的视觉效果: ##### 示例 1 - 星星图案 利用 `polygon()` 函数绘制五角星样式 ```html <div class="star"></div> <style> .star{ width: 10em; height: 10em; background-color:red; clip-path:polygon( 50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35% ); } </style> ``` ##### 示例 2 - 渐变遮罩文字 结合伪类与渐层背景制作透明度变化的文字特效 ```html <h1>Gradient Mask Text</h1> <style> h1::before { content:""; position:absolute; top:0;left:0;right:0;bottom:0; z-index:-1; background-image:linear-gradient(to right,#ff7e5f,#feb47b); -moz-background-clip:text; -webkit-background-clip:text; background-clip:text; color:#fff; text-fill-color:none; filter:brightness(.8); transform-origin:center; animation:shine linear infinite alternate-reverse 3s; } @keyframes shine {to{filter:hue-rotate(-360deg)}} </style> ``` 注意,在此示例中虽然没有直接使用到 `clip-path` ,但是展示了另一种形式的内容裁剪方式——即 `-webkit-background-clip:text` 。这表明除了传统的 `clip-path` 方法外还有其他途径达到相似的效果。 #### 五、最佳实践建议 当考虑采用 `clip-path` 进行设计时,请记住以下几点: - 浏览器兼容性问题可能会影响最终呈现; - 对于复杂的图形操作,SVG可能是更好的选择; - 尽量减少不必要的复杂度以免影响性能表现;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值