본문 바로가기

프로그래밍 예제/ 예제

브라우저 창에 따라 클래스 변화시키기

$(window).resize(function() {
    var width = document.documentElement.offsetWidth,
        sizeMode = width > 1440 ? 4 : width > 1023 ? 3 : width > 767 ? 2 : 1;
    
document.documentElement.className =
document.documentElement.className.replace(/ *s[1-4][1-4]?/g, '') +
(' s'+ sizeMode +' s'+ (3 > sizeMode ? 12 : 34) + (360 > width ? ' s0' : ''));
});


resize 될 때 변수 sizeMode 값의 변화에 따라 정규표현식 문법 사용하여,

기존 클래스 변환시키기


반응형 사이트에 사용가능 


* 삼성 글로벌 common.js 참고