index.vue 1.2 KB
Newer Older
P
Pan 已提交
1
<template>
P
Pan 已提交
2 3
  <div class="scroll-container" ref="scrollContainer" @mousewheel="handleScroll">
    <div class="scroll-wrapper" ref="scrollWrapper" :style="{left: left + 'px'}">
P
Pan 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'scrollPane',
  data() {
    return {
      left: 0
    }
  },
  methods: {
    handleScroll(e) {
      e.preventDefault()
      const $container = this.$refs.scrollContainer
      const $containerWidth = $container.offsetWidth
      const $wrapper = this.$refs.scrollWrapper
      const $wrapperWidth = $wrapper.offsetWidth
      if (e.wheelDelta > 0) {
        this.left = Math.min(0, this.left + e.wheelDelta)
      } else {
        if ($containerWidth - 100 < $wrapperWidth) {
          if (this.left < -($wrapperWidth - $containerWidth + 100)) {
            this.left = this.left
          } else {
            this.left = Math.max(this.left + e.wheelDelta, $containerWidth - $wrapperWidth - 100)
          }
        } else {
          this.left = 0
        }
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.scroll-container {
  white-space: nowrap;
  position: relative;
  .scroll-wrapper {
    position: absolute;
  }
}
</style>