马黑黑《CSS offset-path 属性之几何图形路径》
<style>@import 'https://638183.freep.cn/638183/web/css/hl.css';
.artBox { margin: 20px auto; font: normal 18px/26px sans-serif; max-width: 65vw; }
.artBox mark { padding: 2px 6px; background: #ccc; }
.artBox blockquote { padding: 14px; border: solid gray; border-width: 0 4px; border-radius: 10px; background: #ddd; }
.artBox p { margin: 10px 0; }
.artBox table { border-collapse: collapse; white-space: pre-wrap; box-sizing: border-box; }
.artBox th, .artBox td { padding: 8px 10px; border: 1px solid #999; }
.artBox th { text-align: center; background: #eee; }
.tMid { text-align: center; }
.hlCode { padding: 0;}
#stage { width: 640px; height: 400px; border: 1px solid gray; position: relative; margin: 30px auto 0; }
#stage::before { position: absolute; content: attr(data-code); width: 100%; height: 100%; display: grid; place-items: center; }
#fish { width: 80px; height: 40px; transition: 0.5s; position-anchor: right center; background: url('https://638183.freep.cn/638183/web/svg1/fish.svg') no-repeat center/cover; position: absolute;}
#pathSet p { cursor: pointer; }
@keyframes move {
from { offset-distance: 0%; }
to { offset-distance: 100%; }
}
</style>
<div class="artBox">
<p>CSS offset-path 属性值除了可以使用来源于 SVG 的 path 函数、ray 函数赋值,还可以使用 SVG 基本图形充当函数赋值。基本图形函数制作的路径更易理解、代码更简洁。</p>
<table>
<caption><h2 class="tMid">offset-path 属性基本图形函数</h2></caption>
<tr>
<th width="60">序号</th>
<th width="100">函数</th>
<th>语法</th>
<th>备注</th>
</tr>
<tr>
<td>1</td>
<td>circle(圆)</td>
<td>circle(r at x y)</td>
<td>r :半径,x、y </td>
</tr>
<tr>
<td>2</td>
<td>ellipse(椭圆)</td>
<td>ellipse(rx ry at x y)</td>
<td>rx、ry :x和y轴半径,x、y :圆心位置</td>
</tr>
<tr>
<td>3</td>
<td>inset(内矩形)</td>
<td>inset(top right bottom left )</td>
<td>支持单值、多值表达,支持圆角</td>
</tr>
<tr>
<td>4</td>
<td>polygon(多边形)</td>
<td>polygon(x1 y1, x2 y2, ..., xn yn)</td>
<td>坐标数据支持数值、百分比</td>
</tr>
<tr>
<td>5</td>
<td>xywh(矩形路径)</td>
<td>xywh(x y width height )</td>
<td>x、y :矩形左上角坐标,width、height :矩形宽高,支持圆角</td>
</tr>
</table>
<div id="stage" class="hlCode" data-code="offset属性值(待选择)">
<div id="fish"></div>
</div>
<p><strong>⬇️ 选择动画路径:</strong></p>
<div id="pathSet"></div>
</div>
<script>
const shapes = {
circle: 'circle(200px)',
ellipse: 'ellipse(300px 150px)',
inset: 'inset(40px round 15%)',
polygon: 'polygon(10% 10%,90% 50%,10% 90%)',
xywh: 'xywh(40px 40px 86% 80% round 15%)',
};
const frg = new DocumentFragment();
for (let key in shapes) {
const p = document.createElement('p');
p.textContent = `\u2714\ufe0f${key} :`;
p.title = `点击运行 ${key} 图形路径动画`;
p.onclick = () => {
fish.style.animation = '';
fish.style.setProperty('offset-path', `${shapes}`);
setTimeout( () => {
fish.style.setProperty('animation', 'move linear 8s');
}, 500);
stage.dataset.code = `offset-path: ${shapes};`;
};
const code = document.createElement('code');
code.textContent = shapes;
code.classList.add('hlCode');
p.appendChild(code);
frg.appendChild(p);
}
pathSet.appendChild(frg);
</script>
直观、实用的教程,谢谢分享
页:
[1]