Last active
April 16, 2020 10:32
-
-
Save steveh80/288a9a8bd4c3de16d799 to your computer and use it in GitHub Desktop.
Bootstrap viewport detection in Javascript
This file contains hidden or 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
// needed for viewport size detection in javascript | |
body::before { | |
display: none; | |
content: "xs"; | |
} | |
@media (min-width: $screen-sm-min) { | |
body::before { | |
content: "sm"; | |
} | |
} | |
@media (min-width: $screen-md-min) { | |
body::before { | |
content: "md"; | |
} | |
} | |
@media (min-width: $screen-lg-min) { | |
body::before { | |
content: "lg"; | |
} | |
} |
This file contains hidden or 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
/** | |
* helper to detect viewport sizes in javascript | |
* usage examples: | |
* viewport.is('xs') | |
* viewport.isEqualOrGreaterThan('md') | |
*/ | |
var viewport = (function() { | |
var viewPorts = ['xs', 'sm', 'md', 'lg']; | |
var viewPortSize = function() { | |
return window.getComputedStyle(document.body, ':before').content.replace(/"/g, ''); | |
} | |
var is = function(size) { | |
if ( viewPorts.indexOf(size) == -1 ) throw "no valid viewport name given"; | |
return viewPortSize() == size; | |
} | |
var isEqualOrGreaterThan = function(size) { | |
if ( viewPorts.indexOf(size) == -1 ) throw "no valid viewport name given"; | |
return viewPorts.indexOf(viewPortSize()) >= viewPorts.indexOf(size); | |
} | |
// Public API | |
return { | |
is: is, | |
isEqualOrGreaterThan: isEqualOrGreaterThan | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment