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

· 11년 전 · 4228
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,124
11년 전 조회 4,514
11년 전 조회 3,765
11년 전 조회 4,268
11년 전 조회 1,775
11년 전 조회 3,576
11년 전 조회 3,580
11년 전 조회 3,658
11년 전 조회 2,632
11년 전 조회 3,093
11년 전 조회 2,446
11년 전 조회 2,214
11년 전 조회 2,833
11년 전 조회 2,146
11년 전 조회 5,002
11년 전 조회 4,747
11년 전 조회 2,426
11년 전 조회 3,660
11년 전 조회 2,083
11년 전 조회 2,835
11년 전 조회 4,473
11년 전 조회 4,785
11년 전 조회 5,686
11년 전 조회 2,066
11년 전 조회 2,656
11년 전 조회 4,100
11년 전 조회 2,242
11년 전 조회 2,246
11년 전 조회 2,821
11년 전 조회 2,120
11년 전 조회 3,360
11년 전 조회 2,888
11년 전 조회 4,949
11년 전 조회 3,389
11년 전 조회 5,678
11년 전 조회 2,461
11년 전 조회 2,328
11년 전 조회 3,008
11년 전 조회 2,966
11년 전 조회 2,515
11년 전 조회 2,141
11년 전 조회 2,778
11년 전 조회 2,203
11년 전 조회 2,280
11년 전 조회 2,507
11년 전 조회 2,331
11년 전 조회 2,055
11년 전 조회 3,243
11년 전 조회 3,073
11년 전 조회 4,229
11년 전 조회 2,259
11년 전 조회 2,001
11년 전 조회 2,079
11년 전 조회 3,972
11년 전 조회 2,191
11년 전 조회 2,412
11년 전 조회 1,941
11년 전 조회 2,664
11년 전 조회 2,672
11년 전 조회 2,088
11년 전 조회 2,255
11년 전 조회 1,959
11년 전 조회 1,907
11년 전 조회 2,717
11년 전 조회 2,043
11년 전 조회 1,973
11년 전 조회 2,125
11년 전 조회 2,239
11년 전 조회 1,912
11년 전 조회 1,986
11년 전 조회 2,420
11년 전 조회 2,034
11년 전 조회 2,412
11년 전 조회 2,062
11년 전 조회 2,112
11년 전 조회 2,205
11년 전 조회 1,959
11년 전 조회 1,994
11년 전 조회 2,198
11년 전 조회 3,107
11년 전 조회 2,024
11년 전 조회 2,024
11년 전 조회 1,938
11년 전 조회 2,384
11년 전 조회 2,017
11년 전 조회 1,936
11년 전 조회 2,267
11년 전 조회 2,588
11년 전 조회 2,253
11년 전 조회 2,170
11년 전 조회 2,199
11년 전 조회 3,063
11년 전 조회 2,109
11년 전 조회 2,185
11년 전 조회 3,891
11년 전 조회 3,503
11년 전 조회 2,386
11년 전 조회 2,892
11년 전 조회 2,445
11년 전 조회 2,360