1. 程式人生 > >純 CSS 利用 label + input 實現選項卡

純 CSS 利用 label + input 實現選項卡

label dev 滾動 for center -- meta absolut 獲得

clip 屬性

用於剪裁絕對定位元素。

.class {
      position:absolute;
      clip:rect(0px,60px,200px,0px);
  }

scroll-behavior: smooth;

作用在容器元素上,可以讓容器(非鼠標手勢觸發)的滾動變得平滑。利用這個css屬性可以一步將原來純css標簽直接切換,變成平滑過渡切換效果。

代碼:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>純CSS 利用label 和input 實現選項卡</title>
    <style>
    .tab label {
      padding: 10px;
      border: 1px solid #ccc;
      margin-right: -1px;
      text-align: center;
      float: left;
      overflow: hidden;
    }

    /* 清除浮動 */
    .tab::after {
      content: "";
      display: table;
      clear: both;
    }

    .box {
      height: 200px;
      border: 1px solid #ccc;
      scroll-behavior: smooth;  /* 緩沖效果 */
      overflow: hidden;
      margin-top: 10px;
    }

    .item {
      height: 100%;
      position: relative;
      overflow: hidden;
    }

    .item input {
      position: absolute;
      top: 0;
      height: 100%;
      width: 1px;
      border: 0;
      padding: 0;
      margin: 0;
      clip: rect(0 0 0 0);
    }
  </style>
</head>

<body>
    <h1>tab</h1>
    <div class="tab">
        <label for="tab1">tab1</label>
        <label for="tab2">tab2</label>
        <label for="tab3">tab3</label>
    </div>
    <div class="box">
        <div class="item">
            <!-- 點擊label時,for對應的input會獲得焦點, 並且出現在容器中的可視位置 -->
            <!-- 為禁止手機端自帶鍵盤彈出,需要給input加上“readonly”屬性 -->
            <input type="text" id="tab1" readonly>
            <p>選項卡1內容</p>
        </div>
        <div class="item">
            <input type="text" id="tab2" readonly>
            <p>選項卡2內容</p>
        </div>
        <div class="item">
            <input type="text" id="tab3" readonly>
            <p>選項卡3內容</p>
        </div>
    </div>
    <footer>
        bottom
    </footer>
</body>

</html>

純 CSS 利用 label + input 實現選項卡