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

· 11년 전 · 3140
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,099
11년 전 조회 3,472
11년 전 조회 2,682
11년 전 조회 3,235
11년 전 조회 736
11년 전 조회 2,503
11년 전 조회 2,474
11년 전 조회 2,555
11년 전 조회 1,534
11년 전 조회 2,010
11년 전 조회 1,339
11년 전 조회 1,140
11년 전 조회 1,722
11년 전 조회 1,045
11년 전 조회 3,933
11년 전 조회 3,702
11년 전 조회 1,339
11년 전 조회 2,573
11년 전 조회 994
11년 전 조회 1,801
11년 전 조회 3,411
11년 전 조회 3,711
11년 전 조회 4,634
11년 전 조회 1,028
11년 전 조회 1,587
11년 전 조회 2,992
11년 전 조회 1,173
11년 전 조회 1,163
11년 전 조회 1,775
11년 전 조회 1,036
11년 전 조회 2,307
11년 전 조회 1,815
11년 전 조회 3,897
11년 전 조회 2,349
11년 전 조회 4,597
11년 전 조회 1,373
11년 전 조회 1,238
11년 전 조회 1,880
11년 전 조회 1,848
11년 전 조회 1,422
11년 전 조회 1,070
11년 전 조회 1,701
11년 전 조회 1,086
11년 전 조회 1,187
11년 전 조회 1,397
11년 전 조회 1,218
11년 전 조회 967
11년 전 조회 2,147
11년 전 조회 1,969
11년 전 조회 3,141
11년 전 조회 1,108
11년 전 조회 881
11년 전 조회 974
11년 전 조회 2,847
11년 전 조회 1,089
11년 전 조회 1,290
11년 전 조회 819
11년 전 조회 1,592
11년 전 조회 1,586
11년 전 조회 994
11년 전 조회 1,172
11년 전 조회 858
11년 전 조회 812
11년 전 조회 1,639
11년 전 조회 972
11년 전 조회 875
11년 전 조회 989
11년 전 조회 1,165
11년 전 조회 824
11년 전 조회 865
11년 전 조회 1,351
11년 전 조회 913
11년 전 조회 1,388
11년 전 조회 899
11년 전 조회 1,036
11년 전 조회 1,087
11년 전 조회 837
11년 전 조회 869
11년 전 조회 1,091
11년 전 조회 2,026
11년 전 조회 867
11년 전 조회 892
11년 전 조회 822
11년 전 조회 1,240
11년 전 조회 895
11년 전 조회 801
11년 전 조회 1,114
11년 전 조회 1,455
11년 전 조회 1,092
11년 전 조회 1,016
11년 전 조회 1,063
11년 전 조회 1,893
11년 전 조회 955
11년 전 조회 1,015
11년 전 조회 2,793
11년 전 조회 2,349
11년 전 조회 1,221
11년 전 조회 1,711
11년 전 조회 1,318
11년 전 조회 1,246
🐛 버그신고