function Media () {
this.elems = [];
this.matches = {};
}
Media.sizes = ["original", "small", "medium", "large", "nav", "wide", "huge", "max", "edge", "over"];
Media.attrs = Media.sizes.map(function (size) { return "media-" + size; });
Media.query = Media.attrs.map(function (attr) { return "[" + attr + "]" }).join(",");
Media.widths = [0, 320, 540, 720, 900, 980, 1280, 1600, 2400, 9000];
Media.spaceReg = /\s+/;
Media.prototype = {
init: function () {
this.update();
document.querySelectorAlways(Media.query, this.addElem.bind(this));
window.addEventListener("resize", this.update.bind(this));
window.addEventListener("load", this.resize.bind(this));
},
resize: function () {
if (window.trigger) {
window.trigger("resize");
} else {
this.update();
}
},
addElem: function (elem) {
// save the original classes and always apply them
elem.setAttribute("media-original", elem.className);
this.elems.push(elem);
this.updateElem(elem);
},
update: function () {
this.width = window.innerWidth;
var minWidth = Media.widths[1];
if (this.width < minWidth) {
this.width = minWidth;
}
Media.widths.map(this.updateWidth.bind(this))
this.elems.map(this.updateElem.bind(this));
},
updateWidth: function (width, i) {
var media = "media-" + Media.sizes[i];
this.matches[media] = this.width >= width;
},
updateElem: function (elem) {
Media.attrs.map(this.updateMedia.bind(this, elem));
},
updateMedia: function (elem, media) {
var attr = elem.getAttribute(media);
if (attr) {
var classes = attr.split(Media.spaceReg);
classes.map(this.updateClass.bind(this, elem, media))
}
},
updateClass: function (elem, media, elemClass) {
var match = this.matches[media];
var negate = elemClass.charAt(0) === "!";
if (negate) {
if (match) {
var doubleNegate = elemClass.charAt(1) === "!";
if (doubleNegate) {
elem.classList.add(elemClass.slice(2));
} else {
elem.classList.remove(elemClass.slice(1));
}
}
} else {
elem.classList[match ? "add" : "remove"](elemClass);
}
}
};
var media = new Media();
media.init();
window.Media = Media;
window.media = media;
function querySelectorAlways (selector, callback)
Есть кто понимает в этом хорошо и может помочь переделать в jquery ?