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

· 11년 전 · 4529
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,439
11년 전 조회 4,801
11년 전 조회 4,066
11년 전 조회 4,548
11년 전 조회 2,100
11년 전 조회 3,880
11년 전 조회 3,857
11년 전 조회 3,924
11년 전 조회 2,937
11년 전 조회 3,382
11년 전 조회 2,724
11년 전 조회 2,508
11년 전 조회 3,143
11년 전 조회 2,447
11년 전 조회 5,316
11년 전 조회 4,910
11년 전 조회 2,719
11년 전 조회 3,931
11년 전 조회 2,369
11년 전 조회 3,129
11년 전 조회 4,781
11년 전 조회 5,095
11년 전 조회 6,006
11년 전 조회 2,371
11년 전 조회 2,947
11년 전 조회 4,373
11년 전 조회 2,546
11년 전 조회 2,540
11년 전 조회 3,120
11년 전 조회 2,426
11년 전 조회 3,669
11년 전 조회 3,184
11년 전 조회 5,279
11년 전 조회 3,678
11년 전 조회 5,969
11년 전 조회 2,790
11년 전 조회 2,595
11년 전 조회 3,282
11년 전 조회 3,259
11년 전 조회 2,798
11년 전 조회 2,437
11년 전 조회 3,083
11년 전 조회 2,487
11년 전 조회 2,577
11년 전 조회 2,805
11년 전 조회 2,626
11년 전 조회 2,341
11년 전 조회 3,571
11년 전 조회 3,357
11년 전 조회 4,530
11년 전 조회 2,534
11년 전 조회 2,292
11년 전 조회 2,381
11년 전 조회 4,272
11년 전 조회 2,479
11년 전 조회 2,736
11년 전 조회 2,259
11년 전 조회 2,958
11년 전 조회 2,970
11년 전 조회 2,383
11년 전 조회 2,592
11년 전 조회 2,266
11년 전 조회 2,207
11년 전 조회 2,992
11년 전 조회 2,374
11년 전 조회 2,268
11년 전 조회 2,423
11년 전 조회 2,482
11년 전 조회 2,216
11년 전 조회 2,282
11년 전 조회 2,698
11년 전 조회 2,372
11년 전 조회 2,688
11년 전 조회 2,377
11년 전 조회 2,389
11년 전 조회 2,512
11년 전 조회 2,272
11년 전 조회 2,322
11년 전 조회 2,478
11년 전 조회 3,373
11년 전 조회 2,332
11년 전 조회 2,332
11년 전 조회 2,211
11년 전 조회 2,676
11년 전 조회 2,339
11년 전 조회 2,223
11년 전 조회 2,573
11년 전 조회 2,869
11년 전 조회 2,553
11년 전 조회 2,464
11년 전 조회 2,485
11년 전 조회 3,352
11년 전 조회 2,387
11년 전 조회 2,455
11년 전 조회 4,051
11년 전 조회 3,792
11년 전 조회 2,669
11년 전 조회 3,195
11년 전 조회 2,722
11년 전 조회 2,649