Answer the question
In order to leave comments, you need to log in
How to remove and add a class in JS (jQuery) on screen resize? Problem after page refresh?
Hello.
Not strong in JS and jQuery, I did everything according to the manual and thanks to tips from articles from the Internet.
There is this HTML:
<div class="container">
<div class="row">
<div class="col-xs-12">
CONTENT
</div>
</div>
</div>
$(document).ready(function() {
$(window).resize(function(){
var windowWidth = $('body').innerWidth();
if(windowWidth < 480){$(".number-bullets").removeClass('col-xs-12').addClass('col-xs-4');}
else{$(".number-bullets").removeClass('col-xs-4').addClass('col-xs-12');}
});
});
Answer the question
In order to leave comments, you need to log in
You can do it like this:
$(document).ready(function() {
function checkWidth() {
var windowWidth = $('body').innerWidth(),
elem = $(".number-bullets"); // лучше сохранять объект в переменную, многократно чтобы не насиловать
// страницу для поиска нужного элемента
if(windowWidth < 480){
elem.removeClass('col-xs-12');
elem.addClass('col-xs-4');
}
else{
elem.removeClass('col-xs-4');
elem.addClass('col-xs-12');
}
}
checkWidth(); // проверит при загрузке страницы
$(window).resize(function(){
checkWidth(); // проверит при изменении размера окна клиента
});
});
And no one is embarrassed by what css should do js does and through ... Or is it just a bootstrap of everything. If such a site came to me, I would definitely send it for re-layout. Perhaps the layout designers would add the necessary breakpoint or remove the bootstrap
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question