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

· 11년 전 · 4167
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,064
11년 전 조회 4,457
11년 전 조회 3,702
11년 전 조회 4,207
11년 전 조회 1,691
11년 전 조회 3,500
11년 전 조회 3,512
11년 전 조회 3,581
11년 전 조회 2,559
11년 전 조회 3,015
11년 전 조회 2,380
11년 전 조회 2,153
11년 전 조회 2,760
11년 전 조회 2,076
11년 전 조회 4,936
11년 전 조회 4,599
11년 전 조회 2,368
11년 전 조회 3,603
11년 전 조회 2,029
11년 전 조회 2,781
11년 전 조회 4,414
11년 전 조회 4,719
11년 전 조회 5,627
11년 전 조회 1,992
11년 전 조회 2,597
11년 전 조회 4,022
11년 전 조회 2,180
11년 전 조회 2,180
11년 전 조회 2,757
11년 전 조회 2,061
11년 전 조회 3,302
11년 전 조회 2,828
11년 전 조회 4,885
11년 전 조회 3,325
11년 전 조회 5,603
11년 전 조회 2,399
11년 전 조회 2,270
11년 전 조회 2,943
11년 전 조회 2,896
11년 전 조회 2,456
11년 전 조회 2,075
11년 전 조회 2,708
11년 전 조회 2,150
11년 전 조회 2,230
11년 전 조회 2,427
11년 전 조회 2,263
11년 전 조회 1,997
11년 전 조회 3,178
11년 전 조회 3,031
11년 전 조회 4,168
11년 전 조회 2,204
11년 전 조회 1,943
11년 전 조회 2,020
11년 전 조회 3,913
11년 전 조회 2,140
11년 전 조회 2,361
11년 전 조회 1,892
11년 전 조회 2,610
11년 전 조회 2,609
11년 전 조회 2,014
11년 전 조회 2,186
11년 전 조회 1,909
11년 전 조회 1,847
11년 전 조회 2,651
11년 전 조회 1,991
11년 전 조회 1,913
11년 전 조회 2,074
11년 전 조회 2,153
11년 전 조회 1,853
11년 전 조회 1,946
11년 전 조회 2,353
11년 전 조회 1,975
11년 전 조회 2,355
11년 전 조회 1,989
11년 전 조회 2,048
11년 전 조회 2,148
11년 전 조회 1,913
11년 전 조회 1,933
11년 전 조회 2,133
11년 전 조회 3,044
11년 전 조회 1,953
11년 전 조회 1,973
11년 전 조회 1,882
11년 전 조회 2,319
11년 전 조회 1,954
11년 전 조회 1,872
11년 전 조회 2,218
11년 전 조회 2,525
11년 전 조회 2,191
11년 전 조회 2,111
11년 전 조회 2,146
11년 전 조회 3,010
11년 전 조회 2,037
11년 전 조회 2,115
11년 전 조회 3,757
11년 전 조회 3,439
11년 전 조회 2,328
11년 전 조회 2,834
11년 전 조회 2,371
11년 전 조회 2,315