컴

offset() 메서드를 이용하여 left, top 값 set, get 하기

· 2014-09-22 (월) 05:52:15 · 6092
offset()은 요소의  left, top값을 get또는 set 할 수 있는 메서드입니다.
먼저 요소의 left,top값을 get하는 방법을 알아 볼게요
<형식>

$(선택자).offset().left
$(선택자).offset().top

 <!DOCTYPE HTML>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0;}
        div{width:100px;height:100px;background:red;position:absolute;left:20px;top:50px;}
    </style>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
        $(function(){
            var offset = $('div').offset();
            $('p').html(offset.left + '/' + offset.top);
        });
    </script>
</head>
<body>
    <p></p>
    <div>컨텐츠</div>
</body>
</html>


set하는 형식은
$(선택자).offset({left:값,top:값})

 <!DOCTYPE HTML>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0;}
        div{width:100px;height:100px;background:red;}
    </style>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
        $(function(){
            $('div').offset({left:30,top:50});
        });
    </script>
</head>
<body>   
    <div>컨텐츠</div>
</body>
</html>

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
jQuery 14-10-17 조회 7,667
jQuery 14-10-17 조회 1.8만
jQuery 14-10-17 조회 8,972
PHP 14-10-17 조회 6,007
PHP 14-10-17 조회 5,217
jQuery 14-10-13 조회 3,955
JavaScript 14-10-13 조회 4,032
jQuery 14-10-13 조회 4,284
기타 14-10-10 조회 8,263
기타 14-10-10 조회 4,316
기타 14-10-10 조회 4,746
기타 14-10-10 조회 3,343
기타 14-10-10 조회 3,739
jQuery 14-10-07 조회 9,216
jQuery 14-09-24 조회 3,482
PHP 14-09-23 조회 3,614
jQuery 14-09-23 조회 4,151
PHP 14-09-22 조회 3,427
jQuery 14-09-22 조회 4,960
jQuery 14-09-22 조회 6,093
jQuery 14-09-22 조회 3,618
jQuery 14-09-22 조회 3,511
PHP 14-09-19 조회 3,931
PHP 14-09-19 조회 3,366
jQuery 14-09-19 조회 4,003
PHP 14-09-19 조회 3,765
PHP 14-09-18 조회 4,615
jQuery 14-09-18 조회 1.2만
jQuery 14-09-18 조회 8,846
jQuery 14-09-18 조회 3,548