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

· 11년 전 · 4440
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,374
11년 전 조회 4,740
11년 전 조회 3,997
11년 전 조회 4,480
11년 전 조회 2,015
11년 전 조회 3,795
11년 전 조회 3,777
11년 전 조회 3,857
11년 전 조회 2,848
11년 전 조회 3,303
11년 전 조회 2,652
11년 전 조회 2,439
11년 전 조회 3,064
11년 전 조회 2,365
11년 전 조회 5,245
11년 전 조회 4,870
11년 전 조회 2,626
11년 전 조회 3,858
11년 전 조회 2,297
11년 전 조회 3,060
11년 전 조회 4,692
11년 전 조회 5,011
11년 전 조회 5,920
11년 전 조회 2,290
11년 전 조회 2,853
11년 전 조회 4,289
11년 전 조회 2,465
11년 전 조회 2,459
11년 전 조회 3,043
11년 전 조회 2,330
11년 전 조회 3,586
11년 전 조회 3,101
11년 전 조회 5,190
11년 전 조회 3,609
11년 전 조회 5,880
11년 전 조회 2,703
11년 전 조회 2,522
11년 전 조회 3,203
11년 전 조회 3,170
11년 전 조회 2,716
11년 전 조회 2,366
11년 전 조회 3,001
11년 전 조회 2,412
11년 전 조회 2,487
11년 전 조회 2,720
11년 전 조회 2,556
11년 전 조회 2,245
11년 전 조회 3,478
11년 전 조회 3,277
11년 전 조회 4,441
11년 전 조회 2,441
11년 전 조회 2,205
11년 전 조회 2,290
11년 전 조회 4,186
11년 전 조회 2,384
11년 전 조회 2,635
11년 전 조회 2,151
11년 전 조회 2,865
11년 전 조회 2,880
11년 전 조회 2,298
11년 전 조회 2,506
11년 전 조회 2,166
11년 전 조회 2,101
11년 전 조회 2,898
11년 전 조회 2,287
11년 전 조회 2,173
11년 전 조회 2,336
11년 전 조회 2,407
11년 전 조회 2,116
11년 전 조회 2,205
11년 전 조회 2,600
11년 전 조회 2,263
11년 전 조회 2,598
11년 전 조회 2,297
11년 전 조회 2,298
11년 전 조회 2,419
11년 전 조회 2,181
11년 전 조회 2,212
11년 전 조회 2,405
11년 전 조회 3,278
11년 전 조회 2,233
11년 전 조회 2,240
11년 전 조회 2,115
11년 전 조회 2,585
11년 전 조회 2,231
11년 전 조회 2,142
11년 전 조회 2,464
11년 전 조회 2,785
11년 전 조회 2,453
11년 전 조회 2,367
11년 전 조회 2,393
11년 전 조회 3,256
11년 전 조회 2,298
11년 전 조회 2,374
11년 전 조회 4,019
11년 전 조회 3,697
11년 전 조회 2,574
11년 전 조회 3,111
11년 전 조회 2,625
11년 전 조회 2,560