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

· 11년 전 · 3070
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,030
11년 전 조회 3,411
11년 전 조회 2,616
11년 전 조회 3,169
11년 전 조회 664
11년 전 조회 2,434
11년 전 조회 2,409
11년 전 조회 2,482
11년 전 조회 1,462
11년 전 조회 1,942
11년 전 조회 1,270
11년 전 조회 1,076
11년 전 조회 1,658
11년 전 조회 986
11년 전 조회 3,860
11년 전 조회 3,641
11년 전 조회 1,265
11년 전 조회 2,513
11년 전 조회 926
11년 전 조회 1,735
11년 전 조회 3,342
11년 전 조회 3,640
11년 전 조회 4,560
11년 전 조회 958
11년 전 조회 1,518
11년 전 조회 2,923
11년 전 조회 1,103
11년 전 조회 1,090
11년 전 조회 1,702
11년 전 조회 961
11년 전 조회 2,239
11년 전 조회 1,745
11년 전 조회 3,821
11년 전 조회 2,276
11년 전 조회 4,525
11년 전 조회 1,305
11년 전 조회 1,167
11년 전 조회 1,808
11년 전 조회 1,773
11년 전 조회 1,345
11년 전 조회 991
11년 전 조회 1,643
11년 전 조회 1,016
11년 전 조회 1,113
11년 전 조회 1,327
11년 전 조회 1,144
11년 전 조회 896
11년 전 조회 2,082
11년 전 조회 1,893
11년 전 조회 3,071
11년 전 조회 1,043
11년 전 조회 806
11년 전 조회 900
11년 전 조회 2,775
11년 전 조회 1,028
11년 전 조회 1,226
11년 전 조회 743
11년 전 조회 1,522
11년 전 조회 1,516
11년 전 조회 923
11년 전 조회 1,112
11년 전 조회 789
11년 전 조회 753
11년 전 조회 1,572
11년 전 조회 907
11년 전 조회 808
11년 전 조회 923
11년 전 조회 1,097
11년 전 조회 757
11년 전 조회 804
11년 전 조회 1,295
11년 전 조회 844
11년 전 조회 1,320
11년 전 조회 837
11년 전 조회 983
11년 전 조회 1,030
11년 전 조회 774
11년 전 조회 796
11년 전 조회 1,035
11년 전 조회 1,968
11년 전 조회 808
11년 전 조회 819
11년 전 조회 755
11년 전 조회 1,180
11년 전 조회 815
11년 전 조회 735
11년 전 조회 1,054
11년 전 조회 1,391
11년 전 조회 1,023
11년 전 조회 951
11년 전 조회 987
11년 전 조회 1,823
11년 전 조회 890
11년 전 조회 948
11년 전 조회 2,729
11년 전 조회 2,276
11년 전 조회 1,151
11년 전 조회 1,644
11년 전 조회 1,252
11년 전 조회 1,186
🐛 버그신고