question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected results from scrollSnapList method

See original GitHub issue

Hello,

I am trying to create a carousel with this effect, pictured in screenshot 1 below. The implementation is similar to your iOS Picker example, where a rotation angle is calculated from the scroll progress. The problem is when a window resize event is triggered, some of the slides no longer center properly, see screenshot 2. Even if the window is resized by 1px, the difference is pretty drastic.

Screenshot 1 Screen Shot 2020-11-24 at 11 31 46 AM

Screenshot 2 Screen Shot 2020-11-24 at 11 32 00 AM

It seems that when the carousel is first initialized, embla.scrollSnapList() will return [-0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] for a carousel with 10 items, which looks right to me. However, after a resize event, embla.scrollSnapList() will return a different set of values, something like this [-0, 0.15507861048245647, 0.24100075577592645, 0.26451379351226284, 0.31647667722977557, 0.4308486685044219, 0.5452166758190794, 0.5971744258431836, 0.6898427790350868, 0.8449213895175434].

I would like to understand why embla.scrollSnapList() is returning different values after a resize event, and how I can force it to always use the correct snap points generated on initialization.

Thanks in advance

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
krikienoidcommented, Nov 25, 2020

@davidcetinkaya, the ReactJS solution to #116 was indeed what I needed. I just needed to figure how to do the same thing with VanillaJS.

Here is the solution I came up with, for anyone who has a similar issue:

embla.on('resize', () => {
    // Remove transforms
    embla.slideNodes().map(slideNode => {
        slideNode.style.opacity = 0;
        slideNode.style.transform = 'none';
    });

    // Re-initialize Embla in order for it to pick up the correct element dimensions
    embla.reInit();
    embla.dangerouslyGetEngine().translate.toggleActive(false);

    rotateSlidesFunc();
});
1reaction
krikienoidcommented, Nov 24, 2020

Hi @davidcetinkaya, I was able to find a solution. Thank you so much for your help and pointing me in the right direction. Have a nice day!

Read more comments on GitHub >

github_iconTop Results From Across the Web

scroll_snap_list | Flutter Package - Pub.dev
scroll_snap_list #. A wrapper for ListView.builder widget that allows "snaping" event to an item at the end of user-scroll. This widget allows unrestricted ......
Read more >
Flutter test scroll. dart#L316 We need to create a TestPointer ...
Now, Let's implement the Scroll Snap List through a simple application. ... 方法を説明します。. method. com Test flutter run does not have unexpected engine...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found