Skip to content

Instantly share code, notes, and snippets.

@lutzissler
Created September 13, 2013 10:26
Show Gist options
  • Save lutzissler/6549000 to your computer and use it in GitHub Desktop.
Save lutzissler/6549000 to your computer and use it in GitHub Desktop.
Watch responsive layout changes with the help of an additional element in the body and an appropriate color setting.
@media screen and (min-width: ...) {
// Adjust the breakpoint identifier so that jQuery knows about it
#bp {
color: rgb(1, 1, 1);
}
}
@media screen and (min-width: ...) {
// Adjust the breakpoint identifier so that jQuery knows about it
#bp {
color: rgb(2, 2, 2);
}
}
...
// Watch responsive breakpoints
var bp = false;
$(window)
.on("resize", function () {
var identifier = $("#bp"),
new_bp = false;
if (identifier.length == 0) {
$("body").append('<div id="bp"/>');
identifier = $("#bp");
}
var color = identifier.css("color");
for (var i = 0; i <= 9; i++) {
var rgb = 'rgb(' + i + ', ' + i + ', ' + i + ')';
if (color == rgb) {
new_bp = i;
}
}
if (new_bp != bp) {
$(window).trigger("layoutchanged", { new_bp: new_bp, old_bp: bp });
bp = new_bp;
}
})
.trigger("resize");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment