Skip to content

Instantly share code, notes, and snippets.

@prbaron
Created June 17, 2015 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prbaron/6460f566673ff0282494 to your computer and use it in GitHub Desktop.
Save prbaron/6460f566673ff0282494 to your computer and use it in GitHub Desktop.
Importing CSS Breakpoints Into Javascript - based on https://www.lullabot.com/blog/article/importing-css-breakpoints-javascript
//
// Get the breakpoint
//
var breakpoint = {};
breakpoint.refreshValue = function () {
this.value = window.getComputedStyle(document.querySelector('body'), ':before').getPropertyValue('content').replace(/"/g, '');
};
//
// resize handler
//
$(window).resize(function () {
breakpoint.refreshValue();
}).resize();
//
// In use
//
if (breakpoint.value == 'tablet') {
console.log('Tablet breakpoint');
}
else {
console.log('Some other breakpoint');
}
/**
* These values will not show up in content, but can be
* queried by JavaScript to know which breakpoint is active.
* Add or remove as many breakpoints as you like.
*/
body:before {
content: "smartphone";
display: none; /* Prevent from displaying. */
}
@media (min-width: 700px) {
body:before {
content: "tablet";
}
}
@media (min-width: 1100px) {
body:before {
content: "desktop";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment