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

· 11년 전 · 3130
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,090
11년 전 조회 3,463
11년 전 조회 2,670
11년 전 조회 3,223
11년 전 조회 727
11년 전 조회 2,497
11년 전 조회 2,469
11년 전 조회 2,549
11년 전 조회 1,524
11년 전 조회 2,004
11년 전 조회 1,332
11년 전 조회 1,133
11년 전 조회 1,713
11년 전 조회 1,037
11년 전 조회 3,924
11년 전 조회 3,695
11년 전 조회 1,331
11년 전 조회 2,567
11년 전 조회 988
11년 전 조회 1,794
11년 전 조회 3,401
11년 전 조회 3,700
11년 전 조회 4,624
11년 전 조회 1,017
11년 전 조회 1,579
11년 전 조회 2,985
11년 전 조회 1,165
11년 전 조회 1,156
11년 전 조회 1,765
11년 전 조회 1,026
11년 전 조회 2,299
11년 전 조회 1,806
11년 전 조회 3,892
11년 전 조회 2,340
11년 전 조회 4,591
11년 전 조회 1,361
11년 전 조회 1,229
11년 전 조회 1,868
11년 전 조회 1,838
11년 전 조회 1,409
11년 전 조회 1,057
11년 전 조회 1,692
11년 전 조회 1,077
11년 전 조회 1,174
11년 전 조회 1,392
11년 전 조회 1,207
11년 전 조회 955
11년 전 조회 2,142
11년 전 조회 1,966
11년 전 조회 3,131
11년 전 조회 1,103
11년 전 조회 868
11년 전 조회 961
11년 전 조회 2,834
11년 전 조회 1,082
11년 전 조회 1,284
11년 전 조회 807
11년 전 조회 1,587
11년 전 조회 1,575
11년 전 조회 984
11년 전 조회 1,163
11년 전 조회 847
11년 전 조회 806
11년 전 조회 1,629
11년 전 조회 963
11년 전 조회 865
11년 전 조회 976
11년 전 조회 1,147
11년 전 조회 812
11년 전 조회 856
11년 전 조회 1,341
11년 전 조회 901
11년 전 조회 1,370
11년 전 조회 888
11년 전 조회 1,025
11년 전 조회 1,078
11년 전 조회 825
11년 전 조회 857
11년 전 조회 1,082
11년 전 조회 2,015
11년 전 조회 857
11년 전 조회 878
11년 전 조회 806
11년 전 조회 1,234
11년 전 조회 883
11년 전 조회 792
11년 전 조회 1,104
11년 전 조회 1,445
11년 전 조회 1,078
11년 전 조회 1,001
11년 전 조회 1,048
11년 전 조회 1,880
11년 전 조회 943
11년 전 조회 1,003
11년 전 조회 2,785
11년 전 조회 2,334
11년 전 조회 1,209
11년 전 조회 1,702
11년 전 조회 1,307
11년 전 조회 1,235
🐛 버그신고