detail.vue 1.82 KB
<template>
  <div class="home-wrapper">
    <div class="home-body">
      <router-view></router-view>
      <div class="side-wrapper">
        <side-article :sideClassify="BROWSE_STATUS"></side-article>
        <side-article
          :sideClassify="RECOMMEND_STATUS"
          :class="{ 'side-sticky': showSide && showSticky }"
        ></side-article>
        <label-classify
          :class="[showSide && showSticky ? 'label-sticky' : 'side-sticky']"
        ></label-classify>
      </div>
    </div>
  </div>
</template>

<script>
import SideArticle from "components/sideArticle";
import { BROWSE_STATUS, RECOMMEND_STATUS } from "src/constant/side";
import DetailContent from "./components/detailContent";
import LabelClassify from "components/labelClassify";
export default {
  name: "labelComponent",
  components: {
    SideArticle,
    DetailContent,
    LabelClassify,
  },
  props: {},
  computed: {},
  data() {
    return {
      BROWSE_STATUS,
      RECOMMEND_STATUS,
      scrollTop: 0,
      showSticky: false,
      showSide: true,
    };
  },
  watch: {},
  created() { },
  mounted() {
    const clientHeight = document.documentElement.clientHeight;
    this.showSide = clientHeight > 840; // 840 = 665 + 170(label分类高度) + 之间间距
    window.addEventListener("scroll", this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    handleScroll() {
      this.scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      this.showSticky = this.scrollTop > 665; // 665是一个side-article组件的高度
    },
  },
};
</script>

<style lang="less" scoped>
.side-sticky {
  position: sticky;
  top: 0;
}
.label-sticky {
  position: sticky;
  top: 665px;
}
</style>