-
-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for wheel event scrolling #47
Comments
Hi Kenneth (@sphair), Thank you for your feature request. That’s correct, the two-finger gesture on a touchpad triggers the JavaScript wheel event. Additionally, a mouse with a wheel also triggers the wheel event. Do you mind if I change the title of this issue to:
To cover all devices with wheels? Kindly, |
I need this as well 👍 |
Any idea when you will be able to start on this? I need this before I can use embla in production. |
Hi @horgen, thank you for your question. Short answerUnfortunately, no. Long answerI have a huge backlog including everything from a lot of feature requests to writing documentation, troubleshooting (see issue #45 and #12) and setting up CodeSandbox demonstrations etc. Additionally, I've lately had a lot of This product is an open source product and I'm hoping for the community to help me out with stuff like this so I can spend more time on new features and code quality. But unfortunately, contributions are really rare, so things are progressing slow. With that said, I'm doing my best and I'm hoping to be able to investigate this soon, but can't give you an estimate on when this will happen. I hope you understand. Best, |
Here's my solution. I'm using a throttling function to adjust for the sensitivity of trackpads. Otherwise the carousel will just fly past at the slightest touch. function throttled(delay, fn) {
let lastCall = 0;
return function(...args) {
const now = new Date().getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return fn(...args);
};
}
window.addEventListener(
"wheel",
throttled(350, e => {
if (e.deltaY > 0) this.embla.scrollNext();
else this.embla.scrollPrev();
})
); |
Hey David (@davidcetinkaya), maybe we continue our discussion here. Working with the engine worked great! I think it feels pretty nice, I tested with a Macbook Pro and a Surface Laptop (in all modern browsers). Hope it works fine on your device too ;) CodeSandbox can't install from git repositories yet, so I put the code here. Just the out-of-bounds/bouncing off the edges (when loop: false) does not look quite right yet. Maybe you know a better way to set the values? So it does not start to animate back on its own? Also I think to have a fully reliable add-on a reInit/updated event would be great too, in case the containerNode (or other things change). Also a way to read the options from the engine would be helpful, in this case the dragFree option would be of interest. Cheers! |
Hi Felix (@xiel), Nicely done! Looking good 🙂. I think it makes a lot of sense to expose the options with the engine method The reInit method is actually available in the latest pushed commit (not release!). It will be released with Embla v.3 (as mentioned earlier I'm working on v.3 right now). embla.changeOptions(options)
// has been renamed to
embla.reInit(options) ...where the options object is optional. The out-of-bounds/bouncing you mention is done automatically if the animation loop is going in the const engine = embla.dangerouslyGetEngine()
const reachedAnyBound = engine.limit.reachedAny(engine.location.get())
if (reachedAnyBound) {
// Don't trigger more scroll
} Now I haven't tested this with your implementation so forgive me if it's a dead end 😉. Let me know if it helps. Best, |
Hello again Felix (@xiel), The options object is now exposed when you grab the engine. It comes with this commit. Cheers, |
@davidcetinkaya Awesome! Do you think you can also trigger a "reInit" or "activate" in when it is not Because I would also need to re-init the plugin, when the user updates the option for axis for example, or when the containerNode changed etc.
And I'm still having a problem with targets out of bounds. But when I update the target via Here is a simple demo showing it, by just continously moving right: It there a way to disable the bounds temporarily, which I did not find yet? :) const emblaEngine = embla.dangerouslyGetEngine();
embla.on("init", () => {
emblaEngine.scrollBody.useDefaultMass().useSpeed(80);
moveRight();
});
function moveRight() {
const reachedAnyBound = emblaEngine.limit.reachedAny(
emblaEngine.location.get()
);
emblaEngine.animation.start();
emblaEngine.target.add(-10 / (reachedAnyBound ? 2 : 1));
requestAnimationFrame(moveRight);
} |
Hello Felix (@xiel), Of course, exposing the: embla.on("reInit", callback) ...event makes sense. Now I understand the out of bounds issue you’re experiencing. Hmm. This one is a bit tricky. One way to do it would be to not use any out-of-bounds-effect. So basically never let the target exceed any of the bounds when scrolling the wheel. Because otherwise I’m thinking that it can get quite messy exposing a lot of stuff unless the wheel behavior is actually built into the Embla core 😕. What do you think? Kindly |
@davidcetinkaya Yea, that's fine! Definitely good enough for a first version 💯 no out-of-bounds: https://embla-carousel-wheel-gestures-hio0j134f.now.sh I think it feels better with the shaky version, than no out-of-bounds at all, because it feels too different to native behaviour on Mac imho. Also when the carousel is directly at one of the bounds, it still returns false, which makes it behave weirdly sometimes when I disallow out-of-bounds. Looking forward to the reinit event ;) Thanks for working on this! |
@xiel, I'll see if it's possible to add the disable bounds method soon. About the |
yes, that sounds reasonable, no matter what the reason is embla reInits, it would be good to know ;) |
Allright @xiel 🙂, If you grab the latest commit you have the I'll let you know how it goes with disabling the Cheers! |
Hi Felix (@xiel), The latest commit comes with the possibility to toggle the scrollBounds active/inactive 🙂: emblaEngine.scrollBounds.toggleActive(false) // disable
// ...or
emblaEngine.scrollBounds.toggleActive(true) // enable Note that they're enabled by default for non loop carousels. Let me know if it works as intended. Best, |
@davidcetinkaya Awesome, I will have a look this weekend. Thanks! |
Hey David (@davidcetinkaya), reinit and bounds de/activating work perfectly and create a really great UX now! 🥳 Here is the updated version: https://embla-carousel-wheel-gestures-jsulrk1a3.now.sh (Code) Thanks for the additional API, looking forward to the release of 3.0! |
Hey Felix (@xiel), nice work 🎉 ! Looking good to me! I'm glad the additions helped you achieve this 🙂. Version 3 is just around the corner now. I'm very excited about the release, even though it means a lot of work rewriting the documentation, and not to mention merging all option demonstration CodeSandboxes into one, where you're going to be able to toggle all the different options. So are you wrapping up the wheel package or what's your plan from here 🙂? |
@davidcetinkaya Haha, yea writing all the documentation and demos is often the even harder work. 😅 Yes, my plan would be to wrap this into a small npm package during the coming days. I will also ensure this works great with the react version of embla. I will add you to the repository then, if that is ok? In case you need to make breaking changes in the future or so :) But no pressure, I will try to keep it up-to-date. To set things up more easily, it would help me, if embla 3.0 was pre-released with a next/beta dist tag on npm, because some tools don't allow to install from git and require local linking. Would you consider doing that? (Might also help you, to setup and test new CodeSandboxes). I'll keep you posted, I hope to be ready soon after 3.0 is ready! :) |
Hello again Felix (@xiel), Next up is the release of v.3 so I think doing a pre-release will rather slow things down at this point. There's no turning back 😂 . Of course, add me to the repo 🙂. I'll let you know when v.3 is out! Kindly, |
alrighty, i’ll just wait for the release 👍 |
Here we go, the wheel events plugin is out as well 🥳 yarn add embla-carousel-wheel-gestures # npm install --save embla-carousel-wheel-gestures import EmblaCarousel from 'embla-carousel'
import { setupWheelGestures } from 'embla-carousel-wheel-gestures'
// initialize Embla Carousel
const embla = EmblaCarousel(emblaNode, options)
// add support for wheen events
setupWheelGestures(embla) Thanks for providing the nessessary API, @davidcetinkaya ! |
Hello Felix (@xiel), Wow, awesome work 🎉 🥳! @sphair, @remisture and @horgen, this solves your feature request right? Best, |
@xiel I'm going to add this to the readme just to let users know it's there 🙂. Do you have any suggestions where it should be added? If you want to add the Embla logo to the documentation page, that's totally ok. But don't feel obligated to. Only add it if you think it makes sense. (If you have a better suggestion for a better looking logo, please share it 😜. This is kind of a joke, but also not 😂.) |
@davidcetinkaya Hehe, good question. Maybe below the CodeSandboxes as a new section "Plugins" or "Add-ons"? Cool, I thought about adding the Logo, but wanted you permission first 👍 |
@xiel this is totally unrelated but take a look at this CodeSandbox. Was just fooling around a bit 🙂. |
@davidcetinkaya wow really cool! perfect demo for the y axis! |
Closing this as resolved. Thank you Felix (@xiel) for your efforts 👍. |
Hello Felix (@xiel), In order to get rid of maintaining two separate repos I've merged |
When using touchpad on an MacBook, you can use two-finger gestures for scrolling. As far as I understand, these values are available through "wheel" events in javascript.
It would be very nice to have support for horizontal scrolling using touchpad in Embla Carousel if possible.
Here is a fiddle that demonstrates the values received: https://jsfiddle.net/64p5r459/2/
The text was updated successfully, but these errors were encountered: