윈도우 창 크기가 변할 때 다른 작업하는 방법 - .resize()

resize는 윈도우 크기가 바뀔 때 어떤 작업을 할 수 있게 합니다.
1 min read

.resize()

.resize()는 윈도우 크기가 바뀔 때 어떤 작업을 할 수 있게 합니다.

 

기본형

$( window ).resize( function() {
  // do somthing
});

 

예제

웹브라우저의 크기를 변경할 때, p 요소의 가로폭을 출력합니다. 윈도우 크기를 변경하면 숫자가 바뀝니다.

<p></p>
p {
  width: 80%;
  margin: auto;
  padding: 20px;
  text-align: center;
  border: 1px solid #bcbcbc;
}
$( document ).ready( function() {
  var Width = $( 'p' ).width();
  $( 'p' ).append( Width );
  $( window ).resize( function() {
    Width = $( 'p' ).width();
    $( 'p' ).empty().append( Width );
  });
});

You may like these posts

  • .clone() .clone()은 선택한 요소를 복제합니다.   기본형 .clone( [ withDataAndEvents ] ) 예를 들어 $( '.ab' ).clone().appendTo( 'h1' ); 은 ab를 클래스 값으로 가지는 요소를 복제하여 h1 요소에 넣습니다.  …
  • .attr() .attr()은 요소(element)의 속성(attribute)의 값을 가져오거나 속성을 추가합니다.   기본형 1 .attr( attributeName ) 선택한 요소의 속성의 값을 가져옵니다. 예를 들어 $( 'div' ).attr( 'class' ); 는…
  • .before() .before()는 선택한 요소 앞에 새 요소를 추가하거나, 다른 곳에 있는 요소를 이동시킬 수 있습니다.   기본형 .before( content [, content ] ) 예를 들어 $( 'h1' ).before( '<p>Hello</p>'…
  • .children() .children()은 어떤 요소의 자식 요소를 선택합니다.   기본형 .children( [ selector ] ) 예를 들어 $( 'div' ).children().css( 'color', 'blue' ); 는 <div> 요소의 자식 요소의 색을 파란색으…
  • .css() .css()로 선택한 요소의 css 속성값을 가져오거나 style 속성을 추가합니다.   기본형 1 .css( propertyName ) 속성값을 가져옵니다. 예를 들어 $( "h1" ).css( "color" ); 는 <h1> 요소의 스타일 중 color 속성의 값…
  • .click() .click()은 선택한 요소를 클릭했을 때 특정 작업을 수행할 수 있게 하는 속성입니다.   기본형 .click( handler ) 예를 들어 button 요소를 클릭했을 때 함수를 실행시키고 싶으면 다음과 같이 합니다. $( 'button' ).clic…

Post a Comment