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

· 11년 전 · 3075
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,036
11년 전 조회 3,417
11년 전 조회 2,623
11년 전 조회 3,173
11년 전 조회 672
11년 전 조회 2,443
11년 전 조회 2,415
11년 전 조회 2,486
11년 전 조회 1,471
11년 전 조회 1,949
11년 전 조회 1,275
11년 전 조회 1,081
11년 전 조회 1,664
11년 전 조회 986
11년 전 조회 3,867
11년 전 조회 3,647
11년 전 조회 1,273
11년 전 조회 2,518
11년 전 조회 932
11년 전 조회 1,740
11년 전 조회 3,346
11년 전 조회 3,646
11년 전 조회 4,568
11년 전 조회 963
11년 전 조회 1,524
11년 전 조회 2,929
11년 전 조회 1,109
11년 전 조회 1,093
11년 전 조회 1,710
11년 전 조회 965
11년 전 조회 2,247
11년 전 조회 1,748
11년 전 조회 3,826
11년 전 조회 2,281
11년 전 조회 4,531
11년 전 조회 1,311
11년 전 조회 1,174
11년 전 조회 1,815
11년 전 조회 1,780
11년 전 조회 1,353
11년 전 조회 998
11년 전 조회 1,645
11년 전 조회 1,022
11년 전 조회 1,118
11년 전 조회 1,330
11년 전 조회 1,150
11년 전 조회 899
11년 전 조회 2,086
11년 전 조회 1,897
11년 전 조회 3,076
11년 전 조회 1,050
11년 전 조회 809
11년 전 조회 909
11년 전 조회 2,780
11년 전 조회 1,034
11년 전 조회 1,230
11년 전 조회 749
11년 전 조회 1,526
11년 전 조회 1,520
11년 전 조회 926
11년 전 조회 1,115
11년 전 조회 793
11년 전 조회 758
11년 전 조회 1,576
11년 전 조회 914
11년 전 조회 809
11년 전 조회 924
11년 전 조회 1,101
11년 전 조회 760
11년 전 조회 806
11년 전 조회 1,299
11년 전 조회 847
11년 전 조회 1,324
11년 전 조회 841
11년 전 조회 986
11년 전 조회 1,033
11년 전 조회 777
11년 전 조회 800
11년 전 조회 1,039
11년 전 조회 1,972
11년 전 조회 811
11년 전 조회 822
11년 전 조회 759
11년 전 조회 1,185
11년 전 조회 820
11년 전 조회 740
11년 전 조회 1,059
11년 전 조회 1,394
11년 전 조회 1,025
11년 전 조회 953
11년 전 조회 992
11년 전 조회 1,827
11년 전 조회 895
11년 전 조회 953
11년 전 조회 2,731
11년 전 조회 2,282
11년 전 조회 1,154
11년 전 조회 1,648
11년 전 조회 1,257
11년 전 조회 1,188
🐛 버그신고