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

· 11년 전 · 3220
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,176
11년 전 조회 3,548
11년 전 조회 2,761
11년 전 조회 3,304
11년 전 조회 810
11년 전 조회 2,578
11년 전 조회 2,549
11년 전 조회 2,625
11년 전 조회 1,619
11년 전 조회 2,080
11년 전 조회 1,415
11년 전 조회 1,213
11년 전 조회 1,814
11년 전 조회 1,123
11년 전 조회 4,009
11년 전 조회 3,791
11년 전 조회 1,412
11년 전 조회 2,656
11년 전 조회 1,072
11년 전 조회 1,882
11년 전 조회 3,480
11년 전 조회 3,788
11년 전 조회 4,708
11년 전 조회 1,102
11년 전 조회 1,661
11년 전 조회 3,069
11년 전 조회 1,243
11년 전 조회 1,250
11년 전 조회 1,843
11년 전 조회 1,123
11년 전 조회 2,391
11년 전 조회 1,887
11년 전 조회 3,971
11년 전 조회 2,417
11년 전 조회 4,681
11년 전 조회 1,461
11년 전 조회 1,309
11년 전 조회 1,958
11년 전 조회 1,926
11년 전 조회 1,507
11년 전 조회 1,140
11년 전 조회 1,785
11년 전 조회 1,162
11년 전 조회 1,271
11년 전 조회 1,475
11년 전 조회 1,288
11년 전 조회 1,043
11년 전 조회 2,234
11년 전 조회 2,047
11년 전 조회 3,221
11년 전 조회 1,192
11년 전 조회 960
11년 전 조회 1,051
11년 전 조회 2,923
11년 전 조회 1,173
11년 전 조회 1,381
11년 전 조회 903
11년 전 조회 1,674
11년 전 조회 1,662
11년 전 조회 1,076
11년 전 조회 1,256
11년 전 조회 939
11년 전 조회 900
11년 전 조회 1,729
11년 전 조회 1,060
11년 전 조회 955
11년 전 조회 1,071
11년 전 조회 1,227
11년 전 조회 914
11년 전 조회 951
11년 전 조회 1,419
11년 전 조회 999
11년 전 조회 1,441
11년 전 조회 997
11년 전 조회 1,108
11년 전 조회 1,171
11년 전 조회 922
11년 전 조회 956
11년 전 조회 1,178
11년 전 조회 2,099
11년 전 조회 961
11년 전 조회 976
11년 전 조회 905
11년 전 조회 1,331
11년 전 조회 977
11년 전 조회 890
11년 전 조회 1,199
11년 전 조회 1,548
11년 전 조회 1,180
11년 전 조회 1,115
11년 전 조회 1,137
11년 전 조회 1,981
11년 전 조회 1,052
11년 전 조회 1,103
11년 전 조회 2,898
11년 전 조회 2,444
11년 전 조회 1,313
11년 전 조회 1,795
11년 전 조회 1,408
11년 전 조회 1,330
🐛 버그신고