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

· 11년 전 · 4575
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,493
11년 전 조회 4,847
11년 전 조회 4,120
11년 전 조회 4,595
11년 전 조회 2,147
11년 전 조회 3,942
11년 전 조회 3,918
11년 전 조회 3,978
11년 전 조회 2,987
11년 전 조회 3,428
11년 전 조회 2,783
11년 전 조회 2,549
11년 전 조회 3,200
11년 전 조회 2,499
11년 전 조회 5,362
11년 전 조회 4,924
11년 전 조회 2,779
11년 전 조회 3,992
11년 전 조회 2,420
11년 전 조회 3,177
11년 전 조회 4,814
11년 전 조회 5,147
11년 전 조회 6,049
11년 전 조회 2,431
11년 전 조회 2,994
11년 전 조회 4,419
11년 전 조회 2,597
11년 전 조회 2,578
11년 전 조회 3,180
11년 전 조회 2,484
11년 전 조회 3,722
11년 전 조회 3,226
11년 전 조회 5,315
11년 전 조회 3,728
11년 전 조회 6,014
11년 전 조회 2,839
11년 전 조회 2,640
11년 전 조회 3,327
11년 전 조회 3,301
11년 전 조회 2,848
11년 전 조회 2,487
11년 전 조회 3,141
11년 전 조회 2,538
11년 전 조회 2,627
11년 전 조회 2,861
11년 전 조회 2,670
11년 전 조회 2,386
11년 전 조회 3,613
11년 전 조회 3,396
11년 전 조회 4,576
11년 전 조회 2,587
11년 전 조회 2,334
11년 전 조회 2,433
11년 전 조회 4,327
11년 전 조회 2,527
11년 전 조회 2,781
11년 전 조회 2,316
11년 전 조회 3,003
11년 전 조회 3,012
11년 전 조회 2,426
11년 전 조회 2,646
11년 전 조회 2,317
11년 전 조회 2,254
11년 전 조회 3,034
11년 전 조회 2,416
11년 전 조회 2,319
11년 전 조회 2,481
11년 전 조회 2,534
11년 전 조회 2,258
11년 전 조회 2,325
11년 전 조회 2,735
11년 전 조회 2,426
11년 전 조회 2,748
11년 전 조회 2,429
11년 전 조회 2,426
11년 전 조회 2,561
11년 전 조회 2,328
11년 전 조회 2,372
11년 전 조회 2,524
11년 전 조회 3,430
11년 전 조회 2,384
11년 전 조회 2,393
11년 전 조회 2,266
11년 전 조회 2,736
11년 전 조회 2,389
11년 전 조회 2,263
11년 전 조회 2,625
11년 전 조회 2,911
11년 전 조회 2,598
11년 전 조회 2,524
11년 전 조회 2,528
11년 전 조회 3,406
11년 전 조회 2,443
11년 전 조회 2,504
11년 전 조회 4,066
11년 전 조회 3,844
11년 전 조회 2,713
11년 전 조회 3,238
11년 전 조회 2,764
11년 전 조회 2,708