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

· 11년 전 · 3968
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>

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

프로그램

+
제목 글쓴이 날짜 조회
11년 전 조회 1,800
11년 전 조회 4,190
11년 전 조회 3,445
11년 전 조회 3,933
11년 전 조회 1,430
11년 전 조회 3,241
11년 전 조회 3,238
11년 전 조회 3,297
11년 전 조회 2,290
11년 전 조회 2,756
11년 전 조회 2,092
11년 전 조회 1,876
11년 전 조회 2,490
11년 전 조회 1,795
11년 전 조회 4,680
11년 전 조회 4,473
11년 전 조회 2,107
11년 전 조회 3,337
11년 전 조회 1,742
11년 전 조회 2,546
11년 전 조회 4,141
11년 전 조회 4,449
11년 전 조회 5,376
11년 전 조회 1,728
11년 전 조회 2,321
11년 전 조회 3,727
11년 전 조회 1,910
11년 전 조회 1,914
11년 전 조회 2,482
11년 전 조회 1,778
11년 전 조회 3,009
11년 전 조회 2,529
11년 전 조회 4,603
11년 전 조회 3,046
11년 전 조회 5,317
11년 전 조회 2,105
11년 전 조회 1,993
11년 전 조회 2,643
11년 전 조회 2,612
11년 전 조회 2,171
11년 전 조회 1,770
11년 전 조회 2,432
11년 전 조회 1,950
11년 전 조회 1,938
11년 전 조회 2,132
11년 전 조회 2,046
11년 전 조회 1,698
11년 전 조회 2,877
11년 전 조회 2,842
11년 전 조회 3,969
11년 전 조회 1,973
11년 전 조회 1,738
11년 전 조회 1,723
11년 전 조회 3,653
11년 전 조회 1,861
11년 전 조회 2,186
11년 전 조회 1,702
11년 전 조회 2,353
11년 전 조회 2,338
11년 전 조회 1,739
11년 전 조회 1,912
11년 전 조회 1,743
11년 전 조회 1,684
11년 전 조회 2,388
11년 전 조회 1,814
11년 전 조회 1,738
11년 전 조회 1,895
11년 전 조회 1,872
11년 전 조회 1,587
11년 전 조회 1,765
11년 전 조회 2,162
11년 전 조회 1,807
11년 전 조회 2,079
11년 전 조회 1,799
11년 전 조회 1,770
11년 전 조회 1,982
11년 전 조회 1,733
11년 전 조회 1,754
11년 전 조회 1,841
11년 전 조회 2,756
11년 전 조회 1,774
11년 전 조회 1,787
11년 전 조회 1,591
11년 전 조회 2,039
11년 전 조회 1,768
11년 전 조회 1,584
11년 전 조회 2,029
11년 전 조회 2,246
11년 전 조회 2,012
11년 전 조회 1,858
11년 전 조회 1,962
11년 전 조회 2,832
11년 전 조회 1,756
11년 전 조회 1,827
11년 전 조회 3,624
11년 전 조회 3,271
11년 전 조회 2,146
11년 전 조회 2,655
11년 전 조회 2,093
11년 전 조회 2,038