blob: 1ce68c117a7ab1859a9c7d1d8000cd51d50303be [file] [log] [blame]
huibing.xie1f1606f2018-08-20 15:46:55 +08001<template>
2 <div class="scroll-container" ref="scrollContainer" @wheel.prevent="handleScroll">
3 <div class="scroll-wrapper" ref="scrollWrapper" :style="{left: left + 'px'}">
4 <slot></slot>
5 </div>
6 </div>
7</template>
8
9<script>
10const padding = 15 // tag's padding
11
12export default {
13 name: 'scrollPane',
14 data() {
15 return {
16 left: 0
17 }
18 },
19 methods: {
20 handleScroll(e) {
21 const eventDelta = e.wheelDelta || -e.deltaY * 3
22 const $container = this.$refs.scrollContainer
23 const $containerWidth = $container.offsetWidth
24 const $wrapper = this.$refs.scrollWrapper
25 const $wrapperWidth = $wrapper.offsetWidth
26
27 if (eventDelta > 0) {
28 this.left = Math.min(0, this.left + eventDelta)
29 } else {
30 if ($containerWidth - padding < $wrapperWidth) {
31 if (this.left < -($wrapperWidth - $containerWidth + padding)) {
32 this.left = this.left
33 } else {
34 this.left = Math.max(this.left + eventDelta, $containerWidth - $wrapperWidth - padding)
35 }
36 } else {
37 this.left = 0
38 }
39 }
40 },
41 moveToTarget($target) {
42 const $container = this.$refs.scrollContainer
43 const $containerWidth = $container.offsetWidth
44 const $targetLeft = $target.offsetLeft
45 const $targetWidth = $target.offsetWidth
46
47 if ($targetLeft < -this.left) {
48 // tag in the left
49 this.left = -$targetLeft + padding
50 } else if ($targetLeft + padding > -this.left && $targetLeft + $targetWidth < -this.left + $containerWidth - padding) {
51 // tag in the current view
52 // eslint-disable-line
53 } else {
54 // tag in the right
55 this.left = -($targetLeft - ($containerWidth - $targetWidth) + padding)
56 }
57 }
58 }
59}
60</script>
61
62<style rel="stylesheet/scss" lang="scss" scoped>
63.scroll-container {
64 white-space: nowrap;
65 position: relative;
66 overflow: hidden;
67 width: 100%;
68 .scroll-wrapper {
69 position: absolute;
70 }
71}
72</style>