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

· 11년 전 · 3067
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,027
11년 전 조회 3,411
11년 전 조회 2,613
11년 전 조회 3,168
11년 전 조회 661
11년 전 조회 2,427
11년 전 조회 2,408
11년 전 조회 2,481
11년 전 조회 1,459
11년 전 조회 1,940
11년 전 조회 1,267
11년 전 조회 1,076
11년 전 조회 1,656
11년 전 조회 983
11년 전 조회 3,859
11년 전 조회 3,639
11년 전 조회 1,263
11년 전 조회 2,513
11년 전 조회 924
11년 전 조회 1,734
11년 전 조회 3,342
11년 전 조회 3,639
11년 전 조회 4,559
11년 전 조회 953
11년 전 조회 1,518
11년 전 조회 2,923
11년 전 조회 1,103
11년 전 조회 1,087
11년 전 조회 1,701
11년 전 조회 959
11년 전 조회 2,238
11년 전 조회 1,743
11년 전 조회 3,821
11년 전 조회 2,276
11년 전 조회 4,525
11년 전 조회 1,303
11년 전 조회 1,167
11년 전 조회 1,807
11년 전 조회 1,773
11년 전 조회 1,343
11년 전 조회 990
11년 전 조회 1,639
11년 전 조회 1,015
11년 전 조회 1,110
11년 전 조회 1,325
11년 전 조회 1,143
11년 전 조회 894
11년 전 조회 2,080
11년 전 조회 1,892
11년 전 조회 3,068
11년 전 조회 1,042
11년 전 조회 805
11년 전 조회 899
11년 전 조회 2,775
11년 전 조회 1,026
11년 전 조회 1,225
11년 전 조회 743
11년 전 조회 1,521
11년 전 조회 1,515
11년 전 조회 922
11년 전 조회 1,111
11년 전 조회 789
11년 전 조회 751
11년 전 조회 1,571
11년 전 조회 906
11년 전 조회 807
11년 전 조회 922
11년 전 조회 1,096
11년 전 조회 757
11년 전 조회 802
11년 전 조회 1,295
11년 전 조회 844
11년 전 조회 1,318
11년 전 조회 837
11년 전 조회 982
11년 전 조회 1,028
11년 전 조회 771
11년 전 조회 794
11년 전 조회 1,034
11년 전 조회 1,968
11년 전 조회 808
11년 전 조회 818
11년 전 조회 754
11년 전 조회 1,179
11년 전 조회 814
11년 전 조회 734
11년 전 조회 1,053
11년 전 조회 1,391
11년 전 조회 1,022
11년 전 조회 951
11년 전 조회 985
11년 전 조회 1,821
11년 전 조회 888
11년 전 조회 946
11년 전 조회 2,727
11년 전 조회 2,275
11년 전 조회 1,150
11년 전 조회 1,643
11년 전 조회 1,252
11년 전 조회 1,185
🐛 버그신고