優(yōu)雅的45度角分段器:利用css clip-path打造流暢交互
如何讓分段器在點(diǎn)擊按鈕時(shí),右側(cè)邊框以45度角流暢地變?yōu)榍€,點(diǎn)擊另一個(gè)按鈕時(shí)又恢復(fù)原狀?這不僅提升視覺吸引力,更能優(yōu)化用戶體驗(yàn)。本文將使用CSS的clip-path屬性,結(jié)合path函數(shù),完美實(shí)現(xiàn)這一效果。
以下代碼示例演示了如何創(chuàng)建這種動(dòng)態(tài)的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 { /* 左側(cè)曲線 */ 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 { /* 右側(cè)曲線 */ 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="'標(biāo)簽' + 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類,實(shí)現(xiàn)點(diǎn)擊切換時(shí)的動(dòng)畫效果。 這個(gè)方法簡潔高效,讓你的分段器設(shè)計(jì)更具吸引力。 記住,你需要一個(gè)支持clip-path的現(xiàn)代瀏覽器才能看到效果。
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END