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

· 11년 전 · 4154
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년 전 조회 2,053
11년 전 조회 4,441
11년 전 조회 3,690
11년 전 조회 4,193
11년 전 조회 1,677
11년 전 조회 3,487
11년 전 조회 3,493
11년 전 조회 3,561
11년 전 조회 2,541
11년 전 조회 3,004
11년 전 조회 2,361
11년 전 조회 2,141
11년 전 조회 2,739
11년 전 조회 2,050
11년 전 조회 4,916
11년 전 조회 4,565
11년 전 조회 2,348
11년 전 조회 3,589
11년 전 조회 2,014
11년 전 조회 2,769
11년 전 조회 4,396
11년 전 조회 4,696
11년 전 조회 5,608
11년 전 조회 1,965
11년 전 조회 2,581
11년 전 조회 4,002
11년 전 조회 2,146
11년 전 조회 2,157
11년 전 조회 2,741
11년 전 조회 2,053
11년 전 조회 3,281
11년 전 조회 2,805
11년 전 조회 4,869
11년 전 조회 3,310
11년 전 조회 5,580
11년 전 조회 2,375
11년 전 조회 2,259
11년 전 조회 2,927
11년 전 조회 2,878
11년 전 조회 2,432
11년 전 조회 2,055
11년 전 조회 2,695
11년 전 조회 2,138
11년 전 조회 2,215
11년 전 조회 2,406
11년 전 조회 2,248
11년 전 조회 1,978
11년 전 조회 3,152
11년 전 조회 3,023
11년 전 조회 4,155
11년 전 조회 2,193
11년 전 조회 1,928
11년 전 조회 2,007
11년 전 조회 3,894
11년 전 조회 2,130
11년 전 조회 2,353
11년 전 조회 1,879
11년 전 조회 2,598
11년 전 조회 2,599
11년 전 조회 2,000
11년 전 조회 2,175
11년 전 조회 1,897
11년 전 조회 1,829
11년 전 조회 2,635
11년 전 조회 1,981
11년 전 조회 1,901
11년 전 조회 2,066
11년 전 조회 2,132
11년 전 조회 1,830
11년 전 조회 1,931
11년 전 조회 2,343
11년 전 조회 1,960
11년 전 조회 2,340
11년 전 조회 1,972
11년 전 조회 2,039
11년 전 조회 2,139
11년 전 조회 1,902
11년 전 조회 1,922
11년 전 조회 2,115
11년 전 조회 3,025
11년 전 조회 1,942
11년 전 조회 1,956
11년 전 조회 1,870
11년 전 조회 2,310
11년 전 조회 1,940
11년 전 조회 1,857
11년 전 조회 2,211
11년 전 조회 2,512
11년 전 조회 2,183
11년 전 조회 2,091
11년 전 조회 2,138
11년 전 조회 2,999
11년 전 조회 2,023
11년 전 조회 2,100
11년 전 조회 3,704
11년 전 조회 3,428
11년 전 조회 2,318
11년 전 조회 2,825
11년 전 조회 2,350
11년 전 조회 2,300