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

· 11년 전 · 4528
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,438
11년 전 조회 4,799
11년 전 조회 4,065
11년 전 조회 4,547
11년 전 조회 2,099
11년 전 조회 3,878
11년 전 조회 3,855
11년 전 조회 3,923
11년 전 조회 2,937
11년 전 조회 3,382
11년 전 조회 2,723
11년 전 조회 2,508
11년 전 조회 3,140
11년 전 조회 2,447
11년 전 조회 5,316
11년 전 조회 4,910
11년 전 조회 2,719
11년 전 조회 3,931
11년 전 조회 2,368
11년 전 조회 3,128
11년 전 조회 4,777
11년 전 조회 5,095
11년 전 조회 6,006
11년 전 조회 2,370
11년 전 조회 2,947
11년 전 조회 4,373
11년 전 조회 2,542
11년 전 조회 2,539
11년 전 조회 3,120
11년 전 조회 2,425
11년 전 조회 3,667
11년 전 조회 3,182
11년 전 조회 5,276
11년 전 조회 3,678
11년 전 조회 5,968
11년 전 조회 2,789
11년 전 조회 2,594
11년 전 조회 3,281
11년 전 조회 3,259
11년 전 조회 2,798
11년 전 조회 2,435
11년 전 조회 3,082
11년 전 조회 2,486
11년 전 조회 2,577
11년 전 조회 2,801
11년 전 조회 2,626
11년 전 조회 2,341
11년 전 조회 3,570
11년 전 조회 3,357
11년 전 조회 4,529
11년 전 조회 2,534
11년 전 조회 2,291
11년 전 조회 2,381
11년 전 조회 4,269
11년 전 조회 2,478
11년 전 조회 2,734
11년 전 조회 2,257
11년 전 조회 2,958
11년 전 조회 2,967
11년 전 조회 2,383
11년 전 조회 2,589
11년 전 조회 2,266
11년 전 조회 2,206
11년 전 조회 2,991
11년 전 조회 2,371
11년 전 조회 2,267
11년 전 조회 2,422
11년 전 조회 2,481
11년 전 조회 2,216
11년 전 조회 2,278
11년 전 조회 2,697
11년 전 조회 2,371
11년 전 조회 2,688
11년 전 조회 2,375
11년 전 조회 2,389
11년 전 조회 2,511
11년 전 조회 2,272
11년 전 조회 2,322
11년 전 조회 2,477
11년 전 조회 3,373
11년 전 조회 2,330
11년 전 조회 2,332
11년 전 조회 2,211
11년 전 조회 2,676
11년 전 조회 2,339
11년 전 조회 2,223
11년 전 조회 2,573
11년 전 조회 2,868
11년 전 조회 2,553
11년 전 조회 2,464
11년 전 조회 2,485
11년 전 조회 3,351
11년 전 조회 2,387
11년 전 조회 2,455
11년 전 조회 4,050
11년 전 조회 3,792
11년 전 조회 2,669
11년 전 조회 3,195
11년 전 조회 2,722
11년 전 조회 2,648