tab欄 滑動距離自動切換tab欄

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link rel="icon" href="favicon.ico" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
  </head>
  <body>
    <div id="app">
      <!-- tab容器 -->
      <div id="tab-container">
        <!-- tab列表 -->
        <ul>
          <li
            v-for="(tab, index) in tabs"
            :key="index"
            :class="{active: index === activeIndex}"
            @click="switchTabs(index)"
          >
            {{ tab }}
          </li>
        </ul>
      </div>
      <!-- 內容區(qū)域 -->
      <div
        v-for="(content, index) in contents"
        :key="index"
        :id="'content-' + (index + 1)"
        class="content"
      >
        {{ content }}
      </div>
    </div>

    <script src="./js/vue.js"></script>
    <script>
      var vue = new Vue({
        data() {
          return {
            //定義tab的數(shù)據
            tabs: ["Tab 1", "Tab 2", "Tab 3"],
            //定義內容區(qū)域的數(shù)據
            contents: ["Content 1", "Content 2", "Content 3"],
            //定義當前激活的tab的索引
            activeIndex: 0,
          };
        },
        methods: {
          //定義一個方法,用于切換tab
          switchTab(index) {
            //判斷索引是否有效
            if (index >= 0 && index < this.tabs.length) {
              //更新當前激活的tab的索引
              this.activeIndex = index;
            }
          },
          switchTabs(index){
            var element = document.getElementById("content-" + (index + 1));
            element.scrollIntoView();
          },
          handleScroll() {
            //獲取滾動條的位置
            let scrollTop =
              window.pageYOffset ||
              document.documentElement.scrollTop ||
              document.body.scrollTop;
            //遍歷每個內容區(qū)域,判斷是否在可視區(qū)域內
            this.contents.forEach((content, index) => {
              //獲取內容區(qū)域的元素和位置
              var element = document.getElementById("content-" + (index + 1));
              var rect = element.getBoundingClientRect();
              var top = -rect.top;
              var height = rect.height;
              //判斷是否在可視區(qū)域內,如果是,切換到對應的tab
              if (top >= 0 && top <= height) {
                this.switchTab(index);
              }
            });
          },
        },
        mounted() {
           window.addEventListener("scroll", this.handleScroll);
        },
        destroyed() {
          window.removeEventListener("scroll", this.handleScroll);
       }

      }).$mount("#app");
    </script>
  </body>
  <style>
    /* 定義樣式 */
    #tab-container {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
    }

    #tab-container ul {
      display: flex;
    }

    #tab-container li {
      flex: 1;
      text-align: center;
    }

    .active {
      color: red;
    }

    .content {
      height: 500px;
    }
  </style>
</html>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容