-
Notifications
You must be signed in to change notification settings - Fork 33
Add horizontal scroll support #1
Comments
@mkay581 isn't the first param the X scroll? |
|
Is horizontal scroll still not supported? |
No it isn't unfortunately. 😞 Just haven't had time to get to it. But do a lot of people need this? Horizontal scrolling seems rare when compared to vertical scrolling and haven't really seen this ticket gain as much popularity. I am open to accepting PRs though if you think its something you can tackle. |
No worries! Needed it for a Netflix type navigation with a lot of titles being shown horizontally. Trying to get it working with the the browser native scrolling We're already using this library for some other scrolling and it's been really great this far 😃 |
I've hacked together some changes for async function scrollTo(el = window, options = {}) {
const scroll = ({
from,
to,
startTime,
duration = 400,
easeFunc,
resolve,
}) => {
window.requestAnimationFrame(() => {
const currentTime = Date.now();
const time = Math.min(1, (currentTime - startTime) / duration);
if (from.top === to.top && from.left === to.left) {
return resolve ? resolve() : null;
}
setScrollPosition(el, {
top: easeFunc(time) * (to.top - from.top) + from.top,
left: easeFunc(time) * (to.left - from.left) + from.left,
});
/* prevent scrolling, if already there, or at end */
if (time < 1) {
scroll({ from, to, startTime, duration, easeFunc, resolve });
} else if (resolve) {
resolve();
}
});
};
const currentScrollPosition = getScrollPosition(el);
return new Promise(resolve => {
scroll({
from: currentScrollPosition,
to: { top: options.top, left: options.left },
startTime: Date.now(),
duration: options.duration,
easeFunc: t => {
return t * (2 - t);
},
resolve,
});
});
}
function getScrollPosition(el) {
if (
el === document.body ||
el === document.documentElement ||
el instanceof Window
) {
return {
top: document.body.scrollTop || document.documentElement.scrollTop,
left: document.body.scrollLeft || document.documentElement.scrollLeft,
};
} else {
return {
top: el.scrollTop || el.scrollTop,
left: el.scrollLeft || el.scrollLeft,
};
}
}
function setScrollPosition(el, { top, left }) {
if (
el === document.body ||
el === document.documentElement ||
el instanceof Window
) {
document.body.scrollTop = top;
document.documentElement.scrollTop = top;
document.body.scrollLeft = left;
document.documentElement.scrollLeft = left;
} else {
el.scrollTop = top;
el.scrollLeft = left;
}
} |
Awesome @omonk! You mind submitting a PR? So I can better evaluate, run tests, etc? |
I can have a look at some point! I hacked this together quickly for a small personal project I'm working on but would be happy to contribute for others to use also |
Would also be excited to see this added ☝🏼 @markcellus @omonk |
Vertical scroll is nice but what about horizontal scroll?
The text was updated successfully, but these errors were encountered: