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

· 2014-09-22 (월) 05:52:15 · 3241
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>

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

팁게시판

6,077건

디자인과 관련된 유용한 정보를 공유하세요. 질문은 상단의 QA에서 해주시기 바랍니다.

+
제목 글쓴이 날짜 조회
15-09-22 조회 2,520
15-06-10 조회 2,367
14-10-25 조회 3,446
14-10-22 조회 5,986
14-09-21 조회 2,918
14-09-16 조회 2,332
14-08-22 조회 7,637
14-08-13 조회 3,041
14-08-12 조회 2,546
14-08-12 조회 3,447
14-08-11 조회 3,838
14-06-12 조회 3,234
14-06-11 조회 2,053
14-05-09 조회 2,430
14-05-09 조회 3,264
14-05-09 조회 3,445
14-05-08 조회 2,794
13-10-30 조회 4,773
13-07-11 조회 2,661
13-03-28 조회 8,646
13-02-27 조회 4,125
13-02-14 조회 3,174
13-01-10 조회 1,903
12-12-31 조회 2,613
12-12-20 조회 3,799