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

· 11년 전 · 3138
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,098
11년 전 조회 3,471
11년 전 조회 2,679
11년 전 조회 3,232
11년 전 조회 733
11년 전 조회 2,503
11년 전 조회 2,474
11년 전 조회 2,554
11년 전 조회 1,530
11년 전 조회 2,009
11년 전 조회 1,337
11년 전 조회 1,140
11년 전 조회 1,722
11년 전 조회 1,044
11년 전 조회 3,931
11년 전 조회 3,701
11년 전 조회 1,337
11년 전 조회 2,572
11년 전 조회 994
11년 전 조회 1,801
11년 전 조회 3,410
11년 전 조회 3,709
11년 전 조회 4,632
11년 전 조회 1,025
11년 전 조회 1,585
11년 전 조회 2,990
11년 전 조회 1,173
11년 전 조회 1,163
11년 전 조회 1,773
11년 전 조회 1,034
11년 전 조회 2,306
11년 전 조회 1,814
11년 전 조회 3,897
11년 전 조회 2,349
11년 전 조회 4,597
11년 전 조회 1,370
11년 전 조회 1,237
11년 전 조회 1,876
11년 전 조회 1,844
11년 전 조회 1,418
11년 전 조회 1,068
11년 전 조회 1,698
11년 전 조회 1,083
11년 전 조회 1,185
11년 전 조회 1,396
11년 전 조회 1,216
11년 전 조회 965
11년 전 조회 2,146
11년 전 조회 1,969
11년 전 조회 3,139
11년 전 조회 1,106
11년 전 조회 879
11년 전 조회 971
11년 전 조회 2,844
11년 전 조회 1,088
11년 전 조회 1,290
11년 전 조회 816
11년 전 조회 1,589
11년 전 조회 1,582
11년 전 조회 992
11년 전 조회 1,169
11년 전 조회 855
11년 전 조회 810
11년 전 조회 1,635
11년 전 조회 969
11년 전 조회 875
11년 전 조회 986
11년 전 조회 1,162
11년 전 조회 821
11년 전 조회 863
11년 전 조회 1,348
11년 전 조회 911
11년 전 조회 1,385
11년 전 조회 896
11년 전 조회 1,032
11년 전 조회 1,086
11년 전 조회 836
11년 전 조회 868
11년 전 조회 1,089
11년 전 조회 2,024
11년 전 조회 865
11년 전 조회 890
11년 전 조회 820
11년 전 조회 1,238
11년 전 조회 894
11년 전 조회 798
11년 전 조회 1,113
11년 전 조회 1,451
11년 전 조회 1,089
11년 전 조회 1,012
11년 전 조회 1,062
11년 전 조회 1,892
11년 전 조회 953
11년 전 조회 1,014
11년 전 조회 2,791
11년 전 조회 2,346
11년 전 조회 1,219
11년 전 조회 1,708
11년 전 조회 1,315
11년 전 조회 1,242
🐛 버그신고