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

· 11년 전 · 4140
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,045
11년 전 조회 4,432
11년 전 조회 3,681
11년 전 조회 4,181
11년 전 조회 1,666
11년 전 조회 3,471
11년 전 조회 3,479
11년 전 조회 3,551
11년 전 조회 2,531
11년 전 조회 2,993
11년 전 조회 2,351
11년 전 조회 2,131
11년 전 조회 2,734
11년 전 조회 2,039
11년 전 조회 4,907
11년 전 조회 4,545
11년 전 조회 2,335
11년 전 조회 3,580
11년 전 조회 2,010
11년 전 조회 2,761
11년 전 조회 4,383
11년 전 조회 4,693
11년 전 조회 5,600
11년 전 조회 1,957
11년 전 조회 2,569
11년 전 조회 3,989
11년 전 조회 2,138
11년 전 조회 2,150
11년 전 조회 2,731
11년 전 조회 2,049
11년 전 조회 3,272
11년 전 조회 2,794
11년 전 조회 4,863
11년 전 조회 3,304
11년 전 조회 5,571
11년 전 조회 2,371
11년 전 조회 2,247
11년 전 조회 2,917
11년 전 조회 2,868
11년 전 조회 2,423
11년 전 조회 2,042
11년 전 조회 2,688
11년 전 조회 2,131
11년 전 조회 2,205
11년 전 조회 2,394
11년 전 조회 2,234
11년 전 조회 1,972
11년 전 조회 3,141
11년 전 조회 3,015
11년 전 조회 4,141
11년 전 조회 2,183
11년 전 조회 1,916
11년 전 조회 2,000
11년 전 조회 3,887
11년 전 조회 2,124
11년 전 조회 2,341
11년 전 조회 1,870
11년 전 조회 2,594
11년 전 조회 2,593
11년 전 조회 1,983
11년 전 조회 2,168
11년 전 조회 1,890
11년 전 조회 1,819
11년 전 조회 2,629
11년 전 조회 1,973
11년 전 조회 1,896
11년 전 조회 2,058
11년 전 조회 2,125
11년 전 조회 1,820
11년 전 조회 1,928
11년 전 조회 2,335
11년 전 조회 1,958
11년 전 조회 2,330
11년 전 조회 1,963
11년 전 조회 2,029
11년 전 조회 2,134
11년 전 조회 1,898
11년 전 조회 1,918
11년 전 조회 2,110
11년 전 조회 3,012
11년 전 조회 1,933
11년 전 조회 1,949
11년 전 조회 1,858
11년 전 조회 2,297
11년 전 조회 1,935
11년 전 조회 1,844
11년 전 조회 2,202
11년 전 조회 2,499
11년 전 조회 2,173
11년 전 조회 2,077
11년 전 조회 2,132
11년 전 조회 2,997
11년 전 조회 2,002
11년 전 조회 2,089
11년 전 조회 3,681
11년 전 조회 3,423
11년 전 조회 2,308
11년 전 조회 2,820
11년 전 조회 2,341
11년 전 조회 2,292