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

· 11년 전 · 3176
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,130
11년 전 조회 3,511
11년 전 조회 2,714
11년 전 조회 3,274
11년 전 조회 767
11년 전 조회 2,531
11년 전 조회 2,509
11년 전 조회 2,590
11년 전 조회 1,572
11년 전 조회 2,041
11년 전 조회 1,366
11년 전 조회 1,176
11년 전 조회 1,761
11년 전 조회 1,076
11년 전 조회 3,962
11년 전 조회 3,741
11년 전 조회 1,369
11년 전 조회 2,612
11년 전 조회 1,022
11년 전 조회 1,838
11년 전 조회 3,442
11년 전 조회 3,746
11년 전 조회 4,667
11년 전 조회 1,063
11년 전 조회 1,625
11년 전 조회 3,028
11년 전 조회 1,197
11년 전 조회 1,200
11년 전 조회 1,808
11년 전 조회 1,077
11년 전 조회 2,339
11년 전 조회 1,854
11년 전 조회 3,935
11년 전 조회 2,382
11년 전 조회 4,638
11년 전 조회 1,410
11년 전 조회 1,266
11년 전 조회 1,916
11년 전 조회 1,883
11년 전 조회 1,461
11년 전 조회 1,099
11년 전 조회 1,733
11년 전 조회 1,117
11년 전 조회 1,231
11년 전 조회 1,424
11년 전 조회 1,251
11년 전 조회 1,006
11년 전 조회 2,180
11년 전 조회 2,007
11년 전 조회 3,177
11년 전 조회 1,148
11년 전 조회 915
11년 전 조회 1,011
11년 전 조회 2,879
11년 전 조회 1,130
11년 전 조회 1,330
11년 전 조회 850
11년 전 조회 1,633
11년 전 조회 1,622
11년 전 조회 1,031
11년 전 조회 1,205
11년 전 조회 894
11년 전 조회 852
11년 전 조회 1,684
11년 전 조회 1,007
11년 전 조회 910
11년 전 조회 1,029
11년 전 조회 1,195
11년 전 조회 864
11년 전 조회 905
11년 전 조회 1,380
11년 전 조회 951
11년 전 조회 1,419
11년 전 조회 941
11년 전 조회 1,062
11년 전 조회 1,125
11년 전 조회 881
11년 전 조회 905
11년 전 조회 1,135
11년 전 조회 2,064
11년 전 조회 913
11년 전 조회 929
11년 전 조회 865
11년 전 조회 1,279
11년 전 조회 927
11년 전 조회 839
11년 전 조회 1,151
11년 전 조회 1,496
11년 전 조회 1,137
11년 전 조회 1,051
11년 전 조회 1,097
11년 전 조회 1,944
11년 전 조회 1,005
11년 전 조회 1,055
11년 전 조회 2,838
11년 전 조회 2,390
11년 전 조회 1,253
11년 전 조회 1,752
11년 전 조회 1,355
11년 전 조회 1,289
🐛 버그신고