코드랩 레퍼런스

.fadeTo() 본문

JQUERY

.fadeTo()

webstoryboy 2018. 1. 28. 13:14

Definition

  • .fadeTo() 메서드는 선택한 요소 투명도를 조절합니다.

Syntax

$("selector").fadeTo()

Compatibility

크롬 아이콘 파이어폭스 아이콘 사파리 아이콘 오페라 아이콘 안드로이드 아이콘 ios 아이콘 익스플로러6 아이콘 6 익스플로러7 아이콘 7 익스플로러8 아이콘 8 익스플로러9 아이콘 9 익스플로러10 아이콘 10 익스플로러11 아이콘 11 엣지 아이콘
.fadeTo()

Sample1 클릭하면 이미지가 나타나는 예제입니다.

효과(Effect)

  • 1
  • 2
  • 3
  • 4
  • 5
<!DOCTYPE html>
<html lang="ko">
	<head>
		<meta charset="UTF-8">
		<title>제이쿼리(jQuery)</title>
		<style>
			.list {overflow: hidden;}
			.list li {float: left; width: 19%; margin:0.5%; min-height: 150px; list-style: none; position: relative;}
			.list li img {width: 100%;}
			.list li em {display:block; width: 20px; height: 20px; background-color: #ec407a; color: #fff; font-size: 12px; position: absolute; left: 5px; top:5px; border-radius: 50%; text-align: center; line-height: 22px;}
		</style>
	</head>
	<body>
		<h3 class="h3">효과(Effect)</h3>
		<div class="list">
			<ul>
				<li class="one1"><em>1</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img01.jpg" width="200"></li>
				<li class="one2"><em>2</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img02.jpg" width="200"></li>
				<li class="one3"><em>3</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img03.jpg" width="200"></li>
				<li class="one4"><em>4</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img04.jpg" width="200"></li>
				<li class="one5"><em>5</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img05.jpg" width="200"></li>
			</ul>
		</div>

		<div class="btn">
			<ul>
				<li><input type="radio" name="radio" id="ex1" value="1"><label for="ex1">$(".list .one1 img").fadeTo("slow",0.1);</label></li> 
				<li><input type="radio" name="radio" id="ex2" value="2"><label for="ex2">$(".list .one2 img").fadeTo("normal",0.2);</label></li>
				<li><input type="radio" name="radio" id="ex3" value="3"><label for="ex3">$(".list .one3 img").fadeTo("fast",0.3);</label></li>
				<li><input type="radio" name="radio" id="ex4" value="4"><label for="ex4">$(".list .one4 img").fadeTo(1000,0.4);</label></li>
				<li><input type="radio" name="radio" id="ex5" value="5"><label for="ex5">$(".list .one5 img").fadeTo(3000,0.4);</label></li>
				<li><input type="radio" name="radio" id="ex6" value="6"><label for="ex6">$(".list img").fadeTo("slow",1);</label></li>
			</ul>
		</div>

		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){    
			var btn = $(".btn > ul > li > input");	
			
			btn.click(function(){
				var num = $(this).attr("value");
				$("*").removeClass("on");
				
				if(num == 1){$(".list .one1 img").fadeTo("slow",0.1);}
				if(num == 2){$(".list .one2 img").fadeTo("normal",0.2);}
				if(num == 3){$(".list .one3 img").fadeTo("fast",0.3);}
				if(num == 4){$(".list .one4 img").fadeTo(1000,0.4);}
				if(num == 5){$(".list .one5 img").fadeTo(3000,0.4);}
				if(num == 6){$(".list img").fadeTo("slow",1);}
			});
		}); 
		</script> 
	</body>
</html>         

Sample2 클릭하면 이미지가 순차적으로 투명해지는 예제입니다.

효과(Effect)

  • 1
  • 2
  • 3
  • 4
  • 5
<!DOCTYPE html>
<html lang="ko">
	<head>
		<meta charset="UTF-8">
		<title>제이쿼리(jQuery)</title>
		<style>
			.list3 {overflow: hidden; min-height: 150px;}
			.list3 li {float: left; width: 19%; margin:0.5%; list-style: none; position: relative; }
			.list3 li img {width: 100%; }
			.list3 li em {display:block; width: 20px; height: 20px; background-color: #ec407a; color: #fff; font-size: 12px; position: absolute; left: 5px; top:5px; border-radius: 50%; text-align: center; line-height: 22px;}
			.btn3 {border:1px solid #F06950; padding: 7px 20px 5px 20px; display: inline-block; border-radius: 20px; text-decoration: none;}
		</style>
	</head>
	<body>
		<h3 class="h3">효과(Effect)</h3>
		<div class="list3">
			<ul>
				<li class="one1"><em>1</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img01.jpg" width="200"></li>
				<li class="one2"><em>2</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img02.jpg" width="200"></li>
				<li class="one3"><em>3</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img03.jpg" width="200"></li>
				<li class="one4"><em>4</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img04.jpg" width="200"></li>
				<li class="one5"><em>5</em><img src="https://tistory1.daumcdn.net/tistory/2141493/skin/images/img05.jpg" width="200"></li>
			</ul>
		</div>

		<div class="btn">
			<a href="#" class="btn3">Click : 클릭하면 이미지가 순차적으로 투명해집니다.</a>
		</div>

		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
		<script type="text/javascript">
		$(document).ready(function(){    
			$( ".btn3" ).click(function(e) {
				e.preventDefault();
				$(".list3 li").first().fadeTo(1000,0.1, function Next() {
					$(this).next("li").fadeTo(1000,0.1, Next );
				});
			});
		}); 
		</script> 
	</body>
</html>          

Share

Ad

Comments