@cocreate/scrollbar 0.0.0-stage → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/FUNDING.yml +3 -0
- package/.github/workflows/automated.yml +58 -0
- package/.github/workflows/manual.yml +43 -0
- package/CHANGELOG.md +1396 -0
- package/CONTRIBUTING.md +97 -0
- package/CoCreate.config.js +23 -0
- package/LICENSE +21 -0
- package/README.md +147 -2
- package/demo/index.html +196 -0
- package/docs/index.html +439 -0
- package/package.json +54 -4
- package/prettier.config.js +16 -0
- package/release.config.js +30 -0
- package/src/index.css +172 -0
- package/src/index.js +475 -0
- package/webpack.config.js +65 -0
package/src/index.css
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* CoCreateScrollbar - Modern Overlay Scrollbar Stylesheet
|
|
3
|
+
* Version 2.1.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/* Non-scrolling host for the native viewport and overlay tracks */
|
|
7
|
+
[scrollbar] {
|
|
8
|
+
position: relative !important;
|
|
9
|
+
overflow: hidden !important;
|
|
10
|
+
box-sizing: border-box;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/* Scrollable viewport - hides native scrollbars across browsers */
|
|
14
|
+
[scrollbar] > .scrollbar-viewport {
|
|
15
|
+
width: 100%;
|
|
16
|
+
height: 100%;
|
|
17
|
+
overflow: auto !important;
|
|
18
|
+
box-sizing: border-box;
|
|
19
|
+
scrollbar-width: none !important; /* Firefox */
|
|
20
|
+
-ms-overflow-style: none !important; /* IE 10+ and Edge */
|
|
21
|
+
-webkit-overflow-scrolling: touch; /* Smooth iOS scrolling */
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
[scrollbar] > .scrollbar-viewport::-webkit-scrollbar {
|
|
25
|
+
display: none !important;
|
|
26
|
+
width: 0 !important;
|
|
27
|
+
height: 0 !important;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/* Overlay track base */
|
|
31
|
+
[scrollbar] > .scrollbar-track {
|
|
32
|
+
position: absolute;
|
|
33
|
+
z-index: 30;
|
|
34
|
+
pointer-events: none;
|
|
35
|
+
border-radius: 9999px;
|
|
36
|
+
opacity: 0;
|
|
37
|
+
background-color: var(--cc-track-bg, rgba(0, 0, 0, 0.05));
|
|
38
|
+
transition:
|
|
39
|
+
opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
|
|
40
|
+
background-color 0.2s ease,
|
|
41
|
+
width 0.15s ease,
|
|
42
|
+
height 0.15s ease;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* Interactive thumb base */
|
|
46
|
+
[scrollbar] > .scrollbar-track > .scrollbar-thumb {
|
|
47
|
+
pointer-events: auto;
|
|
48
|
+
border-radius: 9999px;
|
|
49
|
+
cursor: pointer;
|
|
50
|
+
will-change: transform;
|
|
51
|
+
background-color: var(--cc-thumb-bg, rgba(129, 140, 248, 0.75));
|
|
52
|
+
transition: background-color 0.15s ease, opacity 0.2s ease;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* Vertical track positioning and sizing */
|
|
56
|
+
[scrollbar] > .scrollbar-track-y {
|
|
57
|
+
top: 4px;
|
|
58
|
+
right: 4px;
|
|
59
|
+
bottom: 4px;
|
|
60
|
+
width: 6px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
[scrollbar] > .scrollbar-track-y.pos-left {
|
|
64
|
+
right: auto;
|
|
65
|
+
left: 4px;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
[scrollbar] > .scrollbar-track-y > .scrollbar-thumb {
|
|
69
|
+
width: 100%;
|
|
70
|
+
height: 24px;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* Horizontal track positioning and sizing */
|
|
74
|
+
[scrollbar] > .scrollbar-track-x {
|
|
75
|
+
right: 4px;
|
|
76
|
+
bottom: 4px;
|
|
77
|
+
left: 4px;
|
|
78
|
+
height: 6px;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
[scrollbar] > .scrollbar-track-x.pos-top {
|
|
82
|
+
top: 4px;
|
|
83
|
+
bottom: auto;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
[scrollbar] > .scrollbar-track-x > .scrollbar-thumb {
|
|
87
|
+
width: 24px;
|
|
88
|
+
height: 100%;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/* Interactive hover and drag expansion states */
|
|
92
|
+
[scrollbar] > .scrollbar-track > .scrollbar-thumb:hover,
|
|
93
|
+
[scrollbar] > .scrollbar-track > .scrollbar-thumb.is-dragging {
|
|
94
|
+
background-color: var(--cc-thumb-hover-bg, rgba(165, 180, 252, 1));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
[scrollbar] > .scrollbar-track-y:hover,
|
|
98
|
+
[scrollbar] > .scrollbar-track-y.is-dragging {
|
|
99
|
+
width: 8px;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
[scrollbar] > .scrollbar-track-x:hover,
|
|
103
|
+
[scrollbar] > .scrollbar-track-x.is-dragging {
|
|
104
|
+
height: 8px;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* Auto mode: show while scrolling, hovering, or dragging */
|
|
108
|
+
[scrollbar].scrolling > .scrollbar-track,
|
|
109
|
+
[scrollbar]:hover > .scrollbar-track,
|
|
110
|
+
[scrollbar].is-dragging > .scrollbar-track {
|
|
111
|
+
opacity: 1;
|
|
112
|
+
pointer-events: auto;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* Always-visible mode */
|
|
116
|
+
[scrollbar][scrollbar-visible="show"] > .scrollbar-track,
|
|
117
|
+
[scrollbar].visible-show > .scrollbar-track {
|
|
118
|
+
opacity: 1 !important;
|
|
119
|
+
pointer-events: auto !important;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* Axis disabling */
|
|
123
|
+
[scrollbar].disable-y > .scrollbar-track-y,
|
|
124
|
+
[scrollbar].disable-x > .scrollbar-track-x {
|
|
125
|
+
display: none !important;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
[scrollbar].disable-y > .scrollbar-viewport {
|
|
129
|
+
overflow-y: hidden !important;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
[scrollbar].disable-x > .scrollbar-viewport {
|
|
133
|
+
overflow-x: hidden !important;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/* Prevent text selection while dragging */
|
|
137
|
+
body.scrollbar-dragging {
|
|
138
|
+
user-select: none !important;
|
|
139
|
+
-webkit-user-select: none !important;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/* Built-in preset color themes */
|
|
143
|
+
.theme-indigo {
|
|
144
|
+
--cc-thumb-bg: rgba(129, 140, 248, 0.75);
|
|
145
|
+
--cc-thumb-hover-bg: rgba(165, 180, 252, 1);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.theme-emerald {
|
|
149
|
+
--cc-thumb-bg: rgba(52, 211, 153, 0.75);
|
|
150
|
+
--cc-thumb-hover-bg: rgba(110, 231, 183, 1);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.theme-rose {
|
|
154
|
+
--cc-thumb-bg: rgba(251, 113, 133, 0.75);
|
|
155
|
+
--cc-thumb-hover-bg: rgba(253, 164, 175, 1);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.theme-amber {
|
|
159
|
+
--cc-thumb-bg: rgba(251, 191, 36, 0.75);
|
|
160
|
+
--cc-thumb-hover-bg: rgba(252, 211, 77, 1);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.theme-glass {
|
|
164
|
+
--cc-thumb-bg: rgba(255, 255, 255, 0.3);
|
|
165
|
+
--cc-thumb-hover-bg: rgba(255, 255, 255, 0.6);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
.theme-dark {
|
|
169
|
+
--cc-thumb-bg: rgba(255, 255, 255, 0.25);
|
|
170
|
+
--cc-thumb-hover-bg: rgba(255, 255, 255, 0.5);
|
|
171
|
+
--cc-track-bg: rgba(255, 255, 255, 0.05);
|
|
172
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CoCreateScrollbar - Modern Overlay Scrollbar JavaScript Engine
|
|
3
|
+
* Version 2.1.0
|
|
4
|
+
*/
|
|
5
|
+
import Observer from '@cocreate/observer';
|
|
6
|
+
import './index.css';
|
|
7
|
+
|
|
8
|
+
const selector = '[scrollbar]';
|
|
9
|
+
const optionAttributes = [
|
|
10
|
+
'scrollbar-type',
|
|
11
|
+
'scrollbar-pos',
|
|
12
|
+
'scrollbar-visible'
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const CoCreateScrollbar = (function () {
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
var instances = new WeakMap();
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Core ScrollInstance Class
|
|
22
|
+
*/
|
|
23
|
+
function ScrollInstance(el, config) {
|
|
24
|
+
this.element = el;
|
|
25
|
+
this.options = Object.assign({
|
|
26
|
+
visible: 'auto',
|
|
27
|
+
fadeDelay: 1000,
|
|
28
|
+
hScroll: { behavior: 'scroll', pos: 'bottom' },
|
|
29
|
+
vScroll: { behavior: 'scroll', pos: 'right' }
|
|
30
|
+
}, config || {});
|
|
31
|
+
|
|
32
|
+
this.minThumbSize = 24;
|
|
33
|
+
this.scrollTimer = null;
|
|
34
|
+
this.isDragging = false;
|
|
35
|
+
this.dragAxis = null;
|
|
36
|
+
this.startPos = { x: 0, y: 0 };
|
|
37
|
+
this.startScroll = { x: 0, y: 0 };
|
|
38
|
+
|
|
39
|
+
this.viewport = null;
|
|
40
|
+
this.trackV = null;
|
|
41
|
+
this.thumbV = null;
|
|
42
|
+
this.trackH = null;
|
|
43
|
+
this.thumbH = null;
|
|
44
|
+
this.resizeObserver = null;
|
|
45
|
+
this.createdTrackV = false;
|
|
46
|
+
this.createdTrackH = false;
|
|
47
|
+
this.createdThumbV = false;
|
|
48
|
+
this.createdThumbH = false;
|
|
49
|
+
this.viewportClassAdded = false;
|
|
50
|
+
this.initialized = false;
|
|
51
|
+
|
|
52
|
+
this.init();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
ScrollInstance.prototype.parseAttributes = function () {
|
|
56
|
+
var types = (this.element.getAttribute('scrollbar-type') || 'scrollx scrolly').toLowerCase();
|
|
57
|
+
this.options.hScroll.behavior = types.indexOf('scrollx') > -1 ? 'scroll' : 'disabled';
|
|
58
|
+
this.options.vScroll.behavior = types.indexOf('scrolly') > -1 ? 'scroll' : 'disabled';
|
|
59
|
+
|
|
60
|
+
var pos = (this.element.getAttribute('scrollbar-pos') || 'right bottom').toLowerCase();
|
|
61
|
+
this.options.vScroll.pos = pos.indexOf('left') > -1 ? 'left' : 'right';
|
|
62
|
+
this.options.hScroll.pos = pos.indexOf('top') > -1 ? 'top' : 'bottom';
|
|
63
|
+
|
|
64
|
+
var visible = (this.element.getAttribute('scrollbar-visible') || 'auto').toLowerCase();
|
|
65
|
+
this.options.visible = ['auto', 'show'].indexOf(visible) > -1 ? visible : 'auto';
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
ScrollInstance.prototype.applyOptions = function () {
|
|
69
|
+
this.element.classList.toggle('visible-show', this.options.visible === 'show');
|
|
70
|
+
this.element.classList.toggle('disable-y', this.options.vScroll.behavior !== 'scroll');
|
|
71
|
+
this.element.classList.toggle('disable-x', this.options.hScroll.behavior !== 'scroll');
|
|
72
|
+
if (this.trackV) {
|
|
73
|
+
this.trackV.classList.toggle('pos-left', this.options.vScroll.pos === 'left');
|
|
74
|
+
}
|
|
75
|
+
if (this.trackH) {
|
|
76
|
+
this.trackH.classList.toggle('pos-top', this.options.hScroll.pos === 'top');
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
ScrollInstance.prototype.refresh = function () {
|
|
81
|
+
this.parseAttributes();
|
|
82
|
+
this.applyOptions();
|
|
83
|
+
this.update();
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
ScrollInstance.prototype.init = function () {
|
|
87
|
+
this.parseAttributes();
|
|
88
|
+
|
|
89
|
+
// The first non-track child is the existing native scroll container.
|
|
90
|
+
for (var i = 0; i < this.element.children.length; i++) {
|
|
91
|
+
var child = this.element.children[i];
|
|
92
|
+
if (!child.classList.contains('scrollbar-track')) {
|
|
93
|
+
this.viewport = child;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!this.viewport) return;
|
|
98
|
+
|
|
99
|
+
this.viewportClassAdded = !this.viewport.classList.contains('scrollbar-viewport');
|
|
100
|
+
this.viewport.classList.add('scrollbar-viewport');
|
|
101
|
+
|
|
102
|
+
// Vertical Track & Thumb Setup
|
|
103
|
+
this.trackV = this.element.querySelector(':scope > .scrollbar-track-y');
|
|
104
|
+
if (!this.trackV) {
|
|
105
|
+
this.trackV = document.createElement('div');
|
|
106
|
+
this.trackV.className = 'scrollbar-track scrollbar-track-y';
|
|
107
|
+
this.thumbV = document.createElement('div');
|
|
108
|
+
this.thumbV.className = 'scrollbar-thumb';
|
|
109
|
+
this.trackV.appendChild(this.thumbV);
|
|
110
|
+
this.element.appendChild(this.trackV);
|
|
111
|
+
this.createdTrackV = true;
|
|
112
|
+
this.createdThumbV = true;
|
|
113
|
+
} else {
|
|
114
|
+
this.thumbV = this.trackV.querySelector('.scrollbar-thumb');
|
|
115
|
+
if (!this.thumbV) {
|
|
116
|
+
this.thumbV = document.createElement('div');
|
|
117
|
+
this.thumbV.className = 'scrollbar-thumb';
|
|
118
|
+
this.trackV.appendChild(this.thumbV);
|
|
119
|
+
this.createdThumbV = true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Horizontal Track & Thumb Setup
|
|
124
|
+
this.trackH = this.element.querySelector(':scope > .scrollbar-track-x');
|
|
125
|
+
if (!this.trackH) {
|
|
126
|
+
this.trackH = document.createElement('div');
|
|
127
|
+
this.trackH.className = 'scrollbar-track scrollbar-track-x';
|
|
128
|
+
this.thumbH = document.createElement('div');
|
|
129
|
+
this.thumbH.className = 'scrollbar-thumb';
|
|
130
|
+
this.trackH.appendChild(this.thumbH);
|
|
131
|
+
this.element.appendChild(this.trackH);
|
|
132
|
+
this.createdTrackH = true;
|
|
133
|
+
this.createdThumbH = true;
|
|
134
|
+
} else {
|
|
135
|
+
this.thumbH = this.trackH.querySelector('.scrollbar-thumb');
|
|
136
|
+
if (!this.thumbH) {
|
|
137
|
+
this.thumbH = document.createElement('div');
|
|
138
|
+
this.thumbH.className = 'scrollbar-thumb';
|
|
139
|
+
this.trackH.appendChild(this.thumbH);
|
|
140
|
+
this.createdThumbH = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Setup Observers & Listeners
|
|
145
|
+
this.applyOptions();
|
|
146
|
+
this.setupObservers();
|
|
147
|
+
this.bindEvents();
|
|
148
|
+
this.update();
|
|
149
|
+
this.initialized = true;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
ScrollInstance.prototype.setupObservers = function () {
|
|
153
|
+
var self = this;
|
|
154
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
155
|
+
this.resizeObserver = new ResizeObserver(function () { self.update(); });
|
|
156
|
+
this.resizeObserver.observe(this.element);
|
|
157
|
+
this.resizeObserver.observe(this.viewport);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
ScrollInstance.prototype.update = function () {
|
|
162
|
+
if (!this.viewport) return;
|
|
163
|
+
|
|
164
|
+
var scrollTop = this.viewport.scrollTop;
|
|
165
|
+
var scrollLeft = this.viewport.scrollLeft;
|
|
166
|
+
var scrollHeight = this.viewport.scrollHeight;
|
|
167
|
+
var scrollWidth = this.viewport.scrollWidth;
|
|
168
|
+
var clientHeight = this.viewport.clientHeight;
|
|
169
|
+
var clientWidth = this.viewport.clientWidth;
|
|
170
|
+
|
|
171
|
+
// Vertical Axis Calculation
|
|
172
|
+
var maxScrollY = scrollHeight - clientHeight;
|
|
173
|
+
if (maxScrollY > 0 && this.options.vScroll.behavior === 'scroll') {
|
|
174
|
+
this.trackV.style.display = 'block';
|
|
175
|
+
var trackH = this.trackV.clientHeight;
|
|
176
|
+
var thumbH = Math.max((clientHeight / scrollHeight) * trackH, this.minThumbSize);
|
|
177
|
+
this.thumbV.style.height = thumbH + 'px';
|
|
178
|
+
|
|
179
|
+
var ratioY = scrollTop / maxScrollY;
|
|
180
|
+
var thumbY = ratioY * (trackH - thumbH);
|
|
181
|
+
this.thumbV.style.transform = 'translate3d(0, ' + thumbY + 'px, 0)';
|
|
182
|
+
} else {
|
|
183
|
+
this.trackV.style.display = 'none';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Horizontal Axis Calculation
|
|
187
|
+
var maxScrollX = scrollWidth - clientWidth;
|
|
188
|
+
if (maxScrollX > 0 && this.options.hScroll.behavior === 'scroll') {
|
|
189
|
+
this.trackH.style.display = 'block';
|
|
190
|
+
var trackW = this.trackH.clientWidth;
|
|
191
|
+
var thumbW = Math.max((clientWidth / scrollWidth) * trackW, this.minThumbSize);
|
|
192
|
+
this.thumbH.style.width = thumbW + 'px';
|
|
193
|
+
|
|
194
|
+
var ratioX = scrollLeft / maxScrollX;
|
|
195
|
+
var thumbX = ratioX * (trackW - thumbW);
|
|
196
|
+
this.thumbH.style.transform = 'translate3d(' + thumbX + 'px, 0, 0)';
|
|
197
|
+
} else {
|
|
198
|
+
this.trackH.style.display = 'none';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Dispatch Update Event
|
|
202
|
+
this.element.dispatchEvent(new CustomEvent('cocreate-scroll-update', {
|
|
203
|
+
detail: {
|
|
204
|
+
scrollTop: scrollTop,
|
|
205
|
+
scrollLeft: scrollLeft,
|
|
206
|
+
maxScrollY: maxScrollY,
|
|
207
|
+
maxScrollX: maxScrollX,
|
|
208
|
+
clientHeight: clientHeight,
|
|
209
|
+
clientWidth: clientWidth,
|
|
210
|
+
thumbVHeight: this.thumbV ? this.thumbV.clientHeight : 0,
|
|
211
|
+
thumbHWidth: this.thumbH ? this.thumbH.clientWidth : 0
|
|
212
|
+
}
|
|
213
|
+
}));
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
ScrollInstance.prototype.handleScroll = function () {
|
|
217
|
+
var self = this;
|
|
218
|
+
this.update();
|
|
219
|
+
|
|
220
|
+
if (this.options.visible === 'auto') {
|
|
221
|
+
this.element.classList.add('scrolling');
|
|
222
|
+
clearTimeout(this.scrollTimer);
|
|
223
|
+
this.scrollTimer = setTimeout(function () {
|
|
224
|
+
if (!self.isDragging) {
|
|
225
|
+
self.element.classList.remove('scrolling');
|
|
226
|
+
}
|
|
227
|
+
}, this.options.fadeDelay);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
ScrollInstance.prototype.bindEvents = function () {
|
|
232
|
+
var self = this;
|
|
233
|
+
this._onScroll = this.handleScroll.bind(this);
|
|
234
|
+
this.viewport.addEventListener('scroll', this._onScroll, { passive: true });
|
|
235
|
+
|
|
236
|
+
// Thumb Dragging
|
|
237
|
+
this._onThumbVMousedown = function (e) { self.startDrag(e, 'v'); };
|
|
238
|
+
this.thumbV.addEventListener('mousedown', this._onThumbVMousedown);
|
|
239
|
+
this.thumbV.addEventListener('touchstart', this._onThumbVMousedown, { passive: false });
|
|
240
|
+
|
|
241
|
+
this._onThumbHMousedown = function (e) { self.startDrag(e, 'h'); };
|
|
242
|
+
this.thumbH.addEventListener('mousedown', this._onThumbHMousedown);
|
|
243
|
+
this.thumbH.addEventListener('touchstart', this._onThumbHMousedown, { passive: false });
|
|
244
|
+
|
|
245
|
+
// Track Click Jump Logic
|
|
246
|
+
this._onTrackVMousedown = function (e) {
|
|
247
|
+
if (e.target === self.thumbV) return;
|
|
248
|
+
var rect = self.trackV.getBoundingClientRect();
|
|
249
|
+
var clickY = e.clientY - rect.top - (self.thumbV.clientHeight / 2);
|
|
250
|
+
var maxTrackY = self.trackV.clientHeight - self.thumbV.clientHeight;
|
|
251
|
+
var ratio = Math.max(0, Math.min(1, clickY / maxTrackY));
|
|
252
|
+
self.viewport.scrollTo({
|
|
253
|
+
top: ratio * (self.viewport.scrollHeight - self.viewport.clientHeight),
|
|
254
|
+
behavior: 'smooth'
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
this.trackV.addEventListener('mousedown', this._onTrackVMousedown);
|
|
258
|
+
|
|
259
|
+
this._onTrackHMousedown = function (e) {
|
|
260
|
+
if (e.target === self.thumbH) return;
|
|
261
|
+
var rect = self.trackH.getBoundingClientRect();
|
|
262
|
+
var clickX = e.clientX - rect.left - (self.thumbH.clientWidth / 2);
|
|
263
|
+
var maxTrackX = self.trackH.clientWidth - self.thumbH.clientWidth;
|
|
264
|
+
var ratio = Math.max(0, Math.min(1, clickX / maxTrackX));
|
|
265
|
+
self.viewport.scrollTo({
|
|
266
|
+
left: ratio * (self.viewport.scrollWidth - self.viewport.clientWidth),
|
|
267
|
+
behavior: 'smooth'
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
this.trackH.addEventListener('mousedown', this._onTrackHMousedown);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
ScrollInstance.prototype.startDrag = function (e, axis) {
|
|
274
|
+
e.stopPropagation();
|
|
275
|
+
if (e.cancelable) e.preventDefault();
|
|
276
|
+
|
|
277
|
+
var touch = e.touches ? e.touches[0] : e;
|
|
278
|
+
this.isDragging = true;
|
|
279
|
+
this.dragAxis = axis;
|
|
280
|
+
this.startPos = { x: touch.clientX, y: touch.clientY };
|
|
281
|
+
this.startScroll = { x: this.viewport.scrollLeft, y: this.viewport.scrollTop };
|
|
282
|
+
|
|
283
|
+
var thumb = axis === 'v' ? this.thumbV : this.thumbH;
|
|
284
|
+
var track = axis === 'v' ? this.trackV : this.trackH;
|
|
285
|
+
|
|
286
|
+
thumb.classList.add('is-dragging');
|
|
287
|
+
track.classList.add('is-dragging');
|
|
288
|
+
this.element.classList.add('is-dragging');
|
|
289
|
+
document.body.classList.add('scrollbar-dragging');
|
|
290
|
+
|
|
291
|
+
this._onMove = this.onDragMove.bind(this);
|
|
292
|
+
this._onEnd = this.onDragEnd.bind(this);
|
|
293
|
+
|
|
294
|
+
document.addEventListener('mousemove', this._onMove);
|
|
295
|
+
document.addEventListener('mouseup', this._onEnd, { once: true });
|
|
296
|
+
document.addEventListener('touchmove', this._onMove, { passive: false });
|
|
297
|
+
document.addEventListener('touchend', this._onEnd, { once: true });
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
ScrollInstance.prototype.onDragMove = function (e) {
|
|
301
|
+
if (!this.isDragging) return;
|
|
302
|
+
if (e.cancelable) e.preventDefault();
|
|
303
|
+
|
|
304
|
+
var touch = e.touches ? e.touches[0] : e;
|
|
305
|
+
|
|
306
|
+
if (this.dragAxis === 'v') {
|
|
307
|
+
var deltaY = touch.clientY - this.startPos.y;
|
|
308
|
+
var trackH = this.trackV.clientHeight;
|
|
309
|
+
var thumbH = this.thumbV.clientHeight;
|
|
310
|
+
var maxThumbY = trackH - thumbH;
|
|
311
|
+
var maxScrollY = this.viewport.scrollHeight - this.viewport.clientHeight;
|
|
312
|
+
|
|
313
|
+
if (maxThumbY > 0) {
|
|
314
|
+
this.viewport.scrollTop = this.startScroll.y + (deltaY / maxThumbY) * maxScrollY;
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
var deltaX = touch.clientX - this.startPos.x;
|
|
318
|
+
var trackW = this.trackH.clientWidth;
|
|
319
|
+
var thumbW = this.thumbH.clientWidth;
|
|
320
|
+
var maxThumbX = trackW - thumbW;
|
|
321
|
+
var maxScrollX = this.viewport.scrollWidth - this.viewport.clientWidth;
|
|
322
|
+
|
|
323
|
+
if (maxThumbX > 0) {
|
|
324
|
+
this.viewport.scrollLeft = this.startScroll.x + (deltaX / maxThumbX) * maxScrollX;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
ScrollInstance.prototype.onDragEnd = function () {
|
|
330
|
+
var self = this;
|
|
331
|
+
this.isDragging = false;
|
|
332
|
+
this.thumbV.classList.remove('is-dragging');
|
|
333
|
+
this.trackV.classList.remove('is-dragging');
|
|
334
|
+
this.thumbH.classList.remove('is-dragging');
|
|
335
|
+
this.trackH.classList.remove('is-dragging');
|
|
336
|
+
this.element.classList.remove('is-dragging');
|
|
337
|
+
document.body.classList.remove('scrollbar-dragging');
|
|
338
|
+
|
|
339
|
+
document.removeEventListener('mousemove', this._onMove);
|
|
340
|
+
document.removeEventListener('mouseup', this._onEnd);
|
|
341
|
+
document.removeEventListener('touchmove', this._onMove);
|
|
342
|
+
document.removeEventListener('touchend', this._onEnd);
|
|
343
|
+
|
|
344
|
+
if (this.options.visible === 'auto') {
|
|
345
|
+
this.scrollTimer = setTimeout(function () {
|
|
346
|
+
self.element.classList.remove('scrolling');
|
|
347
|
+
}, this.options.fadeDelay);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
ScrollInstance.prototype.destroy = function () {
|
|
352
|
+
clearTimeout(this.scrollTimer);
|
|
353
|
+
if (this.resizeObserver) this.resizeObserver.disconnect();
|
|
354
|
+
if (this.viewport && this._onScroll) {
|
|
355
|
+
this.viewport.removeEventListener('scroll', this._onScroll);
|
|
356
|
+
}
|
|
357
|
+
if (this.thumbV && this._onThumbVMousedown) {
|
|
358
|
+
this.thumbV.removeEventListener('mousedown', this._onThumbVMousedown);
|
|
359
|
+
this.thumbV.removeEventListener('touchstart', this._onThumbVMousedown);
|
|
360
|
+
}
|
|
361
|
+
if (this.thumbH && this._onThumbHMousedown) {
|
|
362
|
+
this.thumbH.removeEventListener('mousedown', this._onThumbHMousedown);
|
|
363
|
+
this.thumbH.removeEventListener('touchstart', this._onThumbHMousedown);
|
|
364
|
+
}
|
|
365
|
+
if (this.trackV && this._onTrackVMousedown) {
|
|
366
|
+
this.trackV.removeEventListener('mousedown', this._onTrackVMousedown);
|
|
367
|
+
}
|
|
368
|
+
if (this.trackH && this._onTrackHMousedown) {
|
|
369
|
+
this.trackH.removeEventListener('mousedown', this._onTrackHMousedown);
|
|
370
|
+
}
|
|
371
|
+
document.removeEventListener('mousemove', this._onMove);
|
|
372
|
+
document.removeEventListener('mouseup', this._onEnd);
|
|
373
|
+
document.removeEventListener('touchmove', this._onMove);
|
|
374
|
+
document.removeEventListener('touchend', this._onEnd);
|
|
375
|
+
document.body.classList.remove('scrollbar-dragging');
|
|
376
|
+
|
|
377
|
+
if (this.createdTrackV) this.trackV.remove();
|
|
378
|
+
else if (this.createdThumbV) this.thumbV.remove();
|
|
379
|
+
if (this.createdTrackH) this.trackH.remove();
|
|
380
|
+
else if (this.createdThumbH) this.thumbH.remove();
|
|
381
|
+
if (this.viewportClassAdded && this.viewport) {
|
|
382
|
+
this.viewport.classList.remove('scrollbar-viewport');
|
|
383
|
+
}
|
|
384
|
+
this.element.classList.remove(
|
|
385
|
+
'visible-show',
|
|
386
|
+
'disable-y',
|
|
387
|
+
'disable-x',
|
|
388
|
+
'scrolling',
|
|
389
|
+
'is-dragging'
|
|
390
|
+
);
|
|
391
|
+
instances.delete(this.element);
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/* Static Manager Interface */
|
|
395
|
+
var CoCreateScrollbar = {
|
|
396
|
+
init: function (target, config) {
|
|
397
|
+
if (!target) return null;
|
|
398
|
+
var elements = typeof target === 'string' ? document.querySelectorAll(target) : (target.length !== undefined ? target : [target]);
|
|
399
|
+
var created = [];
|
|
400
|
+
for (var i = 0; i < elements.length; i++) {
|
|
401
|
+
var el = elements[i];
|
|
402
|
+
var inst = instances.get(el);
|
|
403
|
+
if (!inst) {
|
|
404
|
+
inst = new ScrollInstance(el, config);
|
|
405
|
+
if (!inst.initialized) continue;
|
|
406
|
+
instances.set(el, inst);
|
|
407
|
+
} else {
|
|
408
|
+
if (config) {
|
|
409
|
+
inst.refresh();
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
created.push(inst);
|
|
413
|
+
}
|
|
414
|
+
return created.length === 1 ? created[0] : created;
|
|
415
|
+
},
|
|
416
|
+
|
|
417
|
+
get: function (el) {
|
|
418
|
+
return instances.get(el);
|
|
419
|
+
},
|
|
420
|
+
|
|
421
|
+
destroy: function (el) {
|
|
422
|
+
var inst = instances.get(el);
|
|
423
|
+
if (inst) inst.destroy();
|
|
424
|
+
},
|
|
425
|
+
|
|
426
|
+
initAll: function () {
|
|
427
|
+
if (typeof document === 'undefined') return;
|
|
428
|
+
var targets = document.querySelectorAll(selector);
|
|
429
|
+
for (var i = 0; i < targets.length; i++) {
|
|
430
|
+
this.init(targets[i]);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
return CoCreateScrollbar;
|
|
436
|
+
}());
|
|
437
|
+
|
|
438
|
+
Observer.init({
|
|
439
|
+
name: 'CoCreateOverlayScrollAdded',
|
|
440
|
+
types: ['addedNodes'],
|
|
441
|
+
selector: selector,
|
|
442
|
+
callback: function (mutation) {
|
|
443
|
+
CoCreateScrollbar.init(mutation.target);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
Observer.init({
|
|
448
|
+
name: 'CoCreateOverlayScrollRemoved',
|
|
449
|
+
types: ['removedNodes'],
|
|
450
|
+
selector: selector,
|
|
451
|
+
callback: function (mutation) {
|
|
452
|
+
CoCreateScrollbar.destroy(mutation.target);
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
Observer.init({
|
|
457
|
+
name: 'CoCreateOverlayScrollAttributes',
|
|
458
|
+
types: ['attributes'],
|
|
459
|
+
attributeFilter: ['scrollbar'].concat(optionAttributes),
|
|
460
|
+
callback: function (mutation) {
|
|
461
|
+
var element = mutation.target;
|
|
462
|
+
if (mutation.attributeName === 'scrollbar') {
|
|
463
|
+
if (element.hasAttribute('scrollbar')) CoCreateScrollbar.init(element);
|
|
464
|
+
else CoCreateScrollbar.destroy(element);
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
var instance = CoCreateScrollbar.get(element);
|
|
469
|
+
if (instance) instance.refresh();
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
CoCreateScrollbar.initAll();
|
|
474
|
+
|
|
475
|
+
export default CoCreateScrollbar;
|