Hello friends I am trying to add a class to body dynamically depending on the browser window resolution. Here is the code I am trying to use but need help tuning it as I dont know jQuery at all.
The options I want to achieve are :
Once a visitor comes to my site, this code must check his browser windows size and add class to body as per the following rules
If window size is more than 1024px but less than 1280px then add class
.w1280
.If window size is more than 1280px but less than 1440px then add class
.w1440
.If window size is more than 1440px but less than 1280px then add class
.w1680
.If window size is more than 1680px then add class
.wLarge
.
To achieve this, I am trying to use the following script. My questions are:
Is this the correct code? If not what is the correct code?
Is this the best shortest possible code? If not what will be the correct code?
Kindly help as my knowledge of jQuery is almost ZERO.
function checkWindowSize() {
if ( $(window).width() > 1024) {
$('body').addClass('w1280');
} else {
$('body').removeClass('w1280');
}
if ( $(window).width() > 1280 ) {
$('body').addClass('w1440');
} else {
$('body').removeClass('w1440');
}
if ( $(window).width() > 1440) {
$('body').addClass('w1680');
} else {
$('body').removeClass('w1680');
}
if ( $(window).width() > 1600) {
$('body').addClass('wLarge');
} else {
$('body').removeClass('wLarge');
}
}
checkWindowSize()
var c = width ...
and then:c && $('body').addClass(c);
– Cepheus