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

· 11년 전 · 3034
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년 전 조회 988
11년 전 조회 3,373
11년 전 조회 2,570
11년 전 조회 3,145
11년 전 조회 621
11년 전 조회 2,387
11년 전 조회 2,373
11년 전 조회 2,449
11년 전 조회 1,429
11년 전 조회 1,902
11년 전 조회 1,227
11년 전 조회 1,043
11년 전 조회 1,618
11년 전 조회 942
11년 전 조회 3,824
11년 전 조회 3,596
11년 전 조회 1,223
11년 전 조회 2,469
11년 전 조회 888
11년 전 조회 1,695
11년 전 조회 3,300
11년 전 조회 3,605
11년 전 조회 4,527
11년 전 조회 923
11년 전 조회 1,493
11년 전 조회 2,901
11년 전 조회 1,066
11년 전 조회 1,052
11년 전 조회 1,665
11년 전 조회 921
11년 전 조회 2,202
11년 전 조회 1,708
11년 전 조회 3,786
11년 전 조회 2,250
11년 전 조회 4,501
11년 전 조회 1,268
11년 전 조회 1,130
11년 전 조회 1,770
11년 전 조회 1,736
11년 전 조회 1,309
11년 전 조회 956
11년 전 조회 1,606
11년 전 조회 978
11년 전 조회 1,077
11년 전 조회 1,289
11년 전 조회 1,106
11년 전 조회 869
11년 전 조회 2,044
11년 전 조회 1,863
11년 전 조회 3,035
11년 전 조회 1,009
11년 전 조회 775
11년 전 조회 861
11년 전 조회 2,746
11년 전 조회 1,004
11년 전 조회 1,195
11년 전 조회 708
11년 전 조회 1,492
11년 전 조회 1,482
11년 전 조회 893
11년 전 조회 1,074
11년 전 조회 757
11년 전 조회 717
11년 전 조회 1,542
11년 전 조회 876
11년 전 조회 779
11년 전 조회 891
11년 전 조회 1,081
11년 전 조회 726
11년 전 조회 771
11년 전 조회 1,282
11년 전 조회 811
11년 전 조회 1,306
11년 전 조회 809
11년 전 조회 963
11년 전 조회 998
11년 전 조회 743
11년 전 조회 773
11년 전 조회 1,012
11년 전 조회 1,951
11년 전 조회 780
11년 전 조회 792
11년 전 조회 733
11년 전 조회 1,143
11년 전 조회 791
11년 전 조회 707
11년 전 조회 1,034
11년 전 조회 1,363
11년 전 조회 996
11년 전 조회 916
11년 전 조회 962
11년 전 조회 1,790
11년 전 조회 862
11년 전 조회 921
11년 전 조회 2,698
11년 전 조회 2,250
11년 전 조회 1,130
11년 전 조회 1,618
11년 전 조회 1,231
11년 전 조회 1,157
🐛 버그신고