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

· 11년 전 · 4416
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년 전 조회 2,351
11년 전 조회 4,717
11년 전 조회 3,966
11년 전 조회 4,454
11년 전 조회 1,994
11년 전 조회 3,773
11년 전 조회 3,757
11년 전 조회 3,837
11년 전 조회 2,829
11년 전 조회 3,279
11년 전 조회 2,630
11년 전 조회 2,414
11년 전 조회 3,048
11년 전 조회 2,341
11년 전 조회 5,210
11년 전 조회 4,863
11년 전 조회 2,599
11년 전 조회 3,838
11년 전 조회 2,268
11년 전 조회 3,026
11년 전 조회 4,661
11년 전 조회 4,986
11년 전 조회 5,889
11년 전 조회 2,265
11년 전 조회 2,827
11년 전 조회 4,272
11년 전 조회 2,440
11년 전 조회 2,442
11년 전 조회 3,025
11년 전 조회 2,307
11년 전 조회 3,556
11년 전 조회 3,077
11년 전 조회 5,162
11년 전 조회 3,580
11년 전 조회 5,851
11년 전 조회 2,669
11년 전 조회 2,504
11년 전 조회 3,186
11년 전 조회 3,147
11년 전 조회 2,686
11년 전 조회 2,334
11년 전 조회 2,973
11년 전 조회 2,382
11년 전 조회 2,461
11년 전 조회 2,700
11년 전 조회 2,526
11년 전 조회 2,227
11년 전 조회 3,450
11년 전 조회 3,257
11년 전 조회 4,417
11년 전 조회 2,420
11년 전 조회 2,189
11년 전 조회 2,268
11년 전 조회 4,166
11년 전 조회 2,368
11년 전 조회 2,608
11년 전 조회 2,131
11년 전 조회 2,839
11년 전 조회 2,858
11년 전 조회 2,278
11년 전 조회 2,483
11년 전 조회 2,142
11년 전 조회 2,074
11년 전 조회 2,878
11년 전 조회 2,248
11년 전 조회 2,143
11년 전 조회 2,306
11년 전 조회 2,389
11년 전 조회 2,082
11년 전 조회 2,171
11년 전 조회 2,580
11년 전 조회 2,239
11년 전 조회 2,573
11년 전 조회 2,265
11년 전 조회 2,264
11년 전 조회 2,388
11년 전 조회 2,155
11년 전 조회 2,180
11년 전 조회 2,378
11년 전 조회 3,255
11년 전 조회 2,193
11년 전 조회 2,206
11년 전 조회 2,092
11년 전 조회 2,562
11년 전 조회 2,198
11년 전 조회 2,114
11년 전 조회 2,436
11년 전 조회 2,758
11년 전 조회 2,425
11년 전 조회 2,345
11년 전 조회 2,362
11년 전 조회 3,233
11년 전 조회 2,273
11년 전 조회 2,350
11년 전 조회 4,014
11년 전 조회 3,677
11년 전 조회 2,551
11년 전 조회 3,081
11년 전 조회 2,596
11년 전 조회 2,530