Browse files

Use a better has/add/removeClass implementation. Closes #5

  • Loading branch information...
1 parent 00ee506 commit 66651b70b3927dd82b6f1706deedbc07b89a7a0a @benschwarz committed Aug 17, 2012
Showing with 15 additions and 19 deletions.
  1. +3 −4 README.md
  2. +11 −14 metaquery.js
  3. +1 −1 metaquery.min.js
View
7 README.md
@@ -9,9 +9,9 @@ With metaquery, you define your media query breakpoints once, and only once.
## Getting Started
-Download the [production version][min] (723 bytes) or the [development version][max].
+Download the [production version][min] (691 bytes) or the [development version][max].
-or, if you want the smaller (476 bytes!), lighter jQuery powered edition:
+or, if you want the smaller (480 bytes!), lighter jQuery powered edition:
* [production jQuery version][minjq]
* [development jQuery version][maxjq]
@@ -64,7 +64,7 @@ Modernizr.load([{
Considering the HTML example above, say you wanted watch for breakpoint changes:
``` javascript
-metaQuery.bind( 'phone', function (match) {
+metaQuery.bind( 'phone', function ( match ) {
if( match ) { // phawor! your media query matches. }
});
```
@@ -107,7 +107,6 @@ Code:
Copyright (c) 2012 Ben Schwarz
Licensed under the MIT license.
-
[matchMedia.js]: https://github.com/paulirish/matchMedia.js
[matchmedia-oldie]: https://github.com/benschwarz/matchMedia.js/tree/IE7-8
[picturefill]: https://github.com/scottjehl/picturefill
View
25 metaquery.js
@@ -46,26 +46,23 @@
};
},
- classRegex = {},
- getClassRegex = function(className) {
- if (!classRegex[className]) {
- classRegex[className] = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)', 'g');
- }
- classRegex[className].lastIndex = 0;
- return classRegex[className];
+ hasClass = function( element, className ) {
+ return element.className.split(' ').indexOf( className ) !== -1;
},
- hasClass = function(element, className) {
- return getClassRegex( className ).test( element.className );
- },
+ removeClass = function( element, className ) {
+ var classes = element.className.split( ' ' ),
+ id = classes.indexOf( className );
- removeClass = function(element, className) {
- element.className = ( element.className || '' ).replace( getClassRegex(className), '' );
+ if ( hasClass( element, className ) ) {
+ classes.splice( id, 1 );
+ element.className = classes.join( ' ' );
+ }
},
addClass = function(element, className) {
- if ( !hasClass(element, className) ) {
- element.className = ( element.className ? element.className + ' ' : '' ) + className;
+ if ( !hasClass( element, className ) ) {
+ element.className = ( element.className !== '' ) ? ( element.className + ' ' + className ) : className;
}
},
View
2 metaquery.min.js
@@ -1 +1 @@
-(function(a,b){var c={breakpoints:{},_events:{},_eventMatchCache:{},bind:function(a,b){(c._events[a]=[]).push(b),n()}},d=function(c){/in/.test(b.readyState)?a.setTimeout(function(){d(c)},9):c()},e=function(a,c,d){b.addEventListener?a.addEventListener(c,d):a.attachEvent("on"+c,d)},f=function(b,c){function g(){f=null,b.apply(e,d)}var d,e,f;return function(){a.clearTimeout(f),f=a.setTimeout(g,c)}},g={},h=function(a){return g[a]||(g[a]=new RegExp("(?:^|\\s+)"+a+"(?:\\s+|$)","g")),g[a].lastIndex=0,g[a]},i=function(a,b){return h(b).test(a.className)},j=function(a,b){a.className=(a.className||"").replace(h(b),"")},k=function(a,b){i(a,b)||(a.className=(a.className?a.className+" ":"")+b)},l=function(a,c){var d="breakpoint-"+c,e=b.documentElement;a?k(e,d):j(e,d)},m=function(a,c){if(!a)return;var d=b.getElementsByTagName("img");for(var e=0;e<d.length;e++){var f=d[e],g=f.getAttribute("data-mq-src");g&&(f.src=g.replace("[breakpoint]",c))}},n=function(){for(var b in c.breakpoints){var d=c.breakpoints[b],e=a.matchMedia(d).matches;if(c._events[b]&&c._eventMatchCache[b]!==e)for(var f=0;f<c._events[b].length;f++){var g=c._events[b][f];c._eventMatchCache[b]=e,typeof g=="function"&&g(e)}l(e,b),m(e,b)}},o=function(){var a=b.getElementsByTagName("meta");for(var d=0;d<a.length;d++)if(a[d].name==="breakpoint"){var e=a[d].getAttribute("data"),f=a[d].getAttribute("media");c.breakpoints[e]=f}},p=function(){o(),e(a,"resize",f(function(){n()},50)),n()};a.metaQuery=c,d(p)})(this,this.document);
+(function(a,b){var c={breakpoints:{},_events:{},_eventMatchCache:{},bind:function(a,b){(c._events[a]=[]).push(b),l()}},d=function(c){/in/.test(b.readyState)?a.setTimeout(function(){d(c)},9):c()},e=function(a,c,d){b.addEventListener?a.addEventListener(c,d):a.attachEvent("on"+c,d)},f=function(b,c){function g(){f=null,b.apply(e,d)}var d,e,f;return function(){a.clearTimeout(f),f=a.setTimeout(g,c)}},g=function(a,b){return a.className.split(" ").indexOf(b)!==-1},h=function(a,b){var c=a.className.split(" "),d=c.indexOf(b);g(a,b)&&(c.splice(d,1),a.className=c.join(" "))},i=function(a,b){g(a,b)||(a.className=a.className!==""?a.className+" "+b:b)},j=function(a,c){var d="breakpoint-"+c,e=b.documentElement;a?i(e,d):h(e,d)},k=function(a,c){if(!a)return;var d=b.getElementsByTagName("img");for(var e=0;e<d.length;e++){var f=d[e],g=f.getAttribute("data-mq-src");g&&(f.src=g.replace("[breakpoint]",c))}},l=function(){for(var b in c.breakpoints){var d=c.breakpoints[b],e=a.matchMedia(d).matches;if(c._events[b]&&c._eventMatchCache[b]!==e)for(var f=0;f<c._events[b].length;f++){var g=c._events[b][f];c._eventMatchCache[b]=e,typeof g=="function"&&g(e)}j(e,b),k(e,b)}},m=function(){var a=b.getElementsByTagName("meta");for(var d=0;d<a.length;d++)if(a[d].name==="breakpoint"){var e=a[d].getAttribute("data"),f=a[d].getAttribute("media");c.breakpoints[e]=f}},n=function(){m(),e(a,"resize",f(function(){l()},50)),l()};a.metaQuery=c,d(n)})(this,this.document);

0 comments on commit 66651b7

Please sign in to comment.