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

· 11년 전 · 4545
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,455
11년 전 조회 4,815
11년 전 조회 4,080
11년 전 조회 4,562
11년 전 조회 2,116
11년 전 조회 3,902
11년 전 조회 3,873
11년 전 조회 3,940
11년 전 조회 2,956
11년 전 조회 3,397
11년 전 조회 2,740
11년 전 조회 2,520
11년 전 조회 3,163
11년 전 조회 2,465
11년 전 조회 5,327
11년 전 조회 4,916
11년 전 조회 2,731
11년 전 조회 3,946
11년 전 조회 2,381
11년 전 조회 3,142
11년 전 조회 4,788
11년 전 조회 5,110
11년 전 조회 6,019
11년 전 조회 2,387
11년 전 조회 2,959
11년 전 조회 4,385
11년 전 조회 2,559
11년 전 조회 2,553
11년 전 조회 3,139
11년 전 조회 2,453
11년 전 조회 3,684
11년 전 조회 3,198
11년 전 조회 5,291
11년 전 조회 3,696
11년 전 조회 5,987
11년 전 조회 2,810
11년 전 조회 2,611
11년 전 조회 3,295
11년 전 조회 3,270
11년 전 조회 2,806
11년 전 조회 2,455
11년 전 조회 3,106
11년 전 조회 2,502
11년 전 조회 2,596
11년 전 조회 2,819
11년 전 조회 2,639
11년 전 조회 2,353
11년 전 조회 3,581
11년 전 조회 3,368
11년 전 조회 4,546
11년 전 조회 2,550
11년 전 조회 2,301
11년 전 조회 2,401
11년 전 조회 4,288
11년 전 조회 2,494
11년 전 조회 2,745
11년 전 조회 2,282
11년 전 조회 2,976
11년 전 조회 2,988
11년 전 조회 2,393
11년 전 조회 2,604
11년 전 조회 2,280
11년 전 조회 2,226
11년 전 조회 3,002
11년 전 조회 2,391
11년 전 조회 2,286
11년 전 조회 2,440
11년 전 조회 2,497
11년 전 조회 2,228
11년 전 조회 2,293
11년 전 조회 2,710
11년 전 조회 2,389
11년 전 조회 2,710
11년 전 조회 2,393
11년 전 조회 2,403
11년 전 조회 2,528
11년 전 조회 2,285
11년 전 조회 2,336
11년 전 조회 2,495
11년 전 조회 3,391
11년 전 조회 2,350
11년 전 조회 2,354
11년 전 조회 2,235
11년 전 조회 2,697
11년 전 조회 2,356
11년 전 조회 2,233
11년 전 조회 2,588
11년 전 조회 2,890
11년 전 조회 2,566
11년 전 조회 2,484
11년 전 조회 2,497
11년 전 조회 3,365
11년 전 조회 2,407
11년 전 조회 2,475
11년 전 조회 4,053
11년 전 조회 3,809
11년 전 조회 2,681
11년 전 조회 3,207
11년 전 조회 2,737
11년 전 조회 2,671