mediaquery-event

0.5.0 • Public • Published

mediaquery-event

Get events when mediaquery changes happen. written in plain vanilla js.

example

document.addEventListener('mediaquery', function (event) {

  if (!event.detail.active) {
    return; // dont react to mediaqueries becoming inactive
  }
  
  if (event.detail.media === 'desktop') {
      // do something
  }
  
  if (event.detail.media === 'mobile') {
      // do something else
  }
});

mqe.init({
    mediaqueries: [
        {name: 'mobile', media: 'screen and (max-width: 1023px)'},
        {name: 'desktop', media: 'screen and (min-width: 1024px)'}
    ]
});

bugs and caveats

Good ol' InternetExplorer handles some stuff differently (no kiddin'), not all of these "issues" are handled yet:

  • Your mediaquery.media should start with "screen and", otherwise IE adds its own idea to it.

Readme

Keywords

none

Package Sidebar

Install

npm i mediaquery-event

Weekly Downloads

1

Version

0.5.0

License

MIT

Unpacked Size

153 kB

Total Files

14

Last publish

Collaborators

  • rejas