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

· 11년 전 · 4490
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,418
11년 전 조회 4,780
11년 전 조회 4,035
11년 전 조회 4,522
11년 전 조회 2,069
11년 전 조회 3,845
11년 전 조회 3,823
11년 전 조회 3,897
11년 전 조회 2,902
11년 전 조회 3,356
11년 전 조회 2,697
11년 전 조회 2,482
11년 전 조회 3,111
11년 전 조회 2,415
11년 전 조회 5,286
11년 전 조회 4,890
11년 전 조회 2,679
11년 전 조회 3,897
11년 전 조회 2,347
11년 전 조회 3,106
11년 전 조회 4,746
11년 전 조회 5,070
11년 전 조회 5,979
11년 전 조회 2,339
11년 전 조회 2,911
11년 전 조회 4,343
11년 전 조회 2,516
11년 전 조회 2,507
11년 전 조회 3,090
11년 전 조회 2,391
11년 전 조회 3,647
11년 전 조회 3,156
11년 전 조회 5,243
11년 전 조회 3,656
11년 전 조회 5,942
11년 전 조회 2,756
11년 전 조회 2,573
11년 전 조회 3,252
11년 전 조회 3,224
11년 전 조회 2,763
11년 전 조회 2,411
11년 전 조회 3,053
11년 전 조회 2,455
11년 전 조회 2,534
11년 전 조회 2,776
11년 전 조회 2,596
11년 전 조회 2,303
11년 전 조회 3,529
11년 전 조회 3,322
11년 전 조회 4,491
11년 전 조회 2,497
11년 전 조회 2,259
11년 전 조회 2,349
11년 전 조회 4,240
11년 전 조회 2,437
11년 전 조회 2,690
11년 전 조회 2,217
11년 전 조회 2,919
11년 전 조회 2,930
11년 전 조회 2,347
11년 전 조회 2,560
11년 전 조회 2,221
11년 전 조회 2,170
11년 전 조회 2,955
11년 전 조회 2,343
11년 전 조회 2,231
11년 전 조회 2,387
11년 전 조회 2,450
11년 전 조회 2,174
11년 전 조회 2,249
11년 전 조회 2,660
11년 전 조회 2,326
11년 전 조회 2,648
11년 전 조회 2,335
11년 전 조회 2,355
11년 전 조회 2,479
11년 전 조회 2,237
11년 전 조회 2,282
11년 전 조회 2,453
11년 전 조회 3,337
11년 전 조회 2,292
11년 전 조회 2,299
11년 전 조회 2,174
11년 전 조회 2,646
11년 전 조회 2,295
11년 전 조회 2,188
11년 전 조회 2,529
11년 전 조회 2,836
11년 전 조회 2,512
11년 전 조회 2,421
11년 전 조회 2,450
11년 전 조회 3,313
11년 전 조회 2,352
11년 전 조회 2,431
11년 전 조회 4,039
11년 전 조회 3,760
11년 전 조회 2,638
11년 전 조회 3,163
11년 전 조회 2,680
11년 전 조회 2,622