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

· 11년 전 · 3047
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,007
11년 전 조회 3,381
11년 전 조회 2,585
11년 전 조회 3,152
11년 전 조회 637
11년 전 조회 2,403
11년 전 조회 2,384
11년 전 조회 2,460
11년 전 조회 1,441
11년 전 조회 1,915
11년 전 조회 1,242
11년 전 조회 1,055
11년 전 조회 1,634
11년 전 조회 958
11년 전 조회 3,836
11년 전 조회 3,613
11년 전 조회 1,241
11년 전 조회 2,483
11년 전 조회 907
11년 전 조회 1,706
11년 전 조회 3,314
11년 전 조회 3,614
11년 전 조회 4,540
11년 전 조회 937
11년 전 조회 1,502
11년 전 조회 2,909
11년 전 조회 1,080
11년 전 조회 1,070
11년 전 조회 1,681
11년 전 조회 939
11년 전 조회 2,216
11년 전 조회 1,720
11년 전 조회 3,799
11년 전 조회 2,262
11년 전 조회 4,513
11년 전 조회 1,286
11년 전 조회 1,146
11년 전 조회 1,785
11년 전 조회 1,751
11년 전 조회 1,324
11년 전 조회 972
11년 전 조회 1,624
11년 전 조회 996
11년 전 조회 1,091
11년 전 조회 1,305
11년 전 조회 1,122
11년 전 조회 878
11년 전 조회 2,060
11년 전 조회 1,874
11년 전 조회 3,048
11년 전 조회 1,023
11년 전 조회 788
11년 전 조회 879
11년 전 조회 2,760
11년 전 조회 1,012
11년 전 조회 1,207
11년 전 조회 726
11년 전 조회 1,505
11년 전 조회 1,499
11년 전 조회 907
11년 전 조회 1,091
11년 전 조회 770
11년 전 조회 733
11년 전 조회 1,556
11년 전 조회 894
11년 전 조회 791
11년 전 조회 906
11년 전 조회 1,089
11년 전 조회 740
11년 전 조회 786
11년 전 조회 1,288
11년 전 조회 829
11년 전 조회 1,312
11년 전 조회 828
11년 전 조회 969
11년 전 조회 1,012
11년 전 조회 758
11년 전 조회 785
11년 전 조회 1,020
11년 전 조회 1,955
11년 전 조회 792
11년 전 조회 803
11년 전 조회 742
11년 전 조회 1,161
11년 전 조회 803
11년 전 조회 724
11년 전 조회 1,044
11년 전 조회 1,380
11년 전 조회 1,008
11년 전 조회 935
11년 전 조회 972
11년 전 조회 1,804
11년 전 조회 875
11년 전 조회 935
11년 전 조회 2,714
11년 전 조회 2,265
11년 전 조회 1,140
11년 전 조회 1,635
11년 전 조회 1,238
11년 전 조회 1,169
🐛 버그신고