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

· 11년 전 · 3152
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,109
11년 전 조회 3,481
11년 전 조회 2,691
11년 전 조회 3,239
11년 전 조회 741
11년 전 조회 2,508
11년 전 조회 2,484
11년 전 조회 2,561
11년 전 조회 1,543
11년 전 조회 2,021
11년 전 조회 1,342
11년 전 조회 1,146
11년 전 조회 1,731
11년 전 조회 1,054
11년 전 조회 3,944
11년 전 조회 3,711
11년 전 조회 1,347
11년 전 조회 2,585
11년 전 조회 1,000
11년 전 조회 1,812
11년 전 조회 3,418
11년 전 조회 3,721
11년 전 조회 4,642
11년 전 조회 1,034
11년 전 조회 1,597
11년 전 조회 3,002
11년 전 조회 1,179
11년 전 조회 1,174
11년 전 조회 1,783
11년 전 조회 1,050
11년 전 조회 2,315
11년 전 조회 1,826
11년 전 조회 3,906
11년 전 조회 2,357
11년 전 조회 4,607
11년 전 조회 1,384
11년 전 조회 1,248
11년 전 조회 1,890
11년 전 조회 1,856
11년 전 조회 1,428
11년 전 조회 1,080
11년 전 조회 1,709
11년 전 조회 1,095
11년 전 조회 1,198
11년 전 조회 1,407
11년 전 조회 1,224
11년 전 조회 976
11년 전 조회 2,162
11년 전 조회 1,981
11년 전 조회 3,153
11년 전 조회 1,118
11년 전 조회 889
11년 전 조회 985
11년 전 조회 2,858
11년 전 조회 1,097
11년 전 조회 1,304
11년 전 조회 828
11년 전 조회 1,602
11년 전 조회 1,602
11년 전 조회 1,002
11년 전 조회 1,178
11년 전 조회 869
11년 전 조회 823
11년 전 조회 1,651
11년 전 조회 981
11년 전 조회 887
11년 전 조회 1,001
11년 전 조회 1,169
11년 전 조회 838
11년 전 조회 876
11년 전 조회 1,355
11년 전 조회 927
11년 전 조회 1,396
11년 전 조회 915
11년 전 조회 1,042
11년 전 조회 1,097
11년 전 조회 850
11년 전 조회 875
11년 전 조회 1,103
11년 전 조회 2,034
11년 전 조회 879
11년 전 조회 900
11년 전 조회 829
11년 전 조회 1,257
11년 전 조회 905
11년 전 조회 814
11년 전 조회 1,121
11년 전 조회 1,467
11년 전 조회 1,102
11년 전 조회 1,024
11년 전 조회 1,075
11년 전 조회 1,905
11년 전 조회 969
11년 전 조회 1,028
11년 전 조회 2,805
11년 전 조회 2,359
11년 전 조회 1,228
11년 전 조회 1,724
11년 전 조회 1,328
11년 전 조회 1,259
🐛 버그신고