Created
June 17, 2015 15:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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