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

· 11년 전 · 4525
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,438
11년 전 조회 4,799
11년 전 조회 4,065
11년 전 조회 4,547
11년 전 조회 2,099
11년 전 조회 3,876
11년 전 조회 3,854
11년 전 조회 3,922
11년 전 조회 2,934
11년 전 조회 3,382
11년 전 조회 2,723
11년 전 조회 2,507
11년 전 조회 3,140
11년 전 조회 2,446
11년 전 조회 5,315
11년 전 조회 4,910
11년 전 조회 2,719
11년 전 조회 3,930
11년 전 조회 2,368
11년 전 조회 3,128
11년 전 조회 4,775
11년 전 조회 5,094
11년 전 조회 6,006
11년 전 조회 2,370
11년 전 조회 2,945
11년 전 조회 4,371
11년 전 조회 2,542
11년 전 조회 2,539
11년 전 조회 3,120
11년 전 조회 2,424
11년 전 조회 3,666
11년 전 조회 3,179
11년 전 조회 5,274
11년 전 조회 3,677
11년 전 조회 5,966
11년 전 조회 2,789
11년 전 조회 2,594
11년 전 조회 3,281
11년 전 조회 3,258
11년 전 조회 2,797
11년 전 조회 2,435
11년 전 조회 3,082
11년 전 조회 2,484
11년 전 조회 2,576
11년 전 조회 2,801
11년 전 조회 2,625
11년 전 조회 2,339
11년 전 조회 3,570
11년 전 조회 3,356
11년 전 조회 4,526
11년 전 조회 2,534
11년 전 조회 2,291
11년 전 조회 2,381
11년 전 조회 4,269
11년 전 조회 2,478
11년 전 조회 2,732
11년 전 조회 2,255
11년 전 조회 2,958
11년 전 조회 2,967
11년 전 조회 2,382
11년 전 조회 2,589
11년 전 조회 2,261
11년 전 조회 2,205
11년 전 조회 2,989
11년 전 조회 2,371
11년 전 조회 2,265
11년 전 조회 2,421
11년 전 조회 2,481
11년 전 조회 2,215
11년 전 조회 2,278
11년 전 조회 2,697
11년 전 조회 2,370
11년 전 조회 2,687
11년 전 조회 2,374
11년 전 조회 2,384
11년 전 조회 2,511
11년 전 조회 2,271
11년 전 조회 2,319
11년 전 조회 2,477
11년 전 조회 3,373
11년 전 조회 2,328
11년 전 조회 2,331
11년 전 조회 2,209
11년 전 조회 2,675
11년 전 조회 2,339
11년 전 조회 2,223
11년 전 조회 2,572
11년 전 조회 2,867
11년 전 조회 2,552
11년 전 조회 2,461
11년 전 조회 2,484
11년 전 조회 3,351
11년 전 조회 2,386
11년 전 조회 2,455
11년 전 조회 4,050
11년 전 조회 3,791
11년 전 조회 2,669
11년 전 조회 3,195
11년 전 조회 2,719
11년 전 조회 2,648