Last active
August 29, 2015 14:05
-
-
Save yankeyhotel/853b7c23550aaec93718 to your computer and use it in GitHub Desktop.
Use CSS media queries and Javascript together to determine browser width.
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
/* position static in mobile media queries by default */ | |
#js-media-test { position: static; } | |
/* and position it relative in larger sizes | |
you can set the break to whatever makes the most sense */ | |
@media (max-width: 769px) { | |
#js-media-test { position: relative; } | |
} |
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
<!-- add this html to just before the closing body tag --> | |
<div id="js-media-test" class="hide"></div> |
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
// Now you can test the css property much easier than testing | |
// screen width etc. | |
$(document).ready(function(){ | |
if ( $('#js-media-test').css("position") == "relative") { | |
// do something | |
} else { | |
// do something else | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a much cooler way of doing the same thing check this out from my friend Boots. https://gist.github.com/bootsified/47b79fbf12ff7eccef66