Giter Club home page Giter Club logo

media-query-load's Introduction

Media Query Load

Media queries apply CSS rules when certain conditions are met in the browser. However, even when media queries are defined for specific CSS resources, e.g., media="screen and (min-width: 600px)" href="css/small.css", the browser will load all CSS resources regardless if they apply or not. That's unnecessary and results in poor performance.

What's needed is to make media queries not only apply styles according to certain criteria being met but also load the CSS resources needed on-demand.

matchMedia()

Using matchMedia lets you execute blocks of JavaScript only when a certain media query condition is met. This means you can just write out the CSS when and if the query is true:

if (window.matchMedia('screen and (min-width: 600px)')) {
  document.write('<link rel="stylesheet" href="css/small.css">');
}

However, instead of applying the CSS with a <link> element with a href which causes the undesired loading we'll use data-* attributes instead. Anything we want dependent on the query will get a data- prefix:

<link rel="stylesheet" class="mediaQueryDependent" 
  data-media="screen and (min-width: 600px)" 
  data-href="css/green.css">
<link rel="stylesheet" class="mediaQueryDependent" 
  data-media="screen and (min-width: 4000px)" 
  data-href="css/blue.css">

The function will loop through all the elements we want to change, evaluate their media queries, and change the data- prefixed attributes back to real ones:

(function () {
  var queriedResource = document.querySelectorAll('.mediaQueryDependent'),
    all = queriedResource.length,
    current = null,
    attr = null;
  while (all--) {
    current = queriedResource[all];
    if (current.dataset.media &&
        window.matchMedia(current.dataset.media).matches) {
      for (attr in current.dataset) {
        if (attr !== 'media') {
          current.setAttribute(attr, current.dataset[attr]);
        }
      }
    }
  }
}());

Here's what this function is doing:

  • First, it uses querySelectorAll to get all the elements that need media query checks and loops over them (using a reverse while loop).
  • Second, it tests if the element has a data-media property and if the query defined in it is true.
  • Lastly, it loops through all data-* prefixed attributes and adds a non-prefixed attribute with its value (omitting the media one).

Support

Current versions of Chrome, Firefox, and Safari, and IE10+ support matchMedia. Read more.

media-query-load's People

Contributors

allthingssmitty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.