优雅的45度角分段器:利用css clip-path打造流畅交互
如何让分段器在点击按钮时,右侧边框以45度角流畅地变为曲线,点击另一个按钮时又恢复原状?这不仅提升视觉吸引力,更能优化用户体验。本文将使用CSS的clip-path属性,结合path函数,完美实现这一效果。
以下代码示例演示了如何创建这种动态的45度曲线分段器:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>45度角曲线分段器</title> <style> .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 50, 50 Z'); } .content-wrap { min-height: 200px; background-color: #fff; } </style> </head> <body> <div class="wrap" x-data="initData()"> <div class="tabs"> <template x-for="index in 2"> <div :class="{ 'active': activeIndex == index }" class="tab" @click="onTabClick(index)" x-text="'标签' + index"></div> </template> </div> <div class="content-wrap"></div> </div> <script> function initData() { return { activeIndex: 1, onTabClick(index) { this.activeIndex = index; } }; } </script> </body> </html>
代码中,clip-path: path(‘M 0,50 C 25,50 25,0 50,0 L 50, 50 Z’); 定义了45度曲线的路径。通过JavaScript控制active类,实现点击切换时的动画效果。 这个方法简洁高效,让你的分段器设计更具吸引力。 记住,你需要一个支持clip-path的现代浏览器才能看到效果。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END