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

· 11년 전 · 3079
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,039
11년 전 조회 3,420
11년 전 조회 2,627
11년 전 조회 3,175
11년 전 조회 675
11년 전 조회 2,447
11년 전 조회 2,418
11년 전 조회 2,489
11년 전 조회 1,476
11년 전 조회 1,952
11년 전 조회 1,278
11년 전 조회 1,085
11년 전 조회 1,668
11년 전 조회 989
11년 전 조회 3,875
11년 전 조회 3,650
11년 전 조회 1,278
11년 전 조회 2,521
11년 전 조회 934
11년 전 조회 1,742
11년 전 조회 3,350
11년 전 조회 3,652
11년 전 조회 4,571
11년 전 조회 966
11년 전 조회 1,527
11년 전 조회 2,934
11년 전 조회 1,112
11년 전 조회 1,097
11년 전 조회 1,714
11년 전 조회 970
11년 전 조회 2,248
11년 전 조회 1,750
11년 전 조회 3,831
11년 전 조회 2,284
11년 전 조회 4,534
11년 전 조회 1,312
11년 전 조회 1,178
11년 전 조회 1,815
11년 전 조회 1,780
11년 전 조회 1,355
11년 전 조회 999
11년 전 조회 1,647
11년 전 조회 1,024
11년 전 조회 1,122
11년 전 조회 1,333
11년 전 조회 1,152
11년 전 조회 904
11년 전 조회 2,088
11년 전 조회 1,903
11년 전 조회 3,079
11년 전 조회 1,053
11년 전 조회 813
11년 전 조회 911
11년 전 조회 2,782
11년 전 조회 1,037
11년 전 조회 1,232
11년 전 조회 752
11년 전 조회 1,530
11년 전 조회 1,521
11년 전 조회 927
11년 전 조회 1,116
11년 전 조회 796
11년 전 조회 760
11년 전 조회 1,580
11년 전 조회 917
11년 전 조회 812
11년 전 조회 926
11년 전 조회 1,101
11년 전 조회 763
11년 전 조회 806
11년 전 조회 1,300
11년 전 조회 849
11년 전 조회 1,326
11년 전 조회 842
11년 전 조회 987
11년 전 조회 1,035
11년 전 조회 780
11년 전 조회 803
11년 전 조회 1,039
11년 전 조회 1,973
11년 전 조회 816
11년 전 조회 823
11년 전 조회 762
11년 전 조회 1,188
11년 전 조회 822
11년 전 조회 743
11년 전 조회 1,062
11년 전 조회 1,397
11년 전 조회 1,028
11년 전 조회 956
11년 전 조회 996
11년 전 조회 1,829
11년 전 조회 897
11년 전 조회 955
11년 전 조회 2,733
11년 전 조회 2,286
11년 전 조회 1,158
11년 전 조회 1,650
11년 전 조회 1,259
11년 전 조회 1,191
🐛 버그신고