Skip to content

Commit 0c42fc1

Browse files
committed
feat: scroll-to to set a scroll position top | bottom | left | right
1 parent 73eeb1d commit 0c42fc1

File tree

1 file changed

+58
-22
lines changed

1 file changed

+58
-22
lines changed

src/index.js

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import observer from '@cocreate/observer';
1+
import Observer from '@cocreate/observer';
2+
import Actions from '@cocreate/actions';
23

34
const CoCreateScroll = {
45
delta: 3,
@@ -7,7 +8,7 @@ const CoCreateScroll = {
78
firedEvents: new Map(),
89

910
init: function () {
10-
let elements = document.querySelectorAll(`[scroll]`);
11+
let elements = document.querySelectorAll(`[scroll], [scroll-to]`);
1112
this.__initIntersectionObserver();
1213
this.initElements(elements);
1314
},
@@ -25,8 +26,9 @@ const CoCreateScroll = {
2526
const targetSelector = element.getAttribute('scroll-selector');
2627
const scrollSelector = element.getAttribute('scroll-element');
2728
const intersectValue = element.getAttribute('scroll-intersect');
29+
const scrollTo = element.getAttribute('scroll-to');
2830

29-
let values = element.getAttribute('scroll') || "";
31+
let values = element.getAttribute('scroll') || element.getAttribute('scroll-value');
3032
values = values.split(",").map(x => x.trim());
3133

3234
let scrollInfo = {
@@ -37,7 +39,8 @@ const CoCreateScroll = {
3739
scrollTop: element.getAttribute('scroll-top'),
3840
scrollLimbo: element.getAttribute('scroll-limbo'),
3941
scrollBottom: element.getAttribute('scroll-bottom'),
40-
scrolling: element.getAttribute('scrolling')
42+
scrolling: element.getAttribute('scrolling'),
43+
scrollTo
4144
};
4245

4346
let elements = [element];
@@ -54,6 +57,8 @@ const CoCreateScroll = {
5457
let scrollableElements;
5558
if (scrollSelector)
5659
scrollableElements = document.querySelectorAll(scrollSelector);
60+
else if (element.hasAttribute('scroll-element'))
61+
scrollableElements = [element]
5762

5863
if (scrollableElements) {
5964
for (let scrollableEl of scrollableElements) {
@@ -71,6 +76,10 @@ const CoCreateScroll = {
7176
if (intersectValue && window.IntersectionObserver && this.observer) {
7277
this.observer.observe(element);
7378
}
79+
80+
if (scrollTo) {
81+
this.setScrollPosition(element, scrollTo)
82+
}
7483
},
7584

7685
_scrollEvent: function (elements, element, scrollInfo, scrollableEl) {
@@ -99,6 +108,20 @@ const CoCreateScroll = {
99108
}, 500);
100109
},
101110

111+
setScrollPosition: function (element, scrollTo) {
112+
if (scrollTo.includes('top')) {
113+
element.scrollTop = 0;
114+
} else if (scrollTo.includes('bottom')) {
115+
element.scrollTop = element.scrollHeight;
116+
}
117+
118+
if (scrollTo.includes('left')) {
119+
element.scrollLeft = 0;
120+
} else if (scrollTo.includes('right')) {
121+
element.scrollLeft = element.scrollWidth;
122+
}
123+
},
124+
102125
__initIntersectionObserver: function () {
103126
const self = this;
104127
this.observer = new IntersectionObserver(entries => {
@@ -114,8 +137,7 @@ const CoCreateScroll = {
114137
}
115138
if (entry.isIntersecting > 0) {
116139
targetElements.forEach((el) => self.__addAttributeValue(el, attrName, intersectValue));
117-
}
118-
else {
140+
} else {
119141
targetElements.forEach((el) => self.__removeAttrbuteValue(el, attrName, intersectValue));
120142
}
121143
});
@@ -126,8 +148,7 @@ const CoCreateScroll = {
126148
const { scrolling, attrName } = info;
127149
if (stopped) {
128150
this.__removeAttrbuteValue(element, attrName, scrolling);
129-
}
130-
else {
151+
} else {
131152
this.__addAttributeValue(element, attrName, scrolling);
132153
}
133154
},
@@ -154,20 +175,19 @@ const CoCreateScroll = {
154175
if (upSize <= (currentPos - scrollY)) {
155176
this.__addAttributeValue(element, attrName, values[0]);
156177
this.__removeAttrbuteValue(element, attrName, values[1]);
157-
}
158-
else if (downSize <= (scrollY - currentPos)) {
178+
} else if (downSize <= (scrollY - currentPos)) {
159179
this.__removeAttrbuteValue(element, attrName, values[0]);
160180
this.__addAttributeValue(element, attrName, values[1]);
161181
}
162182
}
183+
163184
//. scroll top case
164185
if (scrollY <= this.delta) {
165186
this.__removeAttrbuteValue(element, attrName, values[0]);
166187
this.__removeAttrbuteValue(element, attrName, values[1]);
167188

168189
this.__addAttributeValue(element, attrName, scrollTop);
169-
}
170-
else {
190+
} else {
171191
this.__removeAttrbuteValue(element, attrName, scrollTop);
172192
}
173193

@@ -178,16 +198,14 @@ const CoCreateScroll = {
178198
// this.__removeAttrbuteValue(element, attrName, values[1]);
179199

180200
this.__addAttributeValue(element, attrName, scrollBottom);
181-
}
182-
else {
201+
} else {
183202
this.__removeAttrbuteValue(element, attrName, scrollBottom);
184203
}
185204

186205
// if (scrollY != 0 && (scrollY + window.innerHeight) != document.body.scrollHeight){
187206
if (scrollY != 0 && (scrollY + innerHeight) != scrollHeight) {
188207
this.__addAttributeValue(element, attrName, scrollLimbo);
189-
}
190-
else {
208+
} else {
191209
this.__removeAttrbuteValue(element, attrName, scrollLimbo);
192210
}
193211

@@ -200,7 +218,10 @@ const CoCreateScroll = {
200218
let attrValue = element.getAttribute(attrName) || "";
201219

202220
if (!check.test(attrValue)) {
203-
attrValue += " " + value;
221+
if (attrName === 'class')
222+
attrValue += " " + value;
223+
else
224+
attrValue = value
204225
element.setAttribute(attrName, attrValue);
205226
}
206227
},
@@ -228,8 +249,7 @@ const CoCreateScroll = {
228249

229250
size = isWidth ? window.innerWidth / size : window.innerHeight / size;
230251

231-
}
232-
else {
252+
} else {
233253
size = attrValue.replace('px', '').trim();
234254
size = Number(size) || 0;
235255
}
@@ -239,16 +259,32 @@ const CoCreateScroll = {
239259

240260
};
241261

242-
observer.init({
262+
Observer.init({
243263
name: 'CoCreateScrollCreate',
244264
observe: ['addedNodes'],
245-
target: '[scroll]',
265+
target: '[scroll], [scroll-to]',
246266
callback: function (mutation) {
247-
248267
CoCreateScroll.initElement(mutation.target);
249268
}
250269
});
251270

271+
Observer.init({
272+
name: 'CoCreateScrollAttributes',
273+
observe: ['attributes'],
274+
attributeName: ['scroll-to'],
275+
// target: selector, // blocks mutations when applied
276+
callback: function (mutation) {
277+
CoCreateScroll.setScrollPosition(mutation.target, mutation.target.getAttribute('scroll-to'))
278+
}
279+
});
280+
281+
Actions.init({
282+
name: "scroll-to",
283+
callback: (action) => {
284+
// CoCreateScroll.setScrollPosition(mutation.target)
285+
}
286+
});
287+
252288
CoCreateScroll.init();
253289

254290
export default CoCreateScroll;

0 commit comments

Comments
 (0)