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

· 11년 전 · 3039
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년 전 조회 993
11년 전 조회 3,377
11년 전 조회 2,574
11년 전 조회 3,149
11년 전 조회 628
11년 전 조회 2,394
11년 전 조회 2,376
11년 전 조회 2,452
11년 전 조회 1,434
11년 전 조회 1,909
11년 전 조회 1,232
11년 전 조회 1,049
11년 전 조회 1,625
11년 전 조회 951
11년 전 조회 3,829
11년 전 조회 3,605
11년 전 조회 1,228
11년 전 조회 2,475
11년 전 조회 896
11년 전 조회 1,701
11년 전 조회 3,307
11년 전 조회 3,611
11년 전 조회 4,532
11년 전 조회 929
11년 전 조회 1,497
11년 전 조회 2,906
11년 전 조회 1,070
11년 전 조회 1,057
11년 전 조회 1,671
11년 전 조회 925
11년 전 조회 2,210
11년 전 조회 1,716
11년 전 조회 3,793
11년 전 조회 2,256
11년 전 조회 4,507
11년 전 조회 1,276
11년 전 조회 1,138
11년 전 조회 1,780
11년 전 조회 1,744
11년 전 조회 1,315
11년 전 조회 965
11년 전 조회 1,613
11년 전 조회 986
11년 전 조회 1,084
11년 전 조회 1,297
11년 전 조회 1,113
11년 전 조회 874
11년 전 조회 2,050
11년 전 조회 1,869
11년 전 조회 3,040
11년 전 조회 1,016
11년 전 조회 780
11년 전 조회 869
11년 전 조회 2,752
11년 전 조회 1,006
11년 전 조회 1,200
11년 전 조회 716
11년 전 조회 1,499
11년 전 조회 1,489
11년 전 조회 898
11년 전 조회 1,080
11년 전 조회 763
11년 전 조회 720
11년 전 조회 1,548
11년 전 조회 882
11년 전 조회 784
11년 전 조회 899
11년 전 조회 1,084
11년 전 조회 732
11년 전 조회 776
11년 전 조회 1,287
11년 전 조회 819
11년 전 조회 1,307
11년 전 조회 815
11년 전 조회 967
11년 전 조회 1,002
11년 전 조회 747
11년 전 조회 780
11년 전 조회 1,016
11년 전 조회 1,955
11년 전 조회 785
11년 전 조회 797
11년 전 조회 735
11년 전 조회 1,149
11년 전 조회 797
11년 전 조회 711
11년 전 조회 1,039
11년 전 조회 1,371
11년 전 조회 1,003
11년 전 조회 924
11년 전 조회 966
11년 전 조회 1,796
11년 전 조회 867
11년 전 조회 926
11년 전 조회 2,704
11년 전 조회 2,255
11년 전 조회 1,137
11년 전 조회 1,621
11년 전 조회 1,234
11년 전 조회 1,163
🐛 버그신고