Animate()는 다양한 효과 연출을 위해 다양한 기능을 제공한다.

 

1. 기본 사용법

animate의 문법은 아래와 같다.

$(selector).animate({params},speed,callback);
 

param : 애니메이션으로 정의할 CSS 속성.

speed : 애니메이션 효과 연출 시간

callback : 연출 종료 후 호출할 함수

 

아래는 animate()로 박스를 좌 <-> 우 로 움직인다.

  <div id="box"  style="background-color:pink; width:100px; height:100px;position:absolute;">눌러보세요</div>

var b = false;
$(document).ready(function(){
  $("#box").click(function(){
    if(!b) {$('#box').animate({left: '250px'}); b=!b;}
    else { $('#box').animate({left: '0px'}); b=!b;}
  });
});
 

2. Animate 응용

Animate는 여러 속성을 동시에 적용이 가능하다.

아래의 코드는 박스가 커지며 색이 옅어지는 효과를 준다.

$(document).ready(function(){
  $("#box2").click(function(){
    $("#box2").animate({
      left: '250px',
      opacity: '0.5',
      height: '150px',
      width: '150px'
    });
  });
});

  <div id="box2"  style="background-color:skyblue; width:200px; height:200px;position:absolute;">눌러보세요</div>
 

또한 Animate는 속성값의 크기를 증감하여 다양한 형태로 표현 가능하다.

주의 :: "+=150px" 안에 스페이스바(공백)가 들어가면 안됨!

$(document).ready(function(){
  $("#box2").click(function(){
    $("#box2").animate({
      left: '250px',
      height: '+=150px',
      width: '+=150px'
    });
  });
});

  <div id="box2"  style="background-color:skyblue; width:200px; height:200px;position:absolute;">눌러보세요</div>
 

3. Animate toggle

속성을 toggle 로 하여 이벤트에 따라 상태를 계속 변경할 수 있다.

$(document).ready(function(){
  $("#btn").click(function(){
    $('#box').animate({
      width: 'toggle'
    });
  });
});

<button id="btn">퐁</button>
<div id="box"  style="background-color:pink; width:100px; height:100px; position:absolute;"></div>  
 

4. Animate 순차적으로 실행

선택자를 변수에 넣으면 여러 효과를 순차적으로 실행할 수 있다.

 

- 상하좌우 움직이기

$(document).ready(function(){
  $("#btn").click(function(){
    var div = $('#box');
    div.animate({height: '300px', opacity: '0.4'}, "slow");
    div.animate({width: '300px', opacity: '0.8'}, "slow");
    div.animate({height: '100px', opacity: '0.4'}, "slow");
    div.animate({width: '100px', opacity: '0.8'}, "slow");
  });
});

<button id="btn">퐁</button>
<div id="box"  style="background-color:pink; width:100px; height:100px; position:absolute;"></div>
 

- div 이동 후 폰트 확대

$(document).ready(function(){
  $("#btn").click(function(){
    var div = $('#box');
    div.animate({left: '100px'}, "slow");
    div.animate({fontSize: '3em'}, "slow");
  });
});

<button id="btn">퐁</button>
<div id="box"  style="background-color:pink; width:100px; height:100px; position:absolute;">헤헿</div>
  
 

 

 

'JAVASCRIPT > JQUERY' 카테고리의 다른 글

slide  (0) 2022.08.28
fadeIn - fadeOut  (0) 2022.08.28
hide - show  (0) 2022.08.28
Event  (0) 2022.08.28
Selector  (0) 2022.08.28

 

* slide

- slideUp : 해당 부분이 아래에서 위로 hide 됨

- slideDown : 해당 부분이 위에서 아래로 show 됨

- slideToggle : Up, Down 모두 가능

※ :: hide-show 에 시간을 설정하면 동일한 효과가 나타는 듯 하다.

※ :: 좌, 우 슬라이드는 animate() 에서 다룬다.

$(selector).slideUp(speed,callback);
$(selector).slideDown(speed,callback);
$(selector).slideToggle(speed,callback);
 
$(document).ready(function(){
  $("#divbtn").click(function(){
    $("#divpanel").slideToggle("slow");
  });
});

<div id="divbtn"  style="background-color:pink; width:100px;";>눌러보세요</div>
<div id="divpanel" style="background-color:skyblue; display:none; width:100px; height:300px";>촤르르륵</div>
 

 

 

 

 

'JAVASCRIPT > JQUERY' 카테고리의 다른 글

Animate()  (0) 2022.08.28
fadeIn - fadeOut  (0) 2022.08.28
hide - show  (0) 2022.08.28
Event  (0) 2022.08.28
Selector  (0) 2022.08.28

 

* fadeIn - fadeOut

주어진 시간동안 서서히 사라지거나 나타난다.

$(selector).fadeIn(speed,callback);      // 서서히 나타남
$(selector).fadeOut(speed,callback);     // 서서히 사라짐
$(selector).fadeToggle(speed,callback);  // 사라지거나 나타나거나
 

speed : hide-show와 동일하게 'fast' or 'slow' 를 주거나 ms 값을 넣는다.

callback : 실행 후 호출할 함수

$(document).ready(function(){
  $("#button_fadein").click(function(){
    $("h2").fadeIn(3000, function(){alert("The paragraph is now hidden")});
  });
});

$(document).ready(function(){
  $("#button_fadeout").click(function(){
    $("h2").fadeOut(3000);
  });
});
 

 

* fadeTo

주어진 값 만큼 불투명으로 효과를 준다.

$(selector).fadeTo(speed,opacity,callback);
 

speed : 흐려지는데 걸리는 시간

opacity : 불투명도 (0~1 사이값)

callback : 실행 후 호출 함수

$(document).ready(function(){
  $(".buttonfadeto").click(function(){
    $("#fade1").fadeTo('slow',0.1);
  });
});

$(document).ready(function(){
  $(".buttonfadeto").click(function(){
    $("#fade2").fadeTo('slow',0.5);
  });
});

$(document).ready(function(){
  $(".buttonfadeto").click(function(){
    $("#fade3").fadeTo('slow',0.9);
  });
});
 

 

 

'JAVASCRIPT > JQUERY' 카테고리의 다른 글

Animate()  (0) 2022.08.28
slide  (0) 2022.08.28
hide - show  (0) 2022.08.28
Event  (0) 2022.08.28
Selector  (0) 2022.08.28

 

단어 그대로 보여주기, 숨기기, 토글 효과.

 

 

1. hide - show

fade-in out 등 다양한 효과들과 혼합하여 사용 가능하다.

기본 문법은 아래와 같다.

$(selector).hide(speed,callback);

$(selector).show(speed,callback);
 

- speed : "slow", "fast" 등으로 속도 조절 또는 ms 단위로 시간 제어 설정 ex:: hide("slow" or 5000)

speed를 설정하지 않거나 0이면 즉시 사라지거나 나타난다.

- callback : 효과가 실행 -> 종료된 후 호출되는 함수.

아래의 문장은 p태그가 숨겨진 후 function을 실행하여 alert이 발생한다.

$(document).ready(function(){
  $("#button").click(function(){
    $("p").hide(3000, function(){alert("The paragraph is now hidden")});
  });
});
 

 

2. toggle

hide와 show 가 합쳐진 기능이라 생각하면 된다.

아래는 기본 문법이다.

$(selector).toggle(speed,callback);
 

speed와 callback 은 hide-show와 동일하다.

 

처음 누르면 hide, 두 번째 누르면 show 가 된다. (완료 후 callback으로 alert을 발생한다.)

$(document).ready(function(){
  $("#button_toggle").click(function(){
    $("p").toggle(3000, function(){alert("The paragraph is now hidden")});
  });
});
 

 

3. hide-show , toggle 과 비슷한 효과

 

 

 

* animate

다양한 효과를 보면서 나타나거나 사라진다.

$(document).ready(function(){
  $("#button_ani").click(function(){
    $("h2").animate({width: '0%'},1000,'linear');
  });
});
 

animate에는 다양한 효과 연출방법과 연출 시간을 인자로 지정한다.

이와 관련되서는 추후 링크를 추가.

 

'JAVASCRIPT > JQUERY' 카테고리의 다른 글

slide  (0) 2022.08.28
fadeIn - fadeOut  (0) 2022.08.28
Event  (0) 2022.08.28
Selector  (0) 2022.08.28
Selector  (0) 2022.08.28

 

W3School의 이벤트 메소드 긁어오기

https://www.w3schools.com/jquery/jquery_ref_events.asp

jQuery Event Methods

jQuery Event Methods ❮ Previous Next ❯ jQuery Event Methods Event methods trigger or attach a function to an event handler for the selected elements. The following table lists all the jQuery methods used to handle events. Method / Property Description bind() Deprecated in version 3.0. Use the on() ...

www.w3schools.com

 

마우스 관련
click()
Attaches/Triggers the click event
dblclick()
Attaches/Triggers the double click event
mousedown()
Attaches/Triggers the mousedown event
mouseenter()
Attaches/Triggers the mouseenter event
mouseleave()
Attaches/Triggers the mouseleave event
mousemove()
Attaches/Triggers the mousemove event
mouseout()
Attaches/Triggers the mouseout event
mouseover()
Attaches/Triggers the mouseover event
mouseup()
Attaches/Triggers the mouseup event
hover()
Attaches two event handlers to the hover event
키보드 관련
keydown()
Attaches/Triggers the keydown event
keypress()
Attaches/Triggers the keypress event
keyup()
Attaches/Triggers the keyup event
스크롤 관련
scroll()
Attaches/Triggers the scroll event
좌표 관련
event.pageX
Returns the mouse position relative to the left edge of the document
event.pageY
Returns the mouse position relative to the top edge of the document
포커스 관련
focus()
Attaches/Triggers the focus event
focusin()
Attaches an event handler to the focusin event
focusout()
Attaches an event handler to the focusout event
기타 이벤트 관련
ready()
Specifies a function to execute when the DOM is fully loaded
resize()
Attaches/Triggers the resize event
select()
Attaches/Triggers the select event
submit()
Attaches/Triggers the submit event
on()
Attaches event handlers to elements
event.target
Returns which DOM element triggered the event
event.which
Returns which keyboard key or mouse button was pressed for the event
event.timeStamp
Returns the number of milliseconds since January 1, 1970, when the event is triggered
event.type
Returns which event type was triggered

 

'JAVASCRIPT > JQUERY' 카테고리의 다른 글

slide  (0) 2022.08.28
fadeIn - fadeOut  (0) 2022.08.28
hide - show  (0) 2022.08.28
Selector  (0) 2022.08.28
Selector  (0) 2022.08.28

 

W3School 의 주요 Selector 긁어 옴.

 

https://www.w3schools.com/jquery/jquery_ref_selectors.asp

jQuery Selectors

jQuery Selectors ❮ Previous Next ❯ jQuery Selectors Use our jQuery Selector Tester to demonstrate the different selectors. Selector Example Selects * $("*") All elements # id $("#lastname") The element with id="lastname" . class $(".intro") All elements with class="intro" . class, . class $(".intro,...

www.w3schools.com

 
 
순서 또는 조건
:first
$("p:first")
The first <p> element
:last
$("p:last")
The last <p> element
:even
$("tr:even")
All even <tr> elements
:odd
$("tr:odd")
All odd <tr> elements
:focus
$(":focus")
The element that currently has focus
:empty
$(":empty")
All elements that are empty
:parent
$(":parent")
All elements that are a parent of another element
:hidden
$("p:hidden")
All hidden <p> elements
:visible
$("table:visible")
All visible tables
입력도구
:input
$(":input")
All input elements
:text
$(":text")
All input elements with type="text"
:password
$(":password")
All input elements with type="password"
:radio
$(":radio")
All input elements with type="radio"
:checkbox
$(":checkbox")
All input elements with type="checkbox"
:submit
$(":submit")
All input elements with type="submit"
:reset
$(":reset")
All input elements with type="reset"
:button
$(":button")
All input elements with type="button"
:image
$(":image")
All input elements with type="image"
:file
$(":file")
All input elements with type="file"
:enabled
$(":enabled")
All enabled input elements
:disabled
$(":disabled")
All disabled input elements
:selected
$(":selected")
All selected input elements
:checked
$(":checked")
All checked input elements
부모, 자식 관계
parent > child
$("div > p")
All <p> elements that are a direct child of a <div> element
parent descendant
$("div p")
All <p> elements that are descendants of a <div> element
element + next
$("div + p")
The <p> element that are next to each <div> elements
element ~ siblings
$("div ~ p")
All <p> elements that appear after the <div> element

 

 

 

'JAVASCRIPT > JQUERY' 카테고리의 다른 글

slide  (0) 2022.08.28
fadeIn - fadeOut  (0) 2022.08.28
hide - show  (0) 2022.08.28
Event  (0) 2022.08.28
Selector  (0) 2022.08.28

+ Recent posts