ScrollXView.vue 3.31 KB
<template>
  <div class="timeline-content">
    <div
      class="left-mask mask"
      v-show="!isAtLeftEnd"
    >
      <div
        class="btn flex"
        @click="scrollTo(-400)"
      >
        <i class="el-icon-arrow-left"></i>
      </div>
    </div>
    <div
      class="right-mask mask"
      v-show="!isAtRightEnd"
    >
      <div
        class="btn flex"
        @click="scrollTo(400)"
      >
        <i class="el-icon-arrow-left"></i>
      </div>
    </div>
    <el-scrollbar
      class="timeline-scrollbar"
      ref="timelineScrollbar"
    >
      <slot></slot>
    </el-scrollbar>
  </div>
</template>

<script type='text/ecmascript-6'>
export default {
  name: 'ScrollXView',

  data () {
    return {
      isAtLeftEnd: true,  // 初始在最左侧
      isAtRightEnd: false // 初始不在最右侧
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.checkScrollPosition();
      const scrollbar = this.$refs.timelineScrollbar;
      if (scrollbar && scrollbar.wrap) {
        scrollbar.wrap.addEventListener('scroll', this.handleScroll);
      }
    })
  },
  beforeDestroy () {
    const scrollbar = this.$refs.timelineScrollbar;
    if (scrollbar && scrollbar.wrap) {
      scrollbar.wrap.removeEventListener('scroll', this.handleScroll);
    }
  },
  methods: {
    // 检查当前滚动位置
    checkScrollPosition () {
      const scrollbar = this.$refs.timelineScrollbar;
      if (!scrollbar || !scrollbar.wrap) return;

      const { scrollLeft, scrollWidth, clientWidth } = scrollbar.wrap;
      const maxScroll = scrollWidth - clientWidth;

      this.isAtLeftEnd = scrollLeft <= 0;
      this.isAtRightEnd = scrollLeft >= maxScroll - 1; // 允许1px误差
    },

    // 滚动事件处理
    handleScroll () {
      this.checkScrollPosition();
    },
    scrollTo (x) {
      const scrollbar = this.$refs.timelineScrollbar;
      if (scrollbar && scrollbar.wrap) {
        scrollbar.wrap.scrollBy({ left: x, behavior: 'smooth' });
      }
      this.$nextTick(this.checkScrollPosition);
    }
  }
}
</script>

<style scoped lang='scss'>
.flex {
  display: flex;
  align-items: center;
}
.timeline-content {
  width: 100%;
  position: relative;
  .mask {
    // opacity: 0;
    width: 72px;
    height: 100%;
    background: linear-gradient(
      90deg,
      #ffffff 0%,
      #ffffffe6 51%,
      #ffffff00 100%
    );
    position: absolute;
    z-index: 3;
    transition: all 0.15s;
    .btn {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      width: 32px;
      height: 32px;
      background: #ffffff;
      box-shadow: 0 0 4px 0 #0000001a;
      border-radius: 50%;
      cursor: pointer;
      justify-content: center;
      .el-icon-arrow-left {
        color: #222;
        font-weight: 700;
        font-size: 14px;
      }
    }
  }
  .left-mask {
    top: 0;
    left: 0;
    .btn {
      left: 20px;
    }
  }
  .right-mask {
    top: 0;
    right: -1px;
    transform: scaleX(-1);
    .btn {
      right: 20px;
    }
  }
  &:hover {
    .mask {
      opacity: 1;
    }
  }
}
.timeline-scrollbar {
  width: 100%;
  ::v-deep .el-scrollbar__wrap {
    // overflow:auto hidden !important;
    // margin-bottom: 0 !important;
  }
}
</style>