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

· 11년 전 · 3912
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,751
11년 전 조회 4,125
11년 전 조회 3,383
11년 전 조회 3,860
11년 전 조회 1,375
11년 전 조회 3,181
11년 전 조회 3,171
11년 전 조회 3,232
11년 전 조회 2,222
11년 전 조회 2,689
11년 전 조회 2,009
11년 전 조회 1,806
11년 전 조회 2,418
11년 전 조회 1,737
11년 전 조회 4,601
11년 전 조회 4,472
11년 전 조회 2,040
11년 전 조회 3,262
11년 전 조회 1,663
11년 전 조회 2,475
11년 전 조회 4,075
11년 전 조회 4,393
11년 전 조회 5,292
11년 전 조회 1,665
11년 전 조회 2,239
11년 전 조회 3,637
11년 전 조회 1,832
11년 전 조회 1,843
11년 전 조회 2,409
11년 전 조회 1,717
11년 전 조회 2,938
11년 전 조회 2,461
11년 전 조회 4,529
11년 전 조회 2,972
11년 전 조회 5,223
11년 전 조회 2,033
11년 전 조회 1,926
11년 전 조회 2,575
11년 전 조회 2,540
11년 전 조회 2,103
11년 전 조회 1,701
11년 전 조회 2,373
11년 전 조회 1,882
11년 전 조회 1,867
11년 전 조회 2,065
11년 전 조회 1,989
11년 전 조회 1,612
11년 전 조회 2,810
11년 전 조회 2,787
11년 전 조회 3,913
11년 전 조회 1,930
11년 전 조회 1,677
11년 전 조회 1,664
11년 전 조회 3,612
11년 전 조회 1,792
11년 전 조회 2,122
11년 전 조회 1,651
11년 전 조회 2,287
11년 전 조회 2,259
11년 전 조회 1,664
11년 전 조회 1,850
11년 전 조회 1,690
11년 전 조회 1,648
11년 전 조회 2,321
11년 전 조회 1,766
11년 전 조회 1,686
11년 전 조회 1,838
11년 전 조회 1,811
11년 전 조회 1,516
11년 전 조회 1,705
11년 전 조회 2,094
11년 전 조회 1,764
11년 전 조회 1,999
11년 전 조회 1,754
11년 전 조회 1,689
11년 전 조회 1,936
11년 전 조회 1,683
11년 전 조회 1,690
11년 전 조회 1,761
11년 전 조회 2,664
11년 전 조회 1,724
11년 전 조회 1,728
11년 전 조회 1,517
11년 전 조회 1,977
11년 전 조회 1,718
11년 전 조회 1,525
11년 전 조회 1,970
11년 전 조회 2,185
11년 전 조회 1,960
11년 전 조회 1,785
11년 전 조회 1,910
11년 전 조회 2,784
11년 전 조회 1,677
11년 전 조회 1,748
11년 전 조회 3,621
11년 전 조회 3,207
11년 전 조회 2,070
11년 전 조회 2,588
11년 전 조회 2,026
11년 전 조회 1,962