Skip to content

梯形tab按钮实现

🕒 Published at:

在网页设计中,tab按钮是常见的UI组件。本文将介绍如何使用 clip-pathpath 函数和 Alpine.js 实现梯形tab按钮效果。

实现步骤

HTML结构

首先,创建基本的HTML结构,其中包括一个 wrap 容器、 tabs 容器以及每个 tab 按钮:

html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>梯形tab按钮-基于clip-path path函数实现</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 0, 50 Z');
    }

    .content-wrap {
      min-height: 200px;
      background-color: #fff;
    }
  </style>
  <script src="https://cdn.bootcdn.net/ajax/libs/alpinejs/3.10.3/cdn.min.js" defer></script>
  <script src="https://unpkg.com/@unocss/runtime"></script>
  <script>
    window.__unocss = {
      rules: [],
      presets: []
    }
  </script>
</head>

<body>
  <div class="wrap" x-data="initData()">
    <div class="tabs">
      <template x-for="index in 2" :key="index">
        <div @click="onTabClick(index)" class="tab" :class="{ 'active': activeIndex == 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>

关键CSS样式

使用 clip-pathpath 函数实现梯形tab按钮效果:

css
.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-pathpath 函数,可以轻松实现具有梯形效果的tab按钮。