@pushword/js-helper 0.0.96 → 0.0.97
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/package.json +1 -1
- package/src/ScrollEnhancer.js +194 -0
- package/src/MouseSlider.js +0 -26
package/package.json
CHANGED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Demo in Draft
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
class ScrollYEnhancer {
|
|
6
|
+
constructor(
|
|
7
|
+
selector = '.enhance-scroll-y',
|
|
8
|
+
chevron = '<div class="scroller absolute left-[128px] z-10 -mt-[10px] h-[44px] w-[44px] cursor-pointer rounded-full border border-gray-200 bg-white text-center text-3xl leading-none text-gray-600 hover:bg-gray-100 select-none" onclick="scrollPreviousDiv(this)">⌄</div><div class="relative z-0 -mt-8 h-8 w-full bg-gradient-to-t from-white to-transparent"></div>',
|
|
9
|
+
insertAfterBegin = '<div class="fixed left-0 z-0 -mt-3 h-3 w-full bg-gradient-to-b from-white to-transparent"></div>'
|
|
10
|
+
) {
|
|
11
|
+
this.chevron = chevron;
|
|
12
|
+
this.insertAfterBegin = insertAfterBegin;
|
|
13
|
+
window.scrollPreviousDiv = this.scrollPreviousDiv;
|
|
14
|
+
window.manageScrollYControllerVisibility = this.manageScrollYControllerVisibility;
|
|
15
|
+
|
|
16
|
+
document.querySelectorAll(selector).forEach((element) => {
|
|
17
|
+
this.enhanceScrollY(element)
|
|
18
|
+
this.mouseSliderY(element)
|
|
19
|
+
this.wheelScroll(element)
|
|
20
|
+
element.onscroll = function () {
|
|
21
|
+
manageScrollYControllerVisibility(this)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
wheelScroll(element) {
|
|
27
|
+
element.addEventListener('wheel', (evt) => {
|
|
28
|
+
evt.preventDefault()
|
|
29
|
+
element.classList.toggle('scroll-smooth')
|
|
30
|
+
element.scrollTop += evt.deltaY
|
|
31
|
+
element.classList.toggle('scroll-smooth')
|
|
32
|
+
})
|
|
33
|
+
return this
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
enhanceScrollY(element) {
|
|
37
|
+
if (element.scrollHeight <= element.clientHeight) return;
|
|
38
|
+
element.insertAdjacentHTML('afterBegin', this.insertAfterBegin);
|
|
39
|
+
element.insertAdjacentHTML('afterEnd', this.chevron);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
scrollPreviousDiv(element) {
|
|
43
|
+
const previousDiv = element.previousElementSibling;
|
|
44
|
+
if (!previousDiv) return;
|
|
45
|
+
if (element.textContent === '⌄') {
|
|
46
|
+
previousDiv.scrollTop += 25; // ~ one line
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
previousDiv.scrollTop = 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
manageScrollYControllerVisibility(element) {
|
|
53
|
+
const scroller = element.parentNode.querySelector('.scroller');
|
|
54
|
+
if (scroller.textContent === '⌄') {
|
|
55
|
+
const isAtMaxScroll = element.scrollTop >= element.scrollHeight - element.clientHeight - 10;
|
|
56
|
+
if (isAtMaxScroll) {
|
|
57
|
+
scroller.textContent = '⌃';
|
|
58
|
+
scroller.classList.add('pt-[11px]');
|
|
59
|
+
scroller.classList.add('text-gray-200');
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
} else {
|
|
63
|
+
scroller.textContent = '⌄';
|
|
64
|
+
scroller.classList.remove('pt-[11px]');
|
|
65
|
+
scroller.classList.remove('text-gray-200');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
mouseSliderY(toSlide, speed = 1) {
|
|
70
|
+
if ('ontouchstart' in document.documentElement) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
toSlide.classList.add('overflow-y-hidden');
|
|
74
|
+
let isDown = false;
|
|
75
|
+
let startX;
|
|
76
|
+
let scrollTop;
|
|
77
|
+
toSlide.addEventListener('mousedown', (e) => {
|
|
78
|
+
isDown = true;
|
|
79
|
+
//toSlide.classList.add('active');
|
|
80
|
+
startX = e.pageY - toSlide.offsetTop;
|
|
81
|
+
scrollTop = toSlide.scrollTop;
|
|
82
|
+
});
|
|
83
|
+
toSlide.addEventListener('mouseleave', () => {
|
|
84
|
+
isDown = false;
|
|
85
|
+
//toSlide.classList.remove('active');
|
|
86
|
+
});
|
|
87
|
+
toSlide.addEventListener('mouseup', () => {
|
|
88
|
+
isDown = false;
|
|
89
|
+
//toSlide.classList.remove('active');
|
|
90
|
+
});
|
|
91
|
+
toSlide.addEventListener('mousemove', (e) => {
|
|
92
|
+
if (!isDown) return;
|
|
93
|
+
e.preventDefault();
|
|
94
|
+
const x = e.pageY - toSlide.offsetTop;
|
|
95
|
+
const walk = (x - startX) * speed;
|
|
96
|
+
toSlide.scrollTop = scrollTop - walk;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
class ScrollXEnhancer {
|
|
102
|
+
constructor(
|
|
103
|
+
selector = '.enhance-scroll-x',
|
|
104
|
+
chevronRight = '<div class="scroll-right fixed right-0 top-1/3 z-20 h-[44px] w-[44px] cursor-pointer select-none rounded-full border border-gray-200 bg-white pt-[6px] text-center text-3xl leading-none text-gray-600 hover:bg-gray-100" onclick="scrollX(this)">🠆</div>',
|
|
105
|
+
chevronLeft = '<div class="scroll-left fixed left-[22px] top-1/3 z-20 h-[44px] w-[44px] cursor-pointer select-none rounded-full border border-gray-200 bg-white pt-[6px] text-center text-3xl leading-none text-gray-600 hover:bg-gray-100" onclick="scrollX(this)">🠄</div>'
|
|
106
|
+
) {
|
|
107
|
+
this.chevronLeft = chevronLeft;
|
|
108
|
+
this.chevronRight = chevronRight;
|
|
109
|
+
window.scrollLeft = this.scrollLeft;
|
|
110
|
+
window.scrollX = this.scrollX;
|
|
111
|
+
window.manageScrollXControllerVisibility = this.manageScrollXControllerVisibility;
|
|
112
|
+
|
|
113
|
+
document.querySelectorAll(selector).forEach((element) => {
|
|
114
|
+
this.enhanceScrollX(element)
|
|
115
|
+
this.mouseSliderX(element)
|
|
116
|
+
this.wheelScroll(element)
|
|
117
|
+
element.onscroll = function () {
|
|
118
|
+
manageScrollXControllerVisibility(this)
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
wheelScroll(element) {
|
|
124
|
+
element.addEventListener('wheel', (evt) => {
|
|
125
|
+
evt.preventDefault()
|
|
126
|
+
if (evt.target.closest('.enhance-scroll-y')) return
|
|
127
|
+
if (window.isScrolling === true) return
|
|
128
|
+
element.classList.toggle('scroll-smooth')
|
|
129
|
+
element.scrollLeft += evt.deltaY
|
|
130
|
+
element.classList.toggle('scroll-smooth')
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
enhanceScrollX(element) {
|
|
135
|
+
if (element.scrollWidth <= element.clientWidth) return;
|
|
136
|
+
element.insertAdjacentHTML('afterbegin', this.chevronLeft + this.chevronRight);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
scrollX(scroller) {
|
|
140
|
+
const element = scroller.parentNode;
|
|
141
|
+
if (!element) return;
|
|
142
|
+
|
|
143
|
+
const scrollToRight = scroller.classList.contains('scroll-right');
|
|
144
|
+
|
|
145
|
+
const oppositeSelector = scrollToRight ? 'scroll-left' : 'scroll-right';
|
|
146
|
+
const oppositeController = element.querySelector('.' + oppositeSelector);
|
|
147
|
+
|
|
148
|
+
const nextElementToScroll = element.children[3]; // work only with equal width block
|
|
149
|
+
const toScrollWidth =
|
|
150
|
+
nextElementToScroll.offsetWidth +
|
|
151
|
+
parseInt(window.getComputedStyle(nextElementToScroll).marginLeft);
|
|
152
|
+
element.scrollLeft += scrollToRight ? toScrollWidth : -toScrollWidth;
|
|
153
|
+
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
manageScrollXControllerVisibility(element) {
|
|
157
|
+
const scrollLeftElement = element.querySelector('.scroll-left');
|
|
158
|
+
const scrollRightElement = element.querySelector('.scroll-right');
|
|
159
|
+
scrollRightElement.classList.remove('opacity-30');
|
|
160
|
+
scrollLeftElement.classList.remove('opacity-30');
|
|
161
|
+
|
|
162
|
+
const isAtMaxScroll = element.scrollLeft >= element.scrollWidth - element.clientWidth;
|
|
163
|
+
if (isAtMaxScroll) scrollRightElement.classList.add('opacity-30');
|
|
164
|
+
if (element.scrollLeft === 0) scrollLeftElement.classList.add('opacity-30');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
mouseSliderX(toSlide, speed = 1) {
|
|
168
|
+
if ('ontouchstart' in document.documentElement) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
toSlide.classList.add('overflow-x-hidden');
|
|
172
|
+
let isDown = false;
|
|
173
|
+
let startX;
|
|
174
|
+
let scrollLeft;
|
|
175
|
+
toSlide.addEventListener('mousedown', (e) => {
|
|
176
|
+
isDown = true;
|
|
177
|
+
startX = e.pageX - toSlide.offsetLeft;
|
|
178
|
+
scrollLeft = toSlide.scrollLeft;
|
|
179
|
+
});
|
|
180
|
+
toSlide.addEventListener('mouseleave', () => {
|
|
181
|
+
isDown = false;
|
|
182
|
+
});
|
|
183
|
+
toSlide.addEventListener('mouseup', () => {
|
|
184
|
+
isDown = false;
|
|
185
|
+
});
|
|
186
|
+
toSlide.addEventListener('mousemove', (e) => {
|
|
187
|
+
if (!isDown) return;
|
|
188
|
+
e.preventDefault();
|
|
189
|
+
const x = e.pageX - toSlide.offsetLeft;
|
|
190
|
+
const walk = (x - startX) * speed;
|
|
191
|
+
toSlide.scrollLeft = scrollLeft - walk;
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
package/src/MouseSlider.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export function mouseSlider(toSlide, speed = 1) {
|
|
2
|
-
let isDown = false;
|
|
3
|
-
let startX;
|
|
4
|
-
let scrollLeft;
|
|
5
|
-
toSlide.addEventListener('mousedown', (e) => {
|
|
6
|
-
isDown = true;
|
|
7
|
-
toSlide.classList.add('active');
|
|
8
|
-
startX = e.pageX - toSlide.offsetLeft;
|
|
9
|
-
scrollLeft = toSlide.scrollLeft;
|
|
10
|
-
});
|
|
11
|
-
toSlide.addEventListener('mouseleave', () => {
|
|
12
|
-
isDown = false;
|
|
13
|
-
toSlide.classList.remove('active');
|
|
14
|
-
});
|
|
15
|
-
toSlide.addEventListener('mouseup', () => {
|
|
16
|
-
isDown = false;
|
|
17
|
-
toSlide.classList.remove('active');
|
|
18
|
-
});
|
|
19
|
-
toSlide.addEventListener('mousemove', (e) => {
|
|
20
|
-
if (!isDown) return;
|
|
21
|
-
e.preventDefault();
|
|
22
|
-
const x = e.pageX - toSlide.offsetLeft;
|
|
23
|
-
const walk = (x - startX) * speed;
|
|
24
|
-
toSlide.scrollLeft = scrollLeft - walk;
|
|
25
|
-
});
|
|
26
|
-
}
|