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

· 11년 전 · 4422
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년 전 조회 2,354
11년 전 조회 4,720
11년 전 조회 3,975
11년 전 조회 4,460
11년 전 조회 1,995
11년 전 조회 3,774
11년 전 조회 3,761
11년 전 조회 3,840
11년 전 조회 2,832
11년 전 조회 3,284
11년 전 조회 2,637
11년 전 조회 2,421
11년 전 조회 3,052
11년 전 조회 2,345
11년 전 조회 5,217
11년 전 조회 4,863
11년 전 조회 2,602
11년 전 조회 3,842
11년 전 조회 2,273
11년 전 조회 3,034
11년 전 조회 4,668
11년 전 조회 4,990
11년 전 조회 5,894
11년 전 조회 2,271
11년 전 조회 2,833
11년 전 조회 4,274
11년 전 조회 2,448
11년 전 조회 2,447
11년 전 조회 3,031
11년 전 조회 2,313
11년 전 조회 3,565
11년 전 조회 3,082
11년 전 조회 5,172
11년 전 조회 3,588
11년 전 조회 5,858
11년 전 조회 2,677
11년 전 조회 2,510
11년 전 조회 3,191
11년 전 조회 3,152
11년 전 조회 2,693
11년 전 조회 2,338
11년 전 조회 2,976
11년 전 조회 2,389
11년 전 조회 2,465
11년 전 조회 2,704
11년 전 조회 2,533
11년 전 조회 2,232
11년 전 조회 3,457
11년 전 조회 3,262
11년 전 조회 4,423
11년 전 조회 2,423
11년 전 조회 2,193
11년 전 조회 2,276
11년 전 조회 4,169
11년 전 조회 2,370
11년 전 조회 2,612
11년 전 조회 2,136
11년 전 조회 2,844
11년 전 조회 2,866
11년 전 조회 2,283
11년 전 조회 2,485
11년 전 조회 2,144
11년 전 조회 2,078
11년 전 조회 2,883
11년 전 조회 2,256
11년 전 조회 2,152
11년 전 조회 2,315
11년 전 조회 2,392
11년 전 조회 2,090
11년 전 조회 2,177
11년 전 조회 2,585
11년 전 조회 2,244
11년 전 조회 2,576
11년 전 조회 2,274
11년 전 조회 2,272
11년 전 조회 2,392
11년 전 조회 2,165
11년 전 조회 2,185
11년 전 조회 2,380
11년 전 조회 3,258
11년 전 조회 2,199
11년 전 조회 2,215
11년 전 조회 2,098
11년 전 조회 2,568
11년 전 조회 2,205
11년 전 조회 2,120
11년 전 조회 2,446
11년 전 조회 2,763
11년 전 조회 2,433
11년 전 조회 2,351
11년 전 조회 2,373
11년 전 조회 3,237
11년 전 조회 2,276
11년 전 조회 2,356
11년 전 조회 4,016
11년 전 조회 3,681
11년 전 조회 2,555
11년 전 조회 3,089
11년 전 조회 2,606
11년 전 조회 2,536