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

· 11년 전 · 3919
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,753
11년 전 조회 4,128
11년 전 조회 3,384
11년 전 조회 3,864
11년 전 조회 1,377
11년 전 조회 3,184
11년 전 조회 3,174
11년 전 조회 3,237
11년 전 조회 2,230
11년 전 조회 2,695
11년 전 조회 2,014
11년 전 조회 1,810
11년 전 조회 2,423
11년 전 조회 1,742
11년 전 조회 4,605
11년 전 조회 4,472
11년 전 조회 2,047
11년 전 조회 3,268
11년 전 조회 1,665
11년 전 조회 2,478
11년 전 조회 4,077
11년 전 조회 4,396
11년 전 조회 5,293
11년 전 조회 1,668
11년 전 조회 2,241
11년 전 조회 3,644
11년 전 조회 1,833
11년 전 조회 1,847
11년 전 조회 2,414
11년 전 조회 1,718
11년 전 조회 2,939
11년 전 조회 2,461
11년 전 조회 4,534
11년 전 조회 2,976
11년 전 조회 5,228
11년 전 조회 2,037
11년 전 조회 1,928
11년 전 조회 2,580
11년 전 조회 2,545
11년 전 조회 2,111
11년 전 조회 1,705
11년 전 조회 2,377
11년 전 조회 1,889
11년 전 조회 1,870
11년 전 조회 2,067
11년 전 조회 1,993
11년 전 조회 1,617
11년 전 조회 2,812
11년 전 조회 2,790
11년 전 조회 3,920
11년 전 조회 1,931
11년 전 조회 1,682
11년 전 조회 1,668
11년 전 조회 3,614
11년 전 조회 1,796
11년 전 조회 2,125
11년 전 조회 1,653
11년 전 조회 2,293
11년 전 조회 2,261
11년 전 조회 1,670
11년 전 조회 1,850
11년 전 조회 1,693
11년 전 조회 1,651
11년 전 조회 2,324
11년 전 조회 1,770
11년 전 조회 1,688
11년 전 조회 1,840
11년 전 조회 1,814
11년 전 조회 1,524
11년 전 조회 1,708
11년 전 조회 2,101
11년 전 조회 1,765
11년 전 조회 2,002
11년 전 조회 1,756
11년 전 조회 1,698
11년 전 조회 1,938
11년 전 조회 1,684
11년 전 조회 1,693
11년 전 조회 1,764
11년 전 조회 2,667
11년 전 조회 1,727
11년 전 조회 1,732
11년 전 조회 1,523
11년 전 조회 1,981
11년 전 조회 1,721
11년 전 조회 1,528
11년 전 조회 1,974
11년 전 조회 2,187
11년 전 조회 1,963
11년 전 조회 1,790
11년 전 조회 1,914
11년 전 조회 2,788
11년 전 조회 1,679
11년 전 조회 1,752
11년 전 조회 3,622
11년 전 조회 3,210
11년 전 조회 2,074
11년 전 조회 2,590
11년 전 조회 2,031
11년 전 조회 1,968