Comments (9)
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
from scroll-js.
I've hacked together some changes for scrollTo
to accept x and y scrolling. This just works for the scrollTo function, it can be translated to the other methods but would require a bit more consideration to update all methods etc.
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;
}
}
from scroll-js.
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 behavior: smooth
for now but I'll revisit this if we cannot get it to work reliably.
We're already using this library for some other scrolling and it's been really great this far 😃
Thanks!
from scroll-js.
@mkay581 isn't the first param the X scroll?
from scroll-js.
The first param to Scrolls the API has since been changed. See updated README..to()
method is x
, which is the horizontal scroll on the x axis, yes. But its not currently being used. I made the decision to include it so that future horizontal support will be easy.
from scroll-js.
Is horizontal scroll still not supported?
from scroll-js.
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.
from scroll-js.
Awesome @omonk! You mind submitting a PR? So I can better evaluate, run tests, etc?
from scroll-js.
Would also be excited to see this added ☝🏼 @markcellus @omonk
from scroll-js.
Related Issues (20)
- Errors with brunch HOT 6
- Container.contains is not a function HOT 2
- Duration: 0? HOT 8
- Offset option HOT 17
- Massive NPM package HOT 3
- Stop scrolling immediately. HOT 4
- Scroll start always from the top of the page rather than from the current position HOT 5
- Scrolling always starts at the top for Chrome and Firefox HOT 4
- other easing options HOT 3
- Support more ScrollIntoViewOptions per specification
- Remove support for instant behavior
- Holp to support `top` into ScrollIntoViewOptions HOT 1
- Behavior of `behavior` HOT 2
- Why does this package has a minimal node engine version HOT 6
- IE11 Support HOT 3
- On nextjs it runs in development mode. HOT 2
- Import error "cannot find module" HOT 5
- Feature Request: `cancel` method
- Feature Request: `scrollBy` method
- scrollIntoView not working in IE HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scroll-js.