which is the fastest and easy way to fire when bootstrap-responsive.css media queries go in action?
go in action = when you resize window to mobile width and site is changed to responsive mobile
hope question is clear
Bootstrap primarily uses the following media query ranges—or breakpoints—in our source Sass files for our layout, grid system, and components.
As for the syntax, a media query always starts with the @media flag, followed by a mediatype. @media not|only mediatype and|not|only (media feature) { . my-code { … } } This mediatype is in charge of selecting which type of format will be subject to these rules.
At Twitter, Bootstrap has quickly become one of our many go-to front-end tools when starting new applications and sites.
I came up with an approach that uses window resize event, but relying on Twitter Bootstrap's media queries without hard coding their breakpoints:
<span id="mq-detector">
<span class="visible-xs"></span>
<span class="visible-sm"></span>
<span class="visible-md"></span>
<span class="visible-lg"></span>
</span>
#mq-detector {
visibility: hidden;
}
var currMqIdx = undefined;
var mqDetector = $("#mq-detector");
var mqSelectors = [
mqDetector.find(".visible-xs"),
mqDetector.find(".visible-sm"),
mqDetector.find(".visible-md"),
mqDetector.find(".visible-lg")
];
var checkForResize = function() {
for (var i = 0; i <= mqSelectors.length; i++) {
if (mqSelectors[i].is(":visible")) {
if (currMqIdx != i) {
currMqIdx = i;
console.log(mqSelectors[i].attr("class"));
}
break;
}
}
};
$(window).on('resize', checkForResize);
checkForResize();
One issue with the other answers is the change event is triggered EVERY resize. This can be very costly if your javascript is doing something significant.
The code below calls your update function only one time, when a threshold is crossed.
To test, grab your window size handle, and drag resize it quickly to see if the browser chokes.
<script>
// Global state variable
var winSize = '';
window.onresize = function () {
var newWinSize = 'xs'; // default value, check for actual size
if ($(this).width() >= 1200) {
newWinSize = 'lg';
} else if ($(this).width() >= 992) {
newWinSize = 'md';
} else if ($(this).width() >= 768) {
newWinSize = 'sm';
}
if( newWinSize != winSize ) {
doSomethingOnSizeChange();
winSize = newWinSize;
}
};
</script>
Using jquery you can find the size of current window and then accordingly do your stuff.
$(window).resize(function() {
if ($(this).width() >= 1280) {
//do something
}
else if ($(this).width() < 1280 && $(this).width()>= 980) {
//do something
}
...
});
CSS:: Twitter-Bootsrap-layouts
/* Large desktop */
@media (min-width: 1200px) { ... }
/* Portrait tablet to landscape and desktop */
@media (min-width: 768px) and (max-width: 979px) { ... }
/* Landscape phone to portrait tablet */
@media (max-width: 767px) { ... }
/* Landscape phones and down */
@media (max-width: 480px) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With