
巧用CSS打造45度角曲线分段器
现代网页设计中,分段器(或标签切换器)是常见的交互元素。本文将介绍如何利用CSS的clip-path属性和path函数,实现点击左侧按钮时,右侧边框呈现45度曲线的动态效果,提升用户体验。
核心思路是为每个标签创建伪元素,用clip-path定义45度曲线。激活标签时,曲线便会动态出现在左侧或右侧。
以下为HTML和CSS代码示例:
标签1标签2
.wrap {
background-color: #eee;
width: 375px;
margin: 0 auto;
padding: 10px;
}
.tabs {
display: flex;
width: 100%;
overflow: hidden;
border-radius: 8px 8px 0 0;
background: linear-gradient(#cdd9fe, #e2e9fd);
}
.tab {
flex: 0 0 50.1%;
height: 50px;
cursor: pointer;
position: relative;
text-align: center;
line-height: 50px;
}
.tab.active {
background-color: #fff;
color: #4185ef;
}
.tab.active:before { /* 左侧曲线 */
content: '';
position: absolute;
top: 0;
left: -50px;
height: 100%;
width: 50px;
z-index: 2;
background-color: #fff;
clip-path: path('M 0,50 C 25,50 25,0 50,0 L 50, 50 Z');
}
.tab.active:after { /* 右侧曲线 */
content: '';
position: absolute;
top: 0;
right: -50px;
height: 100%;
width: 50px;
z-index: 2;
background-color: #fff;
clip-path: path('M 0,0 C 25,0 25,50 50,50 L 0, 50 Z');
}
.content-wrap {
min-height: 200px;
background-color: #fff;
}
代码中,clip-path: path(...) 精确定义了45度曲线的路径。 JavaScript (例如,使用Alpine.js或其他框架) 可以用来管理active类,实现标签点击切换效果。 此方法简洁高效,为开发者提供了一种优雅的实现动态元素的方案。
立即学习“前端免费学习笔记(深入)”;










