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

· 11년 전 · 4224
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,115
11년 전 조회 4,512
11년 전 조회 3,758
11년 전 조회 4,265
11년 전 조회 1,766
11년 전 조회 3,572
11년 전 조회 3,573
11년 전 조회 3,646
11년 전 조회 2,624
11년 전 조회 3,087
11년 전 조회 2,438
11년 전 조회 2,209
11년 전 조회 2,827
11년 전 조회 2,141
11년 전 조회 4,995
11년 전 조회 4,732
11년 전 조회 2,418
11년 전 조회 3,653
11년 전 조회 2,078
11년 전 조회 2,829
11년 전 조회 4,466
11년 전 조회 4,774
11년 전 조회 5,679
11년 전 조회 2,050
11년 전 조회 2,652
11년 전 조회 4,095
11년 전 조회 2,235
11년 전 조회 2,242
11년 전 조회 2,815
11년 전 조회 2,113
11년 전 조회 3,352
11년 전 조회 2,884
11년 전 조회 4,941
11년 전 조회 3,384
11년 전 조회 5,673
11년 전 조회 2,447
11년 전 조회 2,322
11년 전 조회 3,002
11년 전 조회 2,962
11년 전 조회 2,509
11년 전 조회 2,136
11년 전 조회 2,771
11년 전 조회 2,200
11년 전 조회 2,274
11년 전 조회 2,501
11년 전 조회 2,323
11년 전 조회 2,053
11년 전 조회 3,238
11년 전 조회 3,070
11년 전 조회 4,225
11년 전 조회 2,251
11년 전 조회 1,993
11년 전 조회 2,075
11년 전 조회 3,963
11년 전 조회 2,182
11년 전 조회 2,404
11년 전 조회 1,933
11년 전 조회 2,658
11년 전 조회 2,666
11년 전 조회 2,085
11년 전 조회 2,246
11년 전 조회 1,954
11년 전 조회 1,905
11년 전 조회 2,714
11년 전 조회 2,036
11년 전 조회 1,966
11년 전 조회 2,122
11년 전 조회 2,233
11년 전 조회 1,909
11년 전 조회 1,982
11년 전 조회 2,417
11년 전 조회 2,030
11년 전 조회 2,407
11년 전 조회 2,050
11년 전 조회 2,108
11년 전 조회 2,195
11년 전 조회 1,953
11년 전 조회 1,986
11년 전 조회 2,192
11년 전 조회 3,102
11년 전 조회 2,019
11년 전 조회 2,016
11년 전 조회 1,934
11년 전 조회 2,377
11년 전 조회 2,011
11년 전 조회 1,927
11년 전 조회 2,260
11년 전 조회 2,584
11년 전 조회 2,247
11년 전 조회 2,163
11년 전 조회 2,191
11년 전 조회 3,054
11년 전 조회 2,101
11년 전 조회 2,174
11년 전 조회 3,877
11년 전 조회 3,496
11년 전 조회 2,378
11년 전 조회 2,886
11년 전 조회 2,439
11년 전 조회 2,357