index.vue 1.4 KB
Newer Older
P
Pan 已提交
1
<template>
2 3 4
  <el-scrollbar ref="scrollContainer" :vertical="false" class="scroll-container" @wheel.native.prevent="handleScroll">
    <slot/>
  </el-scrollbar>
P
Pan 已提交
5 6 7
</template>

<script>
P
Pan 已提交
8 9
const padding = 15 // tag's padding

P
Pan 已提交
10
export default {
11
  name: 'ScrollPane',
P
Pan 已提交
12 13 14 15 16 17 18
  data() {
    return {
      left: 0
    }
  },
  methods: {
    handleScroll(e) {
19 20 21
      const eventDelta = e.wheelDelta || -e.deltaY * 40
      const $scrollWrapper = this.$refs.scrollContainer.$refs.wrap
      $scrollWrapper.scrollLeft = $scrollWrapper.scrollLeft + eventDelta / 4
P
Pan 已提交
22 23
    },
    moveToTarget($target) {
24
      const $container = this.$refs.scrollContainer.$el
P
Pan 已提交
25
      const $containerWidth = $container.offsetWidth
26
      const $scrollWrapper = this.$refs.scrollContainer.$refs.wrap
P
Pan 已提交
27 28
      const $targetLeft = $target.offsetLeft
      const $targetWidth = $target.offsetWidth
29
      if ($targetLeft > $containerWidth) {
P
Pan 已提交
30
        // tag in the right
31 32 33 34
        $scrollWrapper.scrollLeft = $targetLeft - $containerWidth + $targetWidth + padding
      } else {
        // tag in the left
        $scrollWrapper.scrollLeft = $targetLeft - padding
P
Pan 已提交
35
      }
P
Pan 已提交
36 37 38 39 40 41 42 43 44
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.scroll-container {
  white-space: nowrap;
  position: relative;
P
Pan 已提交
45
  overflow: hidden;
46
  width: 100%;
47 48 49 50 51 52 53
  /deep/ {
    .el-scrollbar__bar {
      bottom: 0px;
    }
    .el-scrollbar__wrap {
      height: 49px;
    }
P
Pan 已提交
54 55 56
  }
}
</style>