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

· 11년 전 · 4433
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,368
11년 전 조회 4,731
11년 전 조회 3,990
11년 전 조회 4,469
11년 전 조회 2,008
11년 전 조회 3,787
11년 전 조회 3,771
11년 전 조회 3,847
11년 전 조회 2,840
11년 전 조회 3,298
11년 전 조회 2,646
11년 전 조회 2,433
11년 전 조회 3,057
11년 전 조회 2,357
11년 전 조회 5,233
11년 전 조회 4,866
11년 전 조회 2,614
11년 전 조회 3,851
11년 전 조회 2,288
11년 전 조회 3,049
11년 전 조회 4,681
11년 전 조회 5,005
11년 전 조회 5,908
11년 전 조회 2,281
11년 전 조회 2,846
11년 전 조회 4,283
11년 전 조회 2,457
11년 전 조회 2,455
11년 전 조회 3,038
11년 전 조회 2,323
11년 전 조회 3,581
11년 전 조회 3,093
11년 전 조회 5,185
11년 전 조회 3,599
11년 전 조회 5,871
11년 전 조회 2,694
11년 전 조회 2,515
11년 전 조회 3,200
11년 전 조회 3,158
11년 전 조회 2,703
11년 전 조회 2,357
11년 전 조회 2,986
11년 전 조회 2,399
11년 전 조회 2,478
11년 전 조회 2,710
11년 전 조회 2,549
11년 전 조회 2,241
11년 전 조회 3,465
11년 전 조회 3,275
11년 전 조회 4,434
11년 전 조회 2,437
11년 전 조회 2,201
11년 전 조회 2,283
11년 전 조회 4,176
11년 전 조회 2,379
11년 전 조회 2,627
11년 전 조회 2,146
11년 전 조회 2,854
11년 전 조회 2,871
11년 전 조회 2,293
11년 전 조회 2,496
11년 전 조회 2,157
11년 전 조회 2,092
11년 전 조회 2,892
11년 전 조회 2,276
11년 전 조회 2,162
11년 전 조회 2,326
11년 전 조회 2,405
11년 전 조회 2,104
11년 전 조회 2,193
11년 전 조회 2,590
11년 전 조회 2,252
11년 전 조회 2,587
11년 전 조회 2,289
11년 전 조회 2,290
11년 전 조회 2,410
11년 전 조회 2,174
11년 전 조회 2,199
11년 전 조회 2,397
11년 전 조회 3,273
11년 전 조회 2,215
11년 전 조회 2,230
11년 전 조회 2,105
11년 전 조회 2,577
11년 전 조회 2,216
11년 전 조회 2,131
11년 전 조회 2,456
11년 전 조회 2,771
11년 전 조회 2,444
11년 전 조회 2,358
11년 전 조회 2,383
11년 전 조회 3,250
11년 전 조회 2,285
11년 전 조회 2,362
11년 전 조회 4,017
11년 전 조회 3,689
11년 전 조회 2,567
11년 전 조회 3,101
11년 전 조회 2,610
11년 전 조회 2,550