<template>
    <div :class="{'sticky': isSticky}" ref="stickyElement">
      <slot>这里是需要吸顶的内容</slot>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        isSticky: false,
        stickyTop: 0,
        lastScrollY: 0
      }
    },
    mounted() {
      this.stickyTop = this.$refs.stickyElement.offsetTop
      window.addEventListener('scroll', this.handleScroll)
    },
    beforeDestroy() {
      window.removeEventListener('scroll', this.handleScroll)
    },
    methods: {
      handleScroll() {
        const scrollY = window.pageYOffset || document.documentElement.scrollTop
        const delta = scrollY - this.lastScrollY
        
        // 计算吸顶状态（向上滚动时不取消吸顶，保持体验）
        if (scrollY >= this.stickyTop) {
          this.isSticky = true
        } else {
          this.isSticky = false
        }
        
        this.lastScrollY = scrollY
      }
    }
  }
  </script>
  
  <style scoped>
  .sticky {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #ffffff;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    z-index: 1000;
    transition: all 0.3s ease;
  }
  
  /* 原始状态样式（可根据需求自定义） */
  [data-v-6a5d4122] {
    /* 非吸顶时的原始样式，建议保留足够高度避免内容跳跃 */
    height: 60px; 
    line-height: 60px;
    padding: 0 20px;
    background-color: #f8f9fa;
    border-bottom: 1px solid #e9ecef;
  }
  </style>
      