css 绘制不规则图形
tsx
style={{
clipPath: 'polygon(0 0, 100% 0, 100% 100%, 8% 100%)',
}}
tsx
const funnelData: FunnelItem[] = [
{
label: '会话总数',
value: 64,
},
{
label: '咨询用户数',
value: 35,
},
{
label: '留资数',
value: 16,
},
];
return (
<div className="w-[330px] bg-white p-6 pr-16">
<h2 className="mb-6 text-base font-medium">会话漏斗</h2>
<div className="relative">
{funnelData.map((item, index) => (
<div
key={item.label}
className={cn(
'relative mb-1 flex h-16 items-center justify-between',
'my-2 px-6 text-white',
index === 0 && 'w-full bg-new-media-blue-900',
index === 1 && 'w-[90%] bg-new-media-blue-600',
index === 2 && 'w-[80%] bg-new-media-blue-300',
'ml-auto',
)}
style={{
clipPath: 'polygon(0 0, 100% 0, 100% 100%, 8% 100%)',
}}
>
<span className="ml-2 font-bold">{item.label}</span>
<span className="text-2xl font-medium">{item.value}</span>
</div>
))}
{/* 右侧标签和箭头 - 开口率 */}
<img
src={ApertureRatio}
alt="开口率"
className="absolute right-[-30px] top-[20px] h-[70px]"
/>
{/* 右侧标签和箭头 - 转化率 */}
<img
src={ConversionRate}
alt="转化率"
className="absolute bottom-[20px] right-[-30px] h-[70px]"
/>
</div>
</div>
);
可以拖拽生成 css 代码的 clip-path 的网站
www.jiangweishan.com/tool/clippy...
不规则图形添加阴影小技巧
添加阴影一般使用 box-shadow,但为不规则图形添加阴影可能没效果。
创建不规则图形
tsx
<div className={styles.demo}></div>
css
.demo {
background: green;
padding: 30px;
clip-path: polygon(30px 0%, 100% 0%, 100% 100%, 30px 100%, 0 50%)
}
css
.demo {
background: green;
padding: 30px;
clip-path: polygon(30px 0%, 100% 0%, 100% 100%, 30px 100%, 0 50%);
box-shadow: 0 0 10px 2px #323200;
}
没效果
正确做法 添加一个父元素 div 包裹,然后使用 filter: drop-shadow
添加阴影
tsx
<div className={styles.shadow}>
<div className={styles.demo}></div>
</div>
css
.shadow {
filter: drop-shadow(-1px 6px 3px rgba(50, 50, 0, 0.5));
}
.demo {
background: green;
padding: 30px;
clip-path: polygon(30px 0%, 100% 0%, 100% 100%, 30px 100%, 0 50%);
// box-shadow: 0 0 10px 2px #323200;
}
成功设置上阴影生效~