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

· 11년 전 · 3200
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,152
11년 전 조회 3,535
11년 전 조회 2,737
11년 전 조회 3,286
11년 전 조회 790
11년 전 조회 2,553
11년 전 조회 2,522
11년 전 조회 2,609
11년 전 조회 1,598
11년 전 조회 2,063
11년 전 조회 1,396
11년 전 조회 1,197
11년 전 조회 1,783
11년 전 조회 1,101
11년 전 조회 3,988
11년 전 조회 3,766
11년 전 조회 1,389
11년 전 조회 2,635
11년 전 조회 1,050
11년 전 조회 1,860
11년 전 조회 3,464
11년 전 조회 3,773
11년 전 조회 4,692
11년 전 조회 1,081
11년 전 조회 1,645
11년 전 조회 3,043
11년 전 조회 1,221
11년 전 조회 1,226
11년 전 조회 1,833
11년 전 조회 1,100
11년 전 조회 2,368
11년 전 조회 1,873
11년 전 조회 3,952
11년 전 조회 2,397
11년 전 조회 4,660
11년 전 조회 1,437
11년 전 조회 1,285
11년 전 조회 1,938
11년 전 조회 1,908
11년 전 조회 1,487
11년 전 조회 1,124
11년 전 조회 1,755
11년 전 조회 1,143
11년 전 조회 1,252
11년 전 조회 1,447
11년 전 조회 1,273
11년 전 조회 1,023
11년 전 조회 2,209
11년 전 조회 2,032
11년 전 조회 3,201
11년 전 조회 1,175
11년 전 조회 936
11년 전 조회 1,038
11년 전 조회 2,906
11년 전 조회 1,149
11년 전 조회 1,353
11년 전 조회 874
11년 전 조회 1,655
11년 전 조회 1,642
11년 전 조회 1,052
11년 전 조회 1,229
11년 전 조회 916
11년 전 조회 878
11년 전 조회 1,716
11년 전 조회 1,034
11년 전 조회 937
11년 전 조회 1,053
11년 전 조회 1,218
11년 전 조회 892
11년 전 조회 934
11년 전 조회 1,405
11년 전 조회 971
11년 전 조회 1,430
11년 전 조회 968
11년 전 조회 1,081
11년 전 조회 1,153
11년 전 조회 900
11년 전 조회 932
11년 전 조회 1,153
11년 전 조회 2,083
11년 전 조회 940
11년 전 조회 956
11년 전 조회 883
11년 전 조회 1,305
11년 전 조회 949
11년 전 조회 868
11년 전 조회 1,177
11년 전 조회 1,516
11년 전 조회 1,162
11년 전 조회 1,077
11년 전 조회 1,114
11년 전 조회 1,964
11년 전 조회 1,028
11년 전 조회 1,082
11년 전 조회 2,867
11년 전 조회 2,415
11년 전 조회 1,284
11년 전 조회 1,770
11년 전 조회 1,389
11년 전 조회 1,308
🐛 버그신고