component.vue 6.34 KB
<template>
  <el-container>
    <layout-header></layout-header>
    <el-container class="layout-container__component">
      <el-aside class="layout-aside__component" width="240px">
        <el-menu :default-active="activeMenu" class="layout-aside-menu__component" @select="handleSelect">
          <h4 class="side-menu__group">开发指南</h4>
          <el-menu-item v-for="(doc, index) in guideList" :key="`doc-${index}`" :index="doc.name">{{ doc.meta.title }}</el-menu-item>
          <h4 class="side-menu__group">
            组件<el-tag type="info" size="mini">{{ componentsCount }}</el-tag>
          </h4>
          <el-menu-item-group v-for="(component, idx) in componentList" :key="idx">
            <template slot="title">{{ component.group }}</template>
            <el-menu-item v-for="(data, index) in component.children" :key="index" :index="data.name">{{ data.meta.title }}</el-menu-item>
          </el-menu-item-group>
        </el-menu>
      </el-aside>
      <el-main class="layout-main__component">
        <router-view></router-view>
      </el-main>
      <el-aside class="layout-aside__preview" width="200px">
        <a class="anchor" :class="{ 'active': item.hash === currentAnchor }" v-for="(item, index) in anchorList" :key="index" @click="jumpAnchor(item)">{{ item.text }}</a>
      </el-aside>
    </el-container>
  </el-container>
</template>

<script>
import LayoutHeader from './components/header';
import { guides, components } from '@/router/routes';

const headerHeight = 80;

export default {
  name: 'componentLayout',
  components: { LayoutHeader },
  data() {
    return {
      activeMenu: 'introduce',
      guideList: guides,
      componentList: components,
      anchorList: [],
      currentAnchor: '',
    }
  },
  created() {
    const { name } = this.$route || {};
    this.activeMenu = name;
  },
  mounted() {
    this.initAnchorList();
    window.onscroll = (e) => {
      let rangeIndex = 0;
      for (let index in this.anchorList) {
        let anchor = this.anchorList[index] || {};
        const dom = document.querySelector(anchor.hash) || { offsetTop: 0 };
        if (window.pageYOffset >= dom.offsetTop - headerHeight) {
          rangeIndex = index
        }
      }
      this.currentAnchor = this.anchorList[rangeIndex].hash;
    };
    // 初次加载时根据url hash跳转到指定锚点
    if (this.$route.hash) {
      const anchor = document.querySelector(this.$route.hash);
      document.documentElement.scrollTop = anchor.offsetTop - headerHeight;
    }
  },
  watch:{
    $route(to, from) {
      this.currentAnchor = to.hash;
      if (to.path !== from.path) {
        this.$nextTick(this.initAnchorList);
      }
    }
  },
  computed: {
    componentsCount() {
      let result = 0;
      this.componentList.forEach(item => {
        result += (item.children || []).length;
      });
      return result;
    }
  },
  methods: {
    isBottom() {
      // 变量scrollTop是滚动条滚动时,距离顶部的距离
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      // 变量windowHeight是可视区的高度
      const windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
      // 变量scrollHeight是滚动条的总高度
      const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
      // 滚动条到底部的条件
      return scrollTop + windowHeight === scrollHeight;
    },
    initAnchorList() {
      document.documentElement.scrollTop = 0;
      this.anchorList = [];
      const domList = document.querySelectorAll('.header-anchor');
      let anchorList = [];
      for (let index in domList) {
        let dom = domList[index] || {};
        if (dom.addEventListener) {
          dom.addEventListener('click', e => {
            // 阻止a标签的默认行为
            e = e || window.event;
            e.preventDefault();
            document.documentElement.scrollTop = dom.offsetTop - headerHeight;
            this.$router.replace({ path: this.$route.path, hash: dom.hash });
          });
        }
        if (dom.parentNode) {
          const text = `${dom.parentNode.innerHTML}`.replace(/<\/?.*[^>]*>/g, '').trim();
          if (text === 'API') {
            break;
          } else {
            anchorList.push({
              text: `${dom.parentNode.innerHTML}`.replace(/<\/?.*[^>]*>/g, '').trim(),
              href: dom.href,
              hash: dom.hash,
            });
          }
        }
      }
      this.anchorList = anchorList;
      this.currentAnchor = (anchorList[0] || {}).hash;
    },
    jumpAnchor({ hash, offsetTop }) {
      this.$router.replace({ path: this.$route.path, hash });
      const dom = document.querySelector(hash) || { offsetTop: 0 };
      document.documentElement.scrollTop = dom.offsetTop - headerHeight;
    },
    handleSelect(key) {
      this.$router.push({ name: key });
    }
  }
}
</script>

<style lang="scss">
@import '@/styles/variables.scss';

.layout-container__component {
  margin-top: 60px;
  .layout-aside__component {
    &::after {
      position: absolute;
      top: 50%;
      right: 0;
      content: '';
      display: inline-block;
      transform: translateY(-50%);
      height: 90%;
      border-right: 1px solid #e6e6e6;
    }
    left: 0;
    position: fixed;
    height: calc(100vh - 60px);
    background: #fff;
    .el-menu-item {
      border-right: 3px solid rgba(0, 0, 0, 0);
    }
    .el-menu-item:hover {
      color: $primary;
      background-color: rgba(0, 0, 0, 0);
    }
    .el-menu-item.is-active {
      background-color: rgba($primary, 0.1);
      border-right: 3px solid $primary;
    }
    .el-menu-item, .el-submenu__title {
      height: 40px;
      line-height: 40px;
    }
    .layout-aside-menu__component {
      background: transparent;
      border-right: 0;
    }
  }
  .layout-aside__preview {
    position: fixed;
    right: 0;
    height: calc(100vh - 60px);
    width: 150px !important;
    padding: 30px 0;
    .anchor {
      display: inline-block;
      font-size: 12px;
      text-decoration: none;
      padding: 5px 20px;
      color: $text;
      transition: all 300ms;
      border-left: 1px solid $border;
      cursor: pointer;
    }
    .anchor:hover,
    .active {
      color: $primary;
      border-left: 1px solid $primary;
    }
  }
  .layout-main__component {
    margin-left: 240px;
    margin-right: 150px;
    padding: 20px 40px;
  }
}
</style>