vue实现吸顶的效果

export default {
  name: "nav-bar",
  data() {
    return {
      isFixed: false, //当滚动条高度大于152时是否定位
      scrollHeight: 152
    };
  },
  mounted() {
    window.addEventListener("scroll", this.initHeight);
  },
  methods: {
    // 实现吸顶效果,判断滚动条距离顶部的距离
    initHeight() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      this.isFixed = scrollTop > this.scrollHeight ? true : false;
    }
  },
  destroyed() {
    window.removeEventListener("scroll", this.initHeight, false);
  }
};
原文地址:https://www.cnblogs.com/chen-cheng/p/12803656.html