-
Notifications
You must be signed in to change notification settings - Fork 2
/
scroll.vue
189 lines (178 loc) · 5.26 KB
/
scroll.vue
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
<template>
<div ref="parent" class="t-scroll-content"
@mouseenter="onMouseEnter"
@mousemove="onMouseMove"
@mouseleave="onMouseLeave"
@wheel="onWheel">
<div ref="child" class="t-scroll-content-box" :style="{transform: `translateY(${this.contentY}px)`}">
<slot></slot>
</div>
<div class="t-scroll-content-track" v-show="scrollBarVisible">
<div class="t-scroll-content-bar" ref="bar" @mousedown="onMouseDownScrollBar"
@selectstart="onSelectStartScrollBar">
<div class="t-scroll-content-inner"></div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'TScroll',
data() {
return {
scrollBarVisible: false,
isScrolling: false,
startPosition: undefined,
endPosition: undefined,
scrollBarY: 0,
contentY: 0,
barHeight: undefined,
parentHeight: undefined,
mouseIn: false
}
},
computed: {
maxScrollHeight() {
return this.parentHeight - this.barHeight
},
childHeight() {
return this.$refs.child.getBoundingClientRect().height
}
},
mounted() {
this.listenToDocument()
this.parentHeight = this.$refs.parent.getBoundingClientRect().height
this.updateScrollBar()
},
methods: {
listenToDocument() {
document.addEventListener('mousemove', e => this.onMouseMoveScrollBar(e))
document.addEventListener('mouseup', e => this.onMouseUpScrollBar())
},
calculateContentYFromDeltaY(deltaY) {
let contentY = this.contentY
if (deltaY > 20) {
contentY -= 20 * 3
} else if (deltaY < -20) {
contentY -= -20 * 3
} else {
contentY -= deltaY * 3
}
return contentY
},
onWheel(e) {
this.updateContentY(e.deltaY, () => e.preventDefault())
this.updateScrollBar()
},
updateContentY(deltaY, fn) {
let maxHeight = this.calculateContentYMax()
this.contentY = this.calculateContentYFromDeltaY(deltaY)
if (this.contentY > 0) {
this.contentY = 0
} else if (this.contentY < -maxHeight) {
this.contentY = -maxHeight
} else {
fn && fn()
}
},
calculateContentYMax() {
let {borderTopWidth, borderBottomWidth, paddingTop, paddingBottom} = window.getComputedStyle(this.$refs.parent)
borderTopWidth = parseInt(borderTopWidth)
borderBottomWidth = parseInt(borderBottomWidth)
paddingTop = parseInt(paddingTop)
paddingBottom = parseInt(paddingBottom)
return this.childHeight - this.parentHeight + (borderTopWidth + borderTopWidth + paddingTop + paddingBottom)
},
updateScrollBar() {
let parentHeight = this.parentHeight
let childHeight = this.childHeight
this.barHeight = parentHeight * parentHeight / childHeight
this.$refs.bar.style.height = this.barHeight + 'px'
this.scrollBarY = -parentHeight * this.contentY / childHeight
this.$refs.bar.style.transform = `translateY(${this.scrollBarY}px)`
},
onMouseEnter() {
this.scrollBarVisible = true
this.mouseIn = true
},
onMouseLeave() {
this.mouseIn = false
if (!this.isScrolling) {
this.scrollBarVisible = false
}
},
onMouseMove() {
this.mouseIn = true
},
onMouseDownScrollBar(e) {
this.isScrolling = true
let {screenX, screenY} = e
this.startPosition = {x: screenX, y: screenY}
},
onMouseMoveScrollBar(e) {
if (!this.isScrolling) {return}
this.endPosition = {x: e.screenX, y: e.screenY}
let delta = {x: this.endPosition.x - this.startPosition.x, y: this.endPosition.y - this.startPosition.y}
this.scrollBarY = this.calculateScrollBarY(delta)
this.contentY = -(this.childHeight * this.scrollBarY / this.parentHeight)
this.startPosition = this.endPosition
this.$refs.bar.style.transform = `translate(0px, ${this.scrollBarY}px)`
},
calculateScrollBarY(delta) {
let newValue = parseInt(this.scrollBarY) + delta.y
if (newValue < 0) {
newValue = 0
} else if (newValue > this.maxScrollHeight) {
newValue = this.maxScrollHeight
}
return newValue
},
onMouseUpScrollBar(e) {
this.isScrolling = false
if (!this.mouseIn) {
this.scrollBarVisible = false
}
},
onSelectStartScrollBar(e) {
e.preventDefault()
}
}
}
</script>
<style lang="scss" scoped>
.t-scroll-content {
border: 1px solid red;
overflow: hidden;
position: relative;
&-box {
transition: transform 0.05s ease;
}
&-track {
position: absolute;
top: 0;
right: 0;
width: 14px;
height: 100%;
background: #FAFAFA;
border-left: 1px solid #E8E7E8;
opacity: 0.9;
}
&-bar {
position: absolute;
top: -1px;
left: 50%;
height: 40px;
width: 8px;
margin-left: -4px;
padding: 4px 0;
}
&-inner {
height: 100%;
border-radius: 4px;
background: #C2C2C2;
&:hover {
background: #7D7D7D;
}
}
}
</style>