Skip to content
Home/ Frontend/ CSS/ Tabs
作者:  WHY
字数统计: 
阅读时长:  分钟
阅读量: 

Tabs

纯 CSS 实现 tab 滑块选项卡效果 (带动画)
html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>纯CSS实现tab滑块选项卡效果</title>

    <style>
      :root {
        font-size: 16px;
        --nav-item-width: 9rem;
        --nav-item-height: 2rem;
        --nav-item-padding: 1rem;
        --transition-speed: 0.5s;
      }

      .container {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
        font-family: Helvetica;
        background-color: #e2e8f0;
      }

      nav {
        background-color: #fff;
        padding: var(--nav-item-padding) 0;
        border-radius: var(--nav-item-height);
        position: relative;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      }

      nav::after {
        content: '';
        display: block;
        width: calc(var(--nav-item-width) - var(--nav-item-padding));
        height: calc(var(--nav-item-height) + var(--nav-item-padding));
        background: #e2e8f0;
        position: absolute;
        top: 0;
        left: 0;
        margin: calc(var(--nav-item-padding) / 2);
        border-radius: var(--nav-item-height);
        transition: var(--transition-speed) all cubic-bezier(0.42, 0, 0.51, 1.49);
      }

      nav ul {
        list-style: none;
        margin: 0;
        padding: 0;
        display: flex;
        align-items: center;
        height: var(--nav-item-height);
        position: relative;
        z-index: 10;
      }

      nav ul li {
        font-size: 1.3rem;
        font-weight: 600;
        width: var(--nav-item-width);
        text-align: center;
      }

      nav ul li label {
        color: #999;
      }

      #nav-home:checked ~ nav::after {
        transform: translateX(0);
      }

      #nav-category:checked ~ nav::after {
        transform: translateX(calc(var(--nav-item-width) * 1));
      }

      #nav-products:checked ~ nav::after {
        transform: translateX(calc(var(--nav-item-width) * 2));
      }

      #nav-about:checked ~ nav::after {
        transform: translateX(calc(var(--nav-item-width) * 3));
      }

      #nav-home:checked ~ nav label[for='nav-home'],
      #nav-category:checked ~ nav label[for='nav-category'],
      #nav-products:checked ~ nav label[for='nav-products'],
      #nav-about:checked ~ nav label[for='nav-about'] {
        color: #ff4c00;
      }

      input[type='radio'] {
        display: none;
      }

      .content {
        width: calc(var(--nav-item-width) * 4);
        height: 400px;
        background-color: #fff;
        margin-top: var(--nav-item-height);
        border-radius: var(--nav-item-padding);
        display: flex;
        overflow: hidden;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      }

      .content div {
        width: calc(var(--nav-item-width) * 4);
        background-color: #000;
        flex-shrink: 0;
        transition: var(--transition-speed) all ease-in-out;
      }

      .content div:nth-child(1) {
        background-color: #22d3ee;
      }

      .content div:nth-child(2) {
        background-color: #4ade80;
      }

      .content div:nth-child(3) {
        background-color: #f472b6;
      }

      .content div:nth-child(4) {
        background-color: #facc15;
      }

      #nav-home:checked ~ .content div {
        transform: translateX(0);
      }

      #nav-category:checked ~ .content div {
        transform: translateX(calc(var(--nav-item-width) * 4 * -1));
      }

      #nav-products:checked ~ .content div {
        transform: translateX(calc(var(--nav-item-width) * 4 * -2));
      }

      #nav-about:checked ~ .content div {
        transform: translateX(calc(var(--nav-item-width) * 4 * -3));
      }
    </style>
  </head>

  <body>
    <div class="container">
      <input type="radio" name="nav" id="nav-home" checked />
      <input type="radio" name="nav" id="nav-category" />
      <input type="radio" name="nav" id="nav-products" />
      <input type="radio" name="nav" id="nav-about" />

      <nav>
        <ul>
          <li>
            <label for="nav-home"><span>Home</span></label>
          </li>

          <li>
            <label for="nav-category"><span>Category</span></label>
          </li>

          <li>
            <label for="nav-products"><span>Products</span></label>
          </li>

          <li>
            <label for="nav-about"><span>About</span></label>
          </li>
        </ul>
      </nav>

      <div class="content">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
    </div>
  </body>
</html>

Contributors

The avatar of contributor named as why why

Changelog

Released under the MIT License.

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.