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

· 11년 전 · 4175
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,066
11년 전 조회 4,464
11년 전 조회 3,711
11년 전 조회 4,213
11년 전 조회 1,701
11년 전 조회 3,507
11년 전 조회 3,520
11년 전 조회 3,587
11년 전 조회 2,567
11년 전 조회 3,022
11년 전 조회 2,386
11년 전 조회 2,159
11년 전 조회 2,772
11년 전 조회 2,083
11년 전 조회 4,941
11년 전 조회 4,616
11년 전 조회 2,375
11년 전 조회 3,610
11년 전 조회 2,031
11년 전 조회 2,785
11년 전 조회 4,421
11년 전 조회 4,725
11년 전 조회 5,636
11년 전 조회 2,003
11년 전 조회 2,601
11년 전 조회 4,035
11년 전 조회 2,188
11년 전 조회 2,190
11년 전 조회 2,768
11년 전 조회 2,071
11년 전 조회 3,305
11년 전 조회 2,838
11년 전 조회 4,895
11년 전 조회 3,333
11년 전 조회 5,608
11년 전 조회 2,404
11년 전 조회 2,274
11년 전 조회 2,950
11년 전 조회 2,905
11년 전 조회 2,465
11년 전 조회 2,080
11년 전 조회 2,718
11년 전 조회 2,154
11년 전 조회 2,234
11년 전 조회 2,439
11년 전 조회 2,270
11년 전 조회 2,004
11년 전 조회 3,189
11년 전 조회 3,033
11년 전 조회 4,176
11년 전 조회 2,208
11년 전 조회 1,951
11년 전 조회 2,029
11년 전 조회 3,920
11년 전 조회 2,142
11년 전 조회 2,368
11년 전 조회 1,896
11년 전 조회 2,612
11년 전 조회 2,624
11년 전 조회 2,022
11년 전 조회 2,193
11년 전 조회 1,919
11년 전 조회 1,852
11년 전 조회 2,660
11년 전 조회 1,995
11년 전 조회 1,920
11년 전 조회 2,077
11년 전 조회 2,158
11년 전 조회 1,862
11년 전 조회 1,952
11년 전 조회 2,362
11년 전 조회 1,977
11년 전 조회 2,359
11년 전 조회 1,999
11년 전 조회 2,058
11년 전 조회 2,151
11년 전 조회 1,918
11년 전 조회 1,942
11년 전 조회 2,143
11년 전 조회 3,054
11년 전 조회 1,957
11년 전 조회 1,977
11년 전 조회 1,893
11년 전 조회 2,326
11년 전 조회 1,964
11년 전 조회 1,879
11년 전 조회 2,222
11년 전 조회 2,534
11년 전 조회 2,198
11년 전 조회 2,115
11년 전 조회 2,155
11년 전 조회 3,019
11년 전 조회 2,042
11년 전 조회 2,122
11년 전 조회 3,778
11년 전 조회 3,443
11년 전 조회 2,335
11년 전 조회 2,841
11년 전 조회 2,376
11년 전 조회 2,318