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>
      <!-- 内容区域 -->
      <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的数据
            tabs: ["Tab 1", "Tab 2", "Tab 3"],
            //定义内容区域的数据
            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;
            //遍历每个内容区域,判断是否在可视区域内
            this.contents.forEach((content, index) => {
              //获取内容区域的元素和位置
              var element = document.getElementById("content-" + (index + 1));
              var rect = element.getBoundingClientRect();
              var top = -rect.top;
              var height = rect.height;
              //判断是否在可视区域内,如果是,切换到对应的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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容