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

· 11년 전 · 4401
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,331
11년 전 조회 4,697
11년 전 조회 3,947
11년 전 조회 4,440
11년 전 조회 1,979
11년 전 조회 3,758
11년 전 조회 3,739
11년 전 조회 3,817
11년 전 조회 2,820
11년 전 조회 3,266
11년 전 조회 2,616
11년 전 조회 2,395
11년 전 조회 3,031
11년 전 조회 2,322
11년 전 조회 5,195
11년 전 조회 4,857
11년 전 조회 2,586
11년 전 조회 3,825
11년 전 조회 2,251
11년 전 조회 3,009
11년 전 조회 4,640
11년 전 조회 4,965
11년 전 조회 5,871
11년 전 조회 2,252
11년 전 조회 2,810
11년 전 조회 4,257
11년 전 조회 2,429
11년 전 조회 2,424
11년 전 조회 3,007
11년 전 조회 2,293
11년 전 조회 3,543
11년 전 조회 3,064
11년 전 조회 5,147
11년 전 조회 3,563
11년 전 조회 5,834
11년 전 조회 2,645
11년 전 조회 2,489
11년 전 조회 3,170
11년 전 조회 3,136
11년 전 조회 2,674
11년 전 조회 2,316
11년 전 조회 2,957
11년 전 조회 2,368
11년 전 조회 2,448
11년 전 조회 2,681
11년 전 조회 2,507
11년 전 조회 2,216
11년 전 조회 3,431
11년 전 조회 3,241
11년 전 조회 4,402
11년 전 조회 2,409
11년 전 조회 2,163
11년 전 조회 2,255
11년 전 조회 4,150
11년 전 조회 2,353
11년 전 조회 2,585
11년 전 조회 2,113
11년 전 조회 2,823
11년 전 조회 2,840
11년 전 조회 2,260
11년 전 조회 2,465
11년 전 조회 2,120
11년 전 조회 2,054
11년 전 조회 2,859
11년 전 조회 2,232
11년 전 조회 2,124
11년 전 조회 2,283
11년 전 조회 2,374
11년 전 조회 2,060
11년 전 조회 2,151
11년 전 조회 2,569
11년 전 조회 2,215
11년 전 조회 2,557
11년 전 조회 2,239
11년 전 조회 2,249
11년 전 조회 2,365
11년 전 조회 2,131
11년 전 조회 2,163
11년 전 조회 2,360
11년 전 조회 3,242
11년 전 조회 2,176
11년 전 조회 2,188
11년 전 조회 2,080
11년 전 조회 2,541
11년 전 조회 2,182
11년 전 조회 2,100
11년 전 조회 2,417
11년 전 조회 2,742
11년 전 조회 2,408
11년 전 조회 2,325
11년 전 조회 2,340
11년 전 조회 3,220
11년 전 조회 2,256
11년 전 조회 2,330
11년 전 조회 4,008
11년 전 조회 3,660
11년 전 조회 2,538
11년 전 조회 3,061
11년 전 조회 2,582
11년 전 조회 2,506