본문 바로가기

강좌/jQuery

제이쿼리(jQuery) - css() 메서드와 콜백함수(CallBack)

1. css()함수 사용 - 예를 들어서 기사나 본문글 폰트 키우고 줄이는 걸 적용해보자.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

    <head>

<meta http-equiv="content-type" content="text/html;" charset="utf-8" >

        <title> slice method </title>

<script src="http://code.jquery.com/jquery-1.7.2.js"></script>

<style type="text/css">

            .red{color:red;}

</style>

<script>

   $(document).ready(function (){

   $('div.button').click(function(){

   //본문 영역을 변수에 담기

   var $exec = $('div.exec');

//본문 영역의 폰트 사이즈 가져오기

var currentSize = $exec.css('fontSize'); //alert(currentSize);

//px문자열을 제외한 16만 가져오기

var num = parseFloat(currentSize, 10);

//px : 단위가져오기

var unit = currentSize.slice(-2);// 16px -> px 오른쪽에서 2자

//늘리기/줄이기

if(this.id == 'goBig'){

   num *= 1.5;

}else if(this.id == 'goSmall'){

   num /= 1.5;

}

//새롭게 설정된 픽셀값을 레이어 재 설정

$exec.css('fontSize', num + unit);

});

});

</script>

    </head>


    <body>

   <div id="btn">

   <div class="button" id="goBig">bigger</div>

   <div class="button" id="goSmall">smaller</div>

</div>


<div class="exec">

   Hello!. This is a main articles

</div>

    </body>

</html>


2. 콜백(Callback)과 맵(Map)





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
<meta http-equiv="content-type" content="text/html;" charset="utf-8" >
        <title> callback </title>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<style type="text/css">
            .red{color:red;}
</style>
<script>
   $(document).ready(function (){
   $('p:eq(1)')      //[1]두번째 p태그 영역
       .css('background-color', 'yellow') //[2]두번째 영역의 배경색 지정
.click(function() {
   var $thisPara = $(this);  //[3]현재 영역을 변수에 설정
$thisPara.next().slideDown('slow', function(){//[4]두번째의 다음 요소를 슬라이드 다운
   $(this).slideUp('slow');//[5]현재 자신 슬라이드업
});

});
});
</script>
    </head>

    <body>
   <p>첫번째</p>
   <p>두번째</p>
   <p style="display:none;">세번째</p>
   <p>네번째</p>
    </body>
</html>