RooWM
RooWM

Reputation: 583

jquery if width is less than, change this attr

    $(".section").each(function(i, el){
    var section = $('.section');
    var width = section.width();
    if (width < 960)
        section.attr('class', 'section-slim');
});

This seems to work fine, on browser refresh, how do I make it act when resized? Ex: if someone makes their browser window smaller, it will add this class?

Upvotes: 1

Views: 2819

Answers (2)

Austin
Austin

Reputation: 6034

bind the event to jQuery's "resize" event

$(window).on("resize load", function () {
    $(".section").each(function(i, el){
    var section = $('.section');
    var width = section.width();
    if (width <= 960) {
        section.attr('class', 'section-slim');
    }
})

Upvotes: 4

IT ppl
IT ppl

Reputation: 2647

Put your code in one common function than call it from window.resize()

$(window).resize(function() { //you can call your function here
    // do something here
});

Upvotes: 0

Related Questions