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

· 11년 전 · 4415
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,351
11년 전 조회 4,715
11년 전 조회 3,965
11년 전 조회 4,453
11년 전 조회 1,994
11년 전 조회 3,773
11년 전 조회 3,754
11년 전 조회 3,835
11년 전 조회 2,828
11년 전 조회 3,279
11년 전 조회 2,629
11년 전 조회 2,413
11년 전 조회 3,048
11년 전 조회 2,341
11년 전 조회 5,209
11년 전 조회 4,863
11년 전 조회 2,598
11년 전 조회 3,838
11년 전 조회 2,266
11년 전 조회 3,024
11년 전 조회 4,658
11년 전 조회 4,984
11년 전 조회 5,888
11년 전 조회 2,265
11년 전 조회 2,825
11년 전 조회 4,271
11년 전 조회 2,440
11년 전 조회 2,442
11년 전 조회 3,023
11년 전 조회 2,306
11년 전 조회 3,555
11년 전 조회 3,077
11년 전 조회 5,161
11년 전 조회 3,578
11년 전 조회 5,850
11년 전 조회 2,667
11년 전 조회 2,504
11년 전 조회 3,185
11년 전 조회 3,146
11년 전 조회 2,686
11년 전 조회 2,334
11년 전 조회 2,971
11년 전 조회 2,381
11년 전 조회 2,461
11년 전 조회 2,700
11년 전 조회 2,526
11년 전 조회 2,225
11년 전 조회 3,450
11년 전 조회 3,257
11년 전 조회 4,416
11년 전 조회 2,419
11년 전 조회 2,189
11년 전 조회 2,267
11년 전 조회 4,164
11년 전 조회 2,367
11년 전 조회 2,607
11년 전 조회 2,128
11년 전 조회 2,839
11년 전 조회 2,858
11년 전 조회 2,278
11년 전 조회 2,481
11년 전 조회 2,140
11년 전 조회 2,073
11년 전 조회 2,877
11년 전 조회 2,247
11년 전 조회 2,143
11년 전 조회 2,305
11년 전 조회 2,388
11년 전 조회 2,082
11년 전 조회 2,170
11년 전 조회 2,579
11년 전 조회 2,236
11년 전 조회 2,571
11년 전 조회 2,264
11년 전 조회 2,263
11년 전 조회 2,388
11년 전 조회 2,153
11년 전 조회 2,176
11년 전 조회 2,377
11년 전 조회 3,255
11년 전 조회 2,191
11년 전 조회 2,205
11년 전 조회 2,092
11년 전 조회 2,561
11년 전 조회 2,197
11년 전 조회 2,113
11년 전 조회 2,435
11년 전 조회 2,756
11년 전 조회 2,424
11년 전 조회 2,343
11년 전 조회 2,361
11년 전 조회 3,233
11년 전 조회 2,272
11년 전 조회 2,349
11년 전 조회 4,013
11년 전 조회 3,677
11년 전 조회 2,551
11년 전 조회 3,081
11년 전 조회 2,596
11년 전 조회 2,527