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

· 11년 전 · 3051
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,012
11년 전 조회 3,390
11년 전 조회 2,591
11년 전 조회 3,155
11년 전 조회 646
11년 전 조회 2,409
11년 전 조회 2,388
11년 전 조회 2,463
11년 전 조회 1,448
11년 전 조회 1,920
11년 전 조회 1,249
11년 전 조회 1,057
11년 전 조회 1,639
11년 전 조회 968
11년 전 조회 3,842
11년 전 조회 3,619
11년 전 조회 1,247
11년 전 조회 2,490
11년 전 조회 911
11년 전 조회 1,714
11년 전 조회 3,320
11년 전 조회 3,621
11년 전 조회 4,546
11년 전 조회 946
11년 전 조회 1,506
11년 전 조회 2,912
11년 전 조회 1,087
11년 전 조회 1,075
11년 전 조회 1,686
11년 전 조회 944
11년 전 조회 2,222
11년 전 조회 1,724
11년 전 조회 3,804
11년 전 조회 2,265
11년 전 조회 4,513
11년 전 조회 1,293
11년 전 조회 1,152
11년 전 조회 1,792
11년 전 조회 1,757
11년 전 조회 1,333
11년 전 조회 977
11년 전 조회 1,631
11년 전 조회 1,002
11년 전 조회 1,098
11년 전 조회 1,314
11년 전 조회 1,128
11년 전 조회 883
11년 전 조회 2,068
11년 전 조회 1,881
11년 전 조회 3,052
11년 전 조회 1,029
11년 전 조회 793
11년 전 조회 885
11년 전 조회 2,763
11년 전 조회 1,018
11년 전 조회 1,212
11년 전 조회 732
11년 전 조회 1,512
11년 전 조회 1,503
11년 전 조회 913
11년 전 조회 1,099
11년 전 조회 778
11년 전 조회 737
11년 전 조회 1,562
11년 전 조회 897
11년 전 조회 798
11년 전 조회 910
11년 전 조회 1,091
11년 전 조회 746
11년 전 조회 788
11년 전 조회 1,290
11년 전 조회 831
11년 전 조회 1,314
11년 전 조회 833
11년 전 조회 972
11년 전 조회 1,014
11년 전 조회 760
11년 전 조회 788
11년 전 조회 1,024
11년 전 조회 1,957
11년 전 조회 797
11년 전 조회 808
11년 전 조회 745
11년 전 조회 1,172
11년 전 조회 806
11년 전 조회 728
11년 전 조회 1,049
11년 전 조회 1,384
11년 전 조회 1,014
11년 전 조회 941
11년 전 조회 976
11년 전 조회 1,810
11년 전 조회 879
11년 전 조회 938
11년 전 조회 2,719
11년 전 조회 2,267
11년 전 조회 1,144
11년 전 조회 1,638
11년 전 조회 1,243
11년 전 조회 1,175
🐛 버그신고