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

· 11년 전 · 3251
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년 전 조회 1,224
11년 전 조회 3,578
11년 전 조회 2,802
11년 전 조회 3,330
11년 전 조회 861
11년 전 조회 2,615
11년 전 조회 2,580
11년 전 조회 2,654
11년 전 조회 1,647
11년 전 조회 2,122
11년 전 조회 1,454
11년 전 조회 1,235
11년 전 조회 1,855
11년 전 조회 1,173
11년 전 조회 4,038
11년 전 조회 3,826
11년 전 조회 1,446
11년 전 조회 2,687
11년 전 조회 1,118
11년 전 조회 1,906
11년 전 조회 3,515
11년 전 조회 3,816
11년 전 조회 4,739
11년 전 조회 1,149
11년 전 조회 1,685
11년 전 조회 3,094
11년 전 조회 1,293
11년 전 조회 1,289
11년 전 조회 1,873
11년 전 조회 1,169
11년 전 조회 2,421
11년 전 조회 1,918
11년 전 조회 3,991
11년 전 조회 2,450
11년 전 조회 4,707
11년 전 조회 1,512
11년 전 조회 1,350
11년 전 조회 1,992
11년 전 조회 1,964
11년 전 조회 1,545
11년 전 조회 1,177
11년 전 조회 1,828
11년 전 조회 1,211
11년 전 조회 1,299
11년 전 조회 1,523
11년 전 조회 1,334
11년 전 조회 1,066
11년 전 조회 2,278
11년 전 조회 2,076
11년 전 조회 3,252
11년 전 조회 1,228
11년 전 조회 991
11년 전 조회 1,090
11년 전 조회 2,957
11년 전 조회 1,199
11년 전 조회 1,404
11년 전 조회 942
11년 전 조회 1,709
11년 전 조회 1,711
11년 전 조회 1,110
11년 전 조회 1,316
11년 전 조회 970
11년 전 조회 941
11년 전 조회 1,754
11년 전 조회 1,092
11년 전 조회 987
11년 전 조회 1,116
11년 전 조회 1,247
11년 전 조회 955
11년 전 조회 989
11년 전 조회 1,442
11년 전 조회 1,047
11년 전 조회 1,460
11년 전 조회 1,053
11년 전 조회 1,132
11년 전 조회 1,209
11년 전 조회 958
11년 전 조회 983
11년 전 조회 1,207
11년 전 조회 2,118
11년 전 조회 993
11년 전 조회 1,000
11년 전 조회 931
11년 전 조회 1,387
11년 전 조회 1,007
11년 전 조회 943
11년 전 조회 1,236
11년 전 조회 1,595
11년 전 조회 1,209
11년 전 조회 1,164
11년 전 조회 1,176
11년 전 조회 2,015
11년 전 조회 1,084
11년 전 조회 1,136
11년 전 조회 2,939
11년 전 조회 2,491
11년 전 조회 1,356
11년 전 조회 1,857
11년 전 조회 1,443
11년 전 조회 1,378