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

destroy and init on resize #37

Closed
isaydesign opened this issue Apr 10, 2019 · 1 comment
Closed

destroy and init on resize #37

isaydesign opened this issue Apr 10, 2019 · 1 comment

Comments

@isaydesign
Copy link

isaydesign commented Apr 10, 2019

Hello, I'm trying to init / destroy a slinky menu, depending on the viewport size.
I'm using this function on resize:

const handleMenuMobile = function () {
    if (window.matchMedia("(max-width: 1099px)").matches) {
        const menuMobile = $('.menu-wrapper').slinky({
            title: true,
            theme: ''
        });

        console.log (menuMobile);
    }

    if (window.matchMedia("(min-width: 1100px)").matches) {
        if (typeof menuMobile !== 'undefined') {
            menuMobile.destroy();
        }
    }
}

The second bit of code (min-width: 1100px) doesn't work because it always returns menuMobile as undefined. I'm guessing this is a scope problem... Can you help me fix this?

Thanks in advance.

@alizahid
Copy link
Owner

This should fix your scope problem;

const handleMenuMobile = function() {
  let menuMobile

  if (window.matchMedia('(max-width: 1099px)').matches) {
    menuMobile = $('.menu-wrapper').slinky({
      title: true,
      theme: ''
    })
  }

  if (window.matchMedia('(min-width: 1100px)').matches) {
    if (typeof menuMobile !== 'undefined') {
      menuMobile.destroy()
    }
  }
}

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

No branches or pull requests

2 participants