Code Monkey home page Code Monkey logo

mediaquery's Introduction

MediaQuery

MediaQuery is a javascript-powered cross-browser media query and media query events handler. It supports queries on the viewport and device as well as more advanced min/max greater-than/less-than queries. It is tested in all browsers on both PC and Mac systems, including the ancient IE6+.

What does it support, exactly?

  • width / height / aspect-ratio / orientation / font-size
  • device-width / device-height / device-aspect-ratio / device-orientation
  • values written with em, in, pt, px, %, and empty suffixes.
  • min-property, max-property, property <, property >

How to use it

First, add the MediaQuery script to your site or page.

<script src="MediaQuery.js"></script>

Evaluate a query on the fly.

var result = MediaQuery('min-width: 640px').matches; // returns a boolean: true or false if whether the media query was matching

Save a media query object to be evaluated anytime later.

var mq = MediaQuery('min-width: 640px');

// many moments later

mq.matches;

Read or replace a media query with the query method.

MediaQuery('min-width: 640px').query('width > 640px').query; // returns a string: "width > 640px"

Add an event with addEvent, which will run each time the media query changes to or from matching.

MediaQuery('min-width: 640px').addEvent(function (matches, data) {
	matches; // whether the media query was matching or not
	data; // the media query as a javascript object
});

Add a classname to <html> with addClass, which will only be added when the media query is matching.

MediaQuery('width < 768').addClass('handheld');

Browser support

  • Google Chrome
  • Mozilla Firefox 3+
  • Apple Safari 4+
  • Opera 10+
  • Internet Explorer 6+

License

MIT

Acknowledgements

MediaQuery is a project by Jonathan Neal.

mediaquery's People

Contributors

jonathantneal avatar

Watchers

 avatar James Cloos 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.