0

I have this piece of code, but I was wondering if there was a way to reduce the amount of lines?

function winResize(w) {
    if (w >= 250 && w < 500 ) { $('body').alterClass('col_*', 'col_1'); }
    if (w >= 500 && w < 750 ) { $('body').alterClass('col_*', 'col_2'); }
    if (w >= 750 && w < 1000 ) { $('body').alterClass('col_*', 'col_3'); }
    if (w >= 1000 && w < 1250 ) { $('body').alterClass('col_*', 'col_4'); }
    if (w >= 1250 && w < 1500 ) { $('body').alterClass('col_*', 'col_5'); }
    if (w >= 1500 && w < 1750 ) { $('body').alterClass('col_*', 'col_6'); }
    if (w >= 1750 && w < 2000 ) { $('body').alterClass('col_*', 'col_7'); }
    if (w >= 2000 && w < 2250 ) { $('body').alterClass('col_*', 'col_8'); }
    if (w >= 2250 && w < 2500 ) { $('body').alterClass('col_*', 'col_9'); }
    if (w >= 2500 ) { $('body').alterClass('col_*', 'col_10'); }
}

This is how I call the function

winResize(window.innerWidth);
4

1 回答 1

6

看起来您的间隔是 250 的倍数。您可以利用它来获得优势:

function winResize(w) {
    var index = Math.min(10, Math.floor(w / 250));
    if (index > 0) {
        $('body').alterClass('col_*', 'col_' + index);
    }
}
于 2013-06-17T06:36:24.293 回答