2011-09-13 5 views
1

컨테이너의 너비를 알아 내고 자식 요소에 특정 크기 (사이드 바 크기 빼기)를 알려주는 jQuery 코드가있다. 화면을 채우십시오.jQuery 브라우저 창 크기가 바뀌더라도 요소 너비를 알아 낸다.

사용자가 브라우저 창을 업데이트하지 않는 경우를 제외하고는 잘 작동합니다! 따라서 실시간으로 작업하려면 jQuery 코드가 필요합니다. 누구든지 도와 줄 수 있습니까? 감사합니다

$(document).ready(function() { 

    // find out the width of the page 
    var pageWidth = $('div.MainContentHolder').width(); 
    // minus the sidebar and margin width 
    var middleWidth = pageWidth - '220'; 
    // apply the new width to the middle column 
    $('div.MiddleColumn').css('width', middleWidth); 

}); 

답변

1

이동을 별도의 함수에 코드와는 document readywindow resize 이벤트를 모두 실행이 있습니다.

function resizeElements() { 
    // find out the width of the page 
    var pageWidth = $('div.MainContentHolder').width(); 
    // minus the sidebar and margin width 
    var middleWidth = pageWidth - '220'; 
    // apply the new width to the middle column 
    $('div.MiddleColumn').css('width', middleWidth); 
} 

$(document).ready(function() { 
    resizeElements(); 
    $(window).resize(resizeElements); 
} 
+0

을 덧붙여는'$ ('div.MiddleColumn')의 폭 (middleWidth가)'이다 '$ ('div.MiddleColumn'). css ('width', middleWidth)'보다 낫다. 'css'를 사용하는 것은 느리고'### px' 형식을 기대합니다. – Blazemonger

+0

@ mblase75 :'.css()'는 숫자를'px' 값으로 해석합니다 ('z-index'와 같은 몇 가지 특정 CSS 속성 제외). –

3

그냥 $ (window) .resize()에 바인딩해야합니다.

$(document).ready(function() { 

    $(window).resize(function() { 
     // find out the width of the page 
     var pageWidth = $('div.MainContentHolder').width(); 
     // minus the sidebar and margin width 
     var middleWidth = pageWidth - '220'; 
     // apply the new width to the middle column 
     $('div.MiddleColumn').css('width', middleWidth); 
    }).resize(); 

}); 
관련 문제