본문 바로가기
프로그래밍/jQuery

fadeTo(), 투명도를 조절하기

by zoo10 2012. 4. 16.

fadeTo

원문 링크 http://api.jquery.com/fadeTo/

.fadeTo( duration, opacity [, callback] )Returns : jQuery

개요 : 투명도를 지정하여 페이드를 조절합니다.

  • .fadeOut( [duration] [, callback] )
  • duration 시간 값
  • opacity 0 ~ 1 사이의 투명도 값
  • callback 콜백 함수
  • .fadeTo( duration, opacity [, easing] [, callback] )
  • duration 시간 값
  • opacity 0 ~ 1 사이의 투명도 값
  • easing 특수한 효과 함수
  • callback 콜백 함수

.fadeTo() 함수는 투명도를 조절하여 움직임을 만들어 내는 함수입니다.

Durations 의 단위는 밀리세컨드(천분의 일초)입니다. 큰 값을 주면 느린 속도로 움직이고, 반대는 빨라집니다. 'fast''slow' 문자를 사용할 수 있으며 각각은 200600 밀리세커드를 의미합니다. 만약 이 인자를 생략하면 기본값인 400 밀리세컨드로 움직이게 됩니다. 다른 효과 함수들과는 조금 다르게, .fadeTo() 함수는 반드시 시간(duration)을 명시해 주어야 합니다.

이미지를 나타나게 하는 간단한 예제를 보시죠.

<div id="clickme">
    Click here
  </div>
  <img id="book" src="book.png" alt="" width="100" height="123" />
  With the element initially shown, we can dim it slowly:
  $('#clickme').click(function() {
    $('#book').fadeTo('slow', 0.5, function() {
      // Animation complete.
    });
  });

duration 값을 0 으로 하면, 이 함수는 opacity CSS 속성을 바꿔줍니다. 그래서 .fadeTo(0, opacity).css('opacity', opacity) 코드와 같은 효과를 나타냅니다.

덧붙임 ( Additional Notes )

.fadeIn() 를 포함하여 모든 jQuery effect 들은, 글로벌 세팅인 jQuery.fx.off = true로 조절할 수 있습니다. 더 많은 정보를 원하시면 jQuery.fx.off를 참고하십시오.

헉!! fadeIn + fadeOut 이랑 아주 비슷하네요. 요번에두 거저 먹었어요. ㅎㅎㅎ

예 제  
첫번째 p 요소만 투명도(opacity)를 0.33 (33%) 만큼 처리합니다. 600 milliseconds 가 소요됩니다..

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>
Click this paragraph to see it fade.
</p>

<p>
Compare to this one that won't fade.
</p>
<script>
$("p:first").click(function () {
$(this).fadeTo("slow", 0.33);
});
</script>

</body>
</html>

미리보기

친절하게 위아래 비교해보라네요. ^^

 

예 제  
클릭 때마다 랜덤하게 투명도를 조절합니다. 200 milliseconds 를 기준으로 합니다..

<!DOCTYPE html>
<html>
<head>
  <style>
p { width:80px; margin:0; padding:5px; }
div { width:40px; height:40px; position:absolute; }
div#one { top:0; left:0; background:#f00; }
div#two { top:20px; left:20px; background:#0f0; }
div#three { top:40px; left:40px; background:#00f; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>And this is the library that John built...</p>

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<script>
$("div").click(function () {
$(this).fadeTo("fast", Math.random());
});
</script>

</body>
</html>

미리보기

박스들이 겹쳐 있어서 클릭을 잘 해야 합니다. 뭐 예제를 위한 예제입니다.

 

예 제  
맞는 답을 찾아라!! 클릭하면 답을 보여줍니다.

<!DOCTYPE html>
<html>
<head>
  <style>
div, p { width:80px; height:40px; top:0; margin:0; 
position:absolute; padding-top:8px; }
p { background:#fcc; text-align:center; }
div { background:blue; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Wrong</p>
<div></div>
<p>Wrong</p>
<div></div>

<p>Right!</p>
<div></div>
<script>
var getPos = function (n) {
return (Math.floor(n) * 90) + "px";
};
$("p").each(function (n) {
var r = Math.floor(Math.random() * 3);
var tmp = $(this).text();
$(this).text($("p:eq(" + r + ")").text());
$("p:eq(" + r + ")").text(tmp);
$(this).css("left", getPos(n));
});
$("div").each(function (n) {
      $(this).css("left", getPos(n));
    })
.css("cursor", "pointer")
.click(function () {
      $(this).fadeTo(250, 0.25, function () {
            $(this).css("cursor", "")
                   .prev().css({"font-weight": "bolder",
                                "font-style": "italic"});
          });
    });

</script>

</body>
</html>

미리보기

이 방식 그대로 실제 시험문제 관련 사이트에 사용하시면 안됩니다. ㅎㅎ 답이 다 노출되잖아요.

 

특정 시점까지 페이드 효과를 주고 싶으면 이 함수를 사용해야 겠네요.

그럼 즐프하세요.

※ 본 예제는 http://www.jquery.com 에 있는 내용임을 밝힙니다.