Tab + 左右切换

1,684 阅读1分钟

案例

屏幕快照 2021-06-09 09.33.46.png

html

<div class="tab-box">
    <section class="left-tab">
      <ul>
        <li
          v-for="(item, index) in tabs"
          :key="index"
          @click="itemClick(item, index, 'isLeftTabFlag')"
        >
          <div class="item-name">{{ item.name }}</div>
          <div v-if="item.selected" class="border-wh"></div>
        </li>
      </ul>
    </section>
    <section class="tab">
      <div class="box1" v-if="tabs && tabs.length > 4">
        <span @click="handleToMove('left')" class="left">
          <i class="el-icon-arrow-left"></i>
        </span>
        <span @click="handleToMove('right')" class="right">
          <i class="el-icon-arrow-right"></i>
        </span>
      </div>
      <div
        class="box2"
        :style="{ width: tabs.length * 82 + 'px' }"
        ref="tabBox"
      >
        <ul ref="tab" :style="{ width: tabs.length * 82 + 'px' }">
          <li
            v-for="(item, index) in tabs"
            @click="itemClick(item, index, 'isTopTabFlag')"
            class="tabsItem"
            :key="index"
          >
            <div class="item-name">{{ item.name }}</div>
            <div v-if="item.selected" class="border-wh"></div>
          </li>
        </ul>
      </div>
    </section>
  </div>

js


<script>
export default {
  data() {
    return {
      tabs: [
        { name: "导航一", selected: false },
        { name: "导航二", selected: false },
        { name: "导航三", selected: false },
        { name: "导航四", selected: false },
        { name: "导航五", selected: false },
        { name: "导航六", selected: false },
        { name: "导航七", selected: false },
        { name: "导航八", selected: false },
      ],
      leftIndex: 0,
    };
  },
  methods: {
    // 左移 右移
    handleToMove(flag) {
      if (flag == "left") {
        if (this.leftIndex <= 0) return;
        this.leftIndex--;
      } else {
        if (this.tabs.length - 4 <= this.leftIndex) return;
        this.leftIndex++;
      }
      let w = document.getElementsByClassName("tabsItem")[0].offsetWidth;
      let box = this.$refs.tabBox;
      box.style.marginLeft = -w * this.leftIndex + "px";
    },
    itemClick(item, index, flag) {
      // 都不选中
      this.tabs.forEach((ele, index1) => {
        this.$set(this.tabs[index1], "selected", false);
      });
      // 设置选中
      this.$set(this.tabs[index], "selected", true);

      if (flag == "isLeftTabFlag") {
        let w = document.getElementsByClassName("tabsItem")[0].offsetWidth;
        let box = this.$refs.tabBox;
        if (index > 3) {
          this.leftIndex = index - 3;
        } else {
          this.leftIndex = 0;
        }
        box.style.marginLeft = -w * this.leftIndex + "px";
      }
    },
  },
};
</script>

css


<style lang="scss" scoped>
.tab-box {
  display: flex;
  height: 200px;
  width: 100%;
  margin-top: 20px;
  background: #fff;
  border-radius: 10px;
  .tab {
    width: 368px;
    overflow: hidden;
    height: 40px;
    position: relative;
    div {
      padding: 0;
      margin: 0;
    }
    .box1 {
      width: 100%;
      overflow: hidden;
      position: absolute;
      span {
        width: 20px;
        line-height: 40px;
        height: 40px;
        cursor: pointer;
        display: inline-block;
        position: relative;
        z-index: 1;
        background: #f9fafb;
        text-align: center;
      }
      span.left {
        float: left;
      }
      span.right {
        float: right;
      }
    }
    .box2 {
      position: absolute;
      ul {
        height: 40px;
        margin-right: 20px;
        margin-left: 20px;
        position: relative;
        li {
          float: left;
          overflow: hidden;
          display: inline-block;
          height: 40px;
          background: #f9fafb;
          line-height: 440px;
          width: 82px;
          text-align: center;
          cursor: pointer;
          padding: 0;
          .item-name {
            width: 80px;
            height: 38px;
            line-height: 38px;
            text-align: center;
          }
          .border-wh {
            height: 2px;
            width: 20px;
            margin-left: 30px;
            background: coral;
          }
        }
      }
    }
  }
  .left-tab {
    height: 320px;
    width: 82px;
    background: #fff;
    ul {
      width: 100%;
      height: 100%;
      li {
        display: block;
        height: 40px;
        width: 82px;
        .item-name {
          height: 38px;
          width: 82px;
          line-height: 38px;
          text-align: center;
          cursor: pointer;
        }
        .border-wh {
          height: 2px;
          width: 20px;
          margin-left: 30px;
          background: coral;
        }
      }
    }
  }
}
</style>