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

Open close instance depending on viewport #105

Open
t-book opened this issue May 24, 2019 · 0 comments
Open

Open close instance depending on viewport #105

t-book opened this issue May 24, 2019 · 0 comments
Labels

Comments

@t-book
Copy link

t-book commented May 24, 2019

Hi,

thanks for this great slider! I would need guidance with following. To save some space I'd like to open an instance on mobile

Bildschirmfoto 2019-05-24 um 13 01 37

and close it on desktop

Bildschirmfoto 2019-05-24 um 13 01 18

It would work to create two sliders, one initiated open the other closed, and hide the one or the other based on css media queries. Anyway, I think it would be nicer to only have one instance which changes depending on viewport resize. Here is a quick and dirty snippet which shows what I'm after:

   var instance;

    if ($(window).width() < 500){
        // initiate for small viewports closed
        instance = $('.image-gal-contain').Chocolat({
            container: '.image-gal-contain',
            loop: true,
        }).data('chocolat');
        instance.api().open();
        $('.image-gal-contain').addClass('image-gal-mobile');
    } else {
        // ... else open
        instance = $('.image-gal-contain').Chocolat({
            loop: true,
        }).data('chocolat');
        instance.api().close();
    }


    var resizeTimer;

    $(window).on('resize', function(e) {

      clearTimeout(resizeTimer);
      resizeTimer = setTimeout(function() {

        if ($(window).width() < 500){
            // on small vieports close the slider
            instance.destroy();
            instance = $('.image-gal-contain').Chocolat({
                container: '.image-gal-contain',
                loop: true,
            }).data('chocolat');
            instance.api().open();
            $('.image-gal-contain').addClass('image-gal-mobile');
        } else {
            // else open it
            instance.destroy();
            instance = $('.image-gal-contain').Chocolat({
                loop: true,
            }).data('chocolat');
            instance.api().close();
        }
                
      }, 400);

    });

The question is: Did someone did something similar before or is even something inbuild with chocolate? If not is it correct to destroy the instance or would you just call the constructor again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants