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

· 11년 전 · 3233
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,214
11년 전 조회 3,570
11년 전 조회 2,784
11년 전 조회 3,320
11년 전 조회 848
11년 전 조회 2,605
11년 전 조회 2,569
11년 전 조회 2,641
11년 전 조회 1,641
11년 전 조회 2,112
11년 전 조회 1,442
11년 전 조회 1,226
11년 전 조회 1,843
11년 전 조회 1,162
11년 전 조회 4,029
11년 전 조회 3,817
11년 전 조회 1,442
11년 전 조회 2,678
11년 전 조회 1,104
11년 전 조회 1,898
11년 전 조회 3,501
11년 전 조회 3,810
11년 전 조회 4,731
11년 전 조회 1,139
11년 전 조회 1,673
11년 전 조회 3,082
11년 전 조회 1,275
11년 전 조회 1,274
11년 전 조회 1,865
11년 전 조회 1,160
11년 전 조회 2,409
11년 전 조회 1,902
11년 전 조회 3,981
11년 전 조회 2,436
11년 전 조회 4,695
11년 전 조회 1,498
11년 전 조회 1,334
11년 전 조회 1,981
11년 전 조회 1,950
11년 전 조회 1,530
11년 전 조회 1,161
11년 전 조회 1,819
11년 전 조회 1,194
11년 전 조회 1,290
11년 전 조회 1,511
11년 전 조회 1,322
11년 전 조회 1,059
11년 전 조회 2,270
11년 전 조회 2,067
11년 전 조회 3,234
11년 전 조회 1,215
11년 전 조회 977
11년 전 조회 1,076
11년 전 조회 2,942
11년 전 조회 1,190
11년 전 조회 1,390
11년 전 조회 931
11년 전 조회 1,700
11년 전 조회 1,693
11년 전 조회 1,099
11년 전 조회 1,303
11년 전 조회 961
11년 전 조회 930
11년 전 조회 1,747
11년 전 조회 1,083
11년 전 조회 972
11년 전 조회 1,100
11년 전 조회 1,237
11년 전 조회 944
11년 전 조회 973
11년 전 조회 1,436
11년 전 조회 1,029
11년 전 조회 1,454
11년 전 조회 1,035
11년 전 조회 1,125
11년 전 조회 1,195
11년 전 조회 943
11년 전 조회 968
11년 전 조회 1,197
11년 전 조회 2,110
11년 전 조회 976
11년 전 조회 987
11년 전 조회 922
11년 전 조회 1,373
11년 전 조회 996
11년 전 조회 928
11년 전 조회 1,218
11년 전 조회 1,581
11년 전 조회 1,196
11년 전 조회 1,145
11년 전 조회 1,166
11년 전 조회 2,004
11년 전 조회 1,075
11년 전 조회 1,129
11년 전 조회 2,924
11년 전 조회 2,477
11년 전 조회 1,344
11년 전 조회 1,833
11년 전 조회 1,432
11년 전 조회 1,364
🐛 버그신고