bltcn
2023-11-23 911fb3421b9867a0b27f57dfc0912f33d9e779e8
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
 
<script>
    import { addResizeListener, removeResizeListener, toObject } from './util'
    import scrollbarWidth from './scrollbar-width'
    import Bar from './bar.vue'
    require('./assets/css/index.scss')
 
    export default {
        name: 'ui-scrollbar',
        components: { Bar },
        props: {
            native: Boolean,
            childOverWidth: {
                type: Boolean,
                default: true
            },
            wrapStyle: {},
            wrapClass: {},
            viewClass: {},
            viewStyle: {},
            noresize: Boolean, // 如果 container 尺寸不会发生变化,最好设置它可以优化性能
            tag: {
                type: String,
                default: 'div'
            }
        },
 
        data () {
            return {
                sizeWidth: '0',
                sizeHeight: '0',
                moveX: 0,
                moveY: 0,
                scrollbarMiddle: 0
            }
        },
 
        computed: {
            wrap () {
                return this.$refs.wrap
            }
        },
 
        mounted () {
            if (this.native) return
            this.$nextTick(this.update)
            !this.noresize && addResizeListener(this.$refs.resize, this.update)
            !this.noresize && addResizeListener(this.$refs.wrap, this.update)
        },
 
        beforeDestroy () {
            if (this.native) return
            !this.noresize && removeResizeListener(this.$refs.resize, this.update)
            !this.noresize && removeResizeListener(this.$refs.wrap, this.update)
        },
 
        methods: {
            handleScroll () {
                const wrap = this.wrap
 
                this.moveY = (wrap.scrollTop * 100) / wrap.clientHeight
                this.moveX = (wrap.scrollLeft * 100) / wrap.clientWidth
                if (wrap.scrollTop + wrap.clientHeight === wrap.scrollHeight) {
                    this.$emit('scrollbottom')
                }
                if (wrap.scrollTop === 0) {
                    this.$emit('scrolltop')
                }
                this.$emit('scrollMove', {
                    moveX: wrap.scrollLeft,
                    moveY: wrap.scrollTop,
                    clientX: wrap.clientWidth,
                    clientY: wrap.clientHeight,
                    scrollHeight: wrap.scrollHeight,
                    scrollWidth: wrap.scrollWidth
                })
            },
 
            update () {
                let heightPercentage = null
                let widthPercentage = null
                const wrap = this.wrap
                if (!wrap) return
 
                heightPercentage = (wrap.clientHeight * 100) / wrap.scrollHeight
                widthPercentage = (wrap.clientWidth * 100) / wrap.scrollWidth
 
                this.sizeHeight = heightPercentage < 100 ? heightPercentage + '%' : ''
                this.sizeWidth = widthPercentage < 100 ? widthPercentage + '%' : ''
            },
            continueScroll (rowNum) {
                const wrap = this.wrap
                if (this.scrollbarMiddle === 0) {
                    this.scrollbarMiddle = wrap.scrollTop / 2 + wrap.clientHeight / 2 - wrap.clientHeight / rowNum
                }
                wrap.scrollTop = this.scrollbarMiddle
            }
        },
 
        render (h) {
            const gutter = scrollbarWidth()
            let style = this.wrapStyle
 
            if (gutter) {
                const gutterWith = `-${gutter}px`
                let gutterStyle = ''
 
                let overFlowXStr = ''
                let overflowYStr = ''
                if (style && style.length > 0) {
                    style.forEach((styleItem) => {
                        if (styleItem['overflow-x'] || styleItem.overflowX) {
                            overFlowXStr = styleItem['overflow-x'] || styleItem.overflowX
                        }
                        if (styleItem['overflow-y'] || styleItem.overflowY) {
                            overflowYStr = styleItem['overflow-y'] || styleItem.overflowY
                        }
                    })
                }
 
                if (overFlowXStr === 'hidden') {
                    gutterStyle = `margin-right: ${gutterWith};`
                } else {
                    gutterStyle = `margin-bottom: ${gutterWith}; margin-right: ${gutterWith};`
                }
 
                if (Array.isArray(this.wrapStyle)) {
                    style = toObject(this.wrapStyle)
                    if (overFlowXStr !== 'hidden') {
                        style.marginBottom = gutterWith
                    }
                    if (overflowYStr !== 'hidden') {
                        style.marginRight = gutterWith
                    }
                } else if (typeof this.wrapStyle === 'string') {
                    style += gutterStyle
                } else {
                    style = gutterStyle
                }
            }
            const view = h(
                this.tag,
                {
                    class: ['ui-scrollbar__view', this.viewClass],
                    style: this.viewStyle,
                    ref: 'resize'
                },
                this.$slots.default
            )
            const wrap = (
                <div
                    ref="wrap"
                    style={style}
                    onScroll={this.handleScroll}
                    class={[this.wrapClass, 'ui-scrollbar__wrap', gutter ? '' : 'ui-scrollbar__wrap--hidden-default']}
                >{[view]}</div>
            )
            let nodes
 
            if (!this.native) {
                nodes = [
                    wrap,
                    <Bar move={this.moveX} size={this.sizeWidth}></Bar>,
                    <Bar vertical move={this.moveY} size={this.sizeHeight}></Bar>
                ]
            } else {
                nodes = [
                <div ref="wrap" class={[this.wrapClass, 'ui-scrollbar__wrap']} style={style}>{[view]}</div>
                ]
            }
            return h('div', { class: ['ui-scrollbar', { 'child-over-width': !this.childOverWidth }] }, nodes)
        }
    }
</script>