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

· 11년 전 · 3102
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,063
11년 전 조회 3,442
11년 전 조회 2,648
11년 전 조회 3,197
11년 전 조회 695
11년 전 조회 2,469
11년 전 조회 2,443
11년 전 조회 2,512
11년 전 조회 1,499
11년 전 조회 1,977
11년 전 조회 1,301
11년 전 조회 1,105
11년 전 조회 1,689
11년 전 조회 1,007
11년 전 조회 3,893
11년 전 조회 3,670
11년 전 조회 1,303
11년 전 조회 2,539
11년 전 조회 953
11년 전 조회 1,763
11년 전 조회 3,368
11년 전 조회 3,673
11년 전 조회 4,592
11년 전 조회 990
11년 전 조회 1,550
11년 전 조회 2,955
11년 전 조회 1,130
11년 전 조회 1,123
11년 전 조회 1,738
11년 전 조회 994
11년 전 조회 2,272
11년 전 조회 1,772
11년 전 조회 3,858
11년 전 조회 2,310
11년 전 조회 4,556
11년 전 조회 1,333
11년 전 조회 1,199
11년 전 조회 1,838
11년 전 조회 1,805
11년 전 조회 1,377
11년 전 조회 1,021
11년 전 조회 1,666
11년 전 조회 1,045
11년 전 조회 1,147
11년 전 조회 1,354
11년 전 조회 1,174
11년 전 조회 928
11년 전 조회 2,111
11년 전 조회 1,931
11년 전 조회 3,103
11년 전 조회 1,077
11년 전 조회 834
11년 전 조회 932
11년 전 조회 2,807
11년 전 조회 1,058
11년 전 조회 1,253
11년 전 조회 775
11년 전 조회 1,555
11년 전 조회 1,539
11년 전 조회 951
11년 전 조회 1,134
11년 전 조회 816
11년 전 조회 778
11년 전 조회 1,600
11년 전 조회 936
11년 전 조회 832
11년 전 조회 950
11년 전 조회 1,115
11년 전 조회 784
11년 전 조회 829
11년 전 조회 1,314
11년 전 조회 870
11년 전 조회 1,337
11년 전 조회 857
11년 전 조회 1,003
11년 전 조회 1,055
11년 전 조회 797
11년 전 조회 826
11년 전 조회 1,056
11년 전 조회 1,989
11년 전 조회 838
11년 전 조회 848
11년 전 조회 779
11년 전 조회 1,203
11년 전 조회 846
11년 전 조회 766
11년 전 조회 1,075
11년 전 조회 1,419
11년 전 조회 1,052
11년 전 조회 974
11년 전 조회 1,014
11년 전 조회 1,852
11년 전 조회 919
11년 전 조회 979
11년 전 조회 2,752
11년 전 조회 2,300
11년 전 조회 1,174
11년 전 조회 1,666
11년 전 조회 1,279
11년 전 조회 1,211
🐛 버그신고