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

· 11년 전 · 3984
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,815
11년 전 조회 4,212
11년 전 조회 3,462
11년 전 조회 3,953
11년 전 조회 1,451
11년 전 조회 3,256
11년 전 조회 3,262
11년 전 조회 3,319
11년 전 조회 2,308
11년 전 조회 2,773
11년 전 조회 2,102
11년 전 조회 1,894
11년 전 조회 2,506
11년 전 조회 1,803
11년 전 조회 4,699
11년 전 조회 4,475
11년 전 조회 2,121
11년 전 조회 3,362
11년 전 조회 1,758
11년 전 조회 2,564
11년 전 조회 4,159
11년 전 조회 4,470
11년 전 조회 5,393
11년 전 조회 1,742
11년 전 조회 2,338
11년 전 조회 3,747
11년 전 조회 1,927
11년 전 조회 1,926
11년 전 조회 2,501
11년 전 조회 1,802
11년 전 조회 3,027
11년 전 조회 2,555
11년 전 조회 4,613
11년 전 조회 3,065
11년 전 조회 5,332
11년 전 조회 2,122
11년 전 조회 2,011
11년 전 조회 2,667
11년 전 조회 2,626
11년 전 조회 2,192
11년 전 조회 1,785
11년 전 조회 2,450
11년 전 조회 1,967
11년 전 조회 1,950
11년 전 조회 2,152
11년 전 조회 2,065
11년 전 조회 1,717
11년 전 조회 2,894
11년 전 조회 2,854
11년 전 조회 3,985
11년 전 조회 1,994
11년 전 조회 1,749
11년 전 조회 1,742
11년 전 조회 3,664
11년 전 조회 1,878
11년 전 조회 2,197
11년 전 조회 1,716
11년 전 조회 2,369
11년 전 조회 2,354
11년 전 조회 1,757
11년 전 조회 1,931
11년 전 조회 1,761
11년 전 조회 1,692
11년 전 조회 2,399
11년 전 조회 1,827
11년 전 조회 1,752
11년 전 조회 1,905
11년 전 조회 1,891
11년 전 조회 1,599
11년 전 조회 1,785
11년 전 조회 2,176
11년 전 조회 1,818
11년 전 조회 2,089
11년 전 조회 1,817
11년 전 조회 1,790
11년 전 조회 1,995
11년 전 조회 1,741
11년 전 조회 1,771
11년 전 조회 1,865
11년 전 조회 2,773
11년 전 조회 1,791
11년 전 조회 1,801
11년 전 조회 1,612
11년 전 조회 2,057
11년 전 조회 1,785
11년 전 조회 1,605
11년 전 조회 2,048
11년 전 조회 2,266
11년 전 조회 2,027
11년 전 조회 1,871
11년 전 조회 1,979
11년 전 조회 2,847
11년 전 조회 1,780
11년 전 조회 1,840
11년 전 조회 3,624
11년 전 조회 3,287
11년 전 조회 2,164
11년 전 조회 2,667
11년 전 조회 2,111
11년 전 조회 2,056