vben
2020-12-23 7db0c5c49f23a4ab4958b3f73d47516deafa6166
1
2
3
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<template>
  <div class="scrollbar">
    <div
      ref="wrap"
      :class="[wrapClass, 'scrollbar__wrap', native ? '' : 'scrollbar__wrap--hidden-default']"
      :style="style"
      @scroll="handleScroll"
    >
      <component :is="tag" ref="resize" :class="['scrollbar__view', viewClass]" :style="viewStyle">
        <slot></slot>
      </component>
    </div>
    <template v-if="!native">
      <bar :move="moveX" :size="sizeWidth" />
      <bar vertical :move="moveY" :size="sizeHeight" />
    </template>
  </div>
</template>
<script lang="ts">
  import { addResizeListener, removeResizeListener } from '/@/utils/event/resizeEvent';
 
  import { toObject } from './util';
  import {
    defineComponent,
    ref,
    onMounted,
    onBeforeUnmount,
    nextTick,
    provide,
    computed,
  } from 'vue';
  import Bar from './bar';
 
  export default defineComponent({
    name: 'Scrollbar',
    components: { Bar },
    props: {
      native: {
        type: Boolean,
        default: false,
      },
      wrapStyle: {
        type: [String, Array],
        default: '',
      },
      wrapClass: {
        type: [String, Array],
        default: '',
      },
      viewClass: {
        type: [String, Array],
        default: '',
      },
      viewStyle: {
        type: [String, Array],
        default: '',
      },
      noresize: Boolean, // 如果 container 尺寸不会发生变化,最好设置它可以优化性能
      tag: {
        type: String,
        default: 'div',
      },
    },
    setup(props) {
      const sizeWidth = ref('0');
      const sizeHeight = ref('0');
      const moveX = ref(0);
      const moveY = ref(0);
      const wrap = ref<any>(null);
      const resize = ref<any>(null);
 
      provide('scroll-bar-wrap', wrap);
 
      const handleScroll = () => {
        if (!props.native) {
          moveY.value = (wrap.value.scrollTop * 100) / wrap.value.clientHeight;
          moveX.value = (wrap.value.scrollLeft * 100) / wrap.value.clientWidth;
        }
      };
 
      const update = () => {
        if (!wrap.value) return;
 
        const heightPercentage = (wrap.value.clientHeight * 100) / wrap.value.scrollHeight;
        const widthPercentage = (wrap.value.clientWidth * 100) / wrap.value.scrollWidth;
 
        sizeHeight.value = heightPercentage < 100 ? heightPercentage + '%' : '';
        sizeWidth.value = widthPercentage < 100 ? widthPercentage + '%' : '';
      };
 
      onMounted(() => {
        if (props.native) return;
        nextTick(update);
        !props.noresize && addResizeListener(resize.value, update);
      });
 
      onBeforeUnmount(() => {
        if (props.native) return;
        !props.noresize && removeResizeListener(resize.value, update);
      });
      const style = computed(() => {
        let style: any = props.wrapStyle;
        if (Array.isArray(props.wrapStyle)) {
          style = toObject(props.wrapStyle);
        }
        return style;
      });
      return {
        moveX,
        moveY,
        sizeWidth,
        sizeHeight,
        style,
        wrap,
        resize,
        update,
        handleScroll,
      };
    },
  });
</script>
<style lang="less">
  .scrollbar {
    position: relative;
    height: 100%;
    overflow: hidden;
 
    &__wrap {
      height: 100%;
      overflow: scroll;
 
      &--hidden-default {
        scrollbar-width: none;
 
        &::-webkit-scrollbar {
          display: none;
          width: 0;
          height: 0;
          opacity: 0;
        }
      }
    }
 
    &__thumb {
      position: relative;
      display: block;
      width: 0;
      height: 0;
      cursor: pointer;
      background-color: rgba(144, 147, 153, 0.3);
      border-radius: inherit;
      transition: 0.3s background-color;
 
      &:hover {
        background-color: rgba(144, 147, 153, 0.5);
      }
    }
 
    &__bar {
      position: absolute;
      right: 2px;
      bottom: 2px;
      z-index: 1;
      border-radius: 4px;
      opacity: 0;
      -webkit-transition: opacity 80ms ease;
      transition: opacity 80ms ease;
 
      &.is-vertical {
        top: 2px;
        width: 6px;
 
        & > div {
          width: 100%;
        }
      }
 
      &.is-horizontal {
        left: 2px;
        height: 6px;
 
        & > div {
          height: 100%;
        }
      }
    }
  }
 
  .scrollbar:active > .scrollbar__bar,
  .scrollbar:focus > .scrollbar__bar,
  .scrollbar:hover > .scrollbar__bar {
    opacity: 1;
    transition: opacity 340ms ease-out;
  }
</style>