Skip to content

Instantly share code, notes, and snippets.

@yeromin
Last active May 31, 2023 14:54
Show Gist options
  • Save yeromin/f927815877a1b78894cd2552f527f3e5 to your computer and use it in GitHub Desktop.
Save yeromin/f927815877a1b78894cd2552f527f3e5 to your computer and use it in GitHub Desktop.

JavaScript media query

  try {
    const mediaQueryMax1023 = window.matchMedia('(max-width: 1023px)')
    const mediaQueryMin1024 = window.matchMedia('(min-width: 1024px)')
  
    mediaQueryMax1023.addListener(mobile);
    function mobile(media) {
      if (media.matches) {
        klevuHelpers.addFilterButton() // load if <= 1023
        console.log(`load if <= 1023`);
      }
    };
    mobile(mediaQueryMax1023);

    mediaQueryMin1024.addListener(desktop);
    function desktop(media) {
      if (media.matches) {
        klevuHelpers.createFilterWrapperBlock() // load if > 1023
        klevuHelpers.moveFiltersToWrapper() // load if > 1023
        console.log(`load if >= 1024`);
      }
    };
    desktop(mediaQueryMin1024);
  } catch (error) {
    console.log(error);
  }

https://css-tricks.com/working-with-javascript-media-queries/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment