Skip to content
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

Is there an alternative to scrollToProgress()? #178

Closed
EntropyReversed opened this issue Apr 13, 2021 · 11 comments
Closed

Is there an alternative to scrollToProgress()? #178

EntropyReversed opened this issue Apr 13, 2021 · 11 comments
Labels
question Question about how to achieve something resolved This issue is resolved

Comments

@EntropyReversed
Copy link

Currently working on project that has a slider that should be moved with input. I can get progress with scrollProgress() and if I plug it into input value input is updated on Embla scroll, how can I invert it so that Embla is updated with input? scrollToProgress() and scrollBy() were good for this use case but they were deprecated in v3.0.0. Are there any alternatives?

@EntropyReversed EntropyReversed added the feature request New feature or request label Apr 13, 2021
@davidjerleke
Copy link
Owner

davidjerleke commented Apr 14, 2021

Hi @EntropyReversed,

Thank you for your question. It seems like you removed the following part when you created your feature request:

options

It's important to not remove this, because I don't know if I should demonstrate how to achieve what you want with VanillaJs or ReactJs. I'm going to show you how to achieve what you want with VanillaJs, and if you're using ReactJs you'll have to refactor my code into the "React way" of doing it.

Take a look at this CodeSandbox.

Let me know if it helps.

Best,
David

@davidjerleke davidjerleke added the awaiting response Issue is awaiting feedback label Apr 14, 2021
@EntropyReversed
Copy link
Author

Hi @davidcetinkaya!
It is vanilla js-related and this is exactly what I've been looking for!
Thank you very much

@davidjerleke
Copy link
Owner

Glad to hear that @EntropyReversed. Try it out and let me know if it's working as intended.

Best,
David

@EntropyReversed
Copy link
Author

Have one question. What is n in withinBounds()?

@davidjerleke
Copy link
Owner

@EntropyReversed, it should be return distance, not return n. Sorry about that. I’ve updated the code snippet.

@EntropyReversed
Copy link
Author

EntropyReversed commented Apr 14, 2021

When I call scrollToProgress(-0.5) on the load it scrolls to the middle (as it should), but when I call it from inputs eventListener and I pass its values (that range from 0 to -1) it doesn't work. It goes from start to end by the time input is about 20% scrolled

input.addEventListener('input', () => {
   let inputValue = input.value / 100;
   scrollToProgress(-inputValue);
   console.log(-inputValue) // this returns numbers from 0 to -1;
})

@davidjerleke
Copy link
Owner

@EntropyReversed I’m not sure why you’re passing a negative number to scrollToProgress? It was some time ago I created these methods but if I’m not mistaken you should pass a positive number from 0 to 1.

This is because scrollToProgress takes an absolute scroll destination. Passing 0.2 will always scroll to the position where the carousel has scrolled 20% of its scrollable length. So a negative value should not be passed to it.

On a side note
I added syntax highlighting to your code snippet. I hope that's ok.

@EntropyReversed
Copy link
Author

@davidcetinkaya I tried to pass only values from 0 to 1 to scrollToProgress but it doesn't work here is a codesandbox link. Maybe I'm doing something wrong?

@davidjerleke
Copy link
Owner

davidjerleke commented Apr 19, 2021

Hi again @EntropyReversed,

I must have rushed it when copy pasting the code snippet. I've created a working CodeSandbox for you now. I took the liberty to sync the input when the user scrolls the carousel directly by dragging or clicking the previous and next buttons too. If you don't need it you can just remove that code.

Sorry for the inconvenience and let me know if it's working as intended now.

Best,
David

@EntropyReversed
Copy link
Author

Hey @davidcetinkaya, it works. And it is more than I asked for but it's everything that I need (i didn't want to bother you with syncing the input when the user scrolls the carousel, was planning to do it myself). So thank you very much and I really appreciate what you did! Btw, love Embla Carousel!

@davidjerleke
Copy link
Owner

Thanks @EntropyReversed, I’m glad to hear that. Closing this issue as resolved then.

Enjoy!
David

@davidjerleke davidjerleke added question Question about how to achieve something resolved This issue is resolved and removed awaiting response Issue is awaiting feedback feature request New feature or request labels Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about how to achieve something resolved This issue is resolved
Projects
None yet
Development

No branches or pull requests

2 participants