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

· 11년 전 · 3106
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,065
11년 전 조회 3,442
11년 전 조회 2,652
11년 전 조회 3,199
11년 전 조회 698
11년 전 조회 2,473
11년 전 조회 2,446
11년 전 조회 2,514
11년 전 조회 1,502
11년 전 조회 1,983
11년 전 조회 1,307
11년 전 조회 1,109
11년 전 조회 1,693
11년 전 조회 1,011
11년 전 조회 3,898
11년 전 조회 3,676
11년 전 조회 1,308
11년 전 조회 2,540
11년 전 조회 957
11년 전 조회 1,766
11년 전 조회 3,369
11년 전 조회 3,677
11년 전 조회 4,596
11년 전 조회 994
11년 전 조회 1,553
11년 전 조회 2,957
11년 전 조회 1,134
11년 전 조회 1,127
11년 전 조회 1,740
11년 전 조회 997
11년 전 조회 2,276
11년 전 조회 1,774
11년 전 조회 3,863
11년 전 조회 2,313
11년 전 조회 4,558
11년 전 조회 1,336
11년 전 조회 1,204
11년 전 조회 1,843
11년 전 조회 1,810
11년 전 조회 1,380
11년 전 조회 1,026
11년 전 조회 1,670
11년 전 조회 1,047
11년 전 조회 1,150
11년 전 조회 1,356
11년 전 조회 1,179
11년 전 조회 929
11년 전 조회 2,114
11년 전 조회 1,938
11년 전 조회 3,107
11년 전 조회 1,080
11년 전 조회 836
11년 전 조회 936
11년 전 조회 2,811
11년 전 조회 1,061
11년 전 조회 1,255
11년 전 조회 781
11년 전 조회 1,561
11년 전 조회 1,542
11년 전 조회 954
11년 전 조회 1,136
11년 전 조회 820
11년 전 조회 780
11년 전 조회 1,605
11년 전 조회 940
11년 전 조회 835
11년 전 조회 956
11년 전 조회 1,119
11년 전 조회 789
11년 전 조회 833
11년 전 조회 1,317
11년 전 조회 874
11년 전 조회 1,341
11년 전 조회 860
11년 전 조회 1,006
11년 전 조회 1,060
11년 전 조회 802
11년 전 조회 830
11년 전 조회 1,058
11년 전 조회 1,995
11년 전 조회 841
11년 전 조회 850
11년 전 조회 784
11년 전 조회 1,205
11년 전 조회 852
11년 전 조회 770
11년 전 조회 1,077
11년 전 조회 1,423
11년 전 조회 1,055
11년 전 조회 976
11년 전 조회 1,018
11년 전 조회 1,857
11년 전 조회 923
11년 전 조회 981
11년 전 조회 2,757
11년 전 조회 2,303
11년 전 조회 1,178
11년 전 조회 1,670
11년 전 조회 1,283
11년 전 조회 1,218
🐛 버그신고