COMING SOON 🚀

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

· 11년 전 · 4315
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,206
11년 전 조회 4,588
11년 전 조회 3,848
11년 전 조회 4,343
11년 전 조회 1,869
11년 전 조회 3,666
11년 전 조회 3,653
11년 전 조회 3,737
11년 전 조회 2,713
11년 전 조회 3,164
11년 전 조회 2,516
11년 전 조회 2,293
11년 전 조회 2,929
11년 전 조회 2,214
11년 전 조회 5,092
11년 전 조회 4,829
11년 전 조회 2,504
11년 전 조회 3,733
11년 전 조회 2,147
11년 전 조회 2,907
11년 전 조회 4,535
11년 전 조회 4,866
11년 전 조회 5,776
11년 전 조회 2,140
11년 전 조회 2,714
11년 전 조회 4,170
11년 전 조회 2,318
11년 전 조회 2,322
11년 전 조회 2,900
11년 전 조회 2,202
11년 전 조회 3,435
11년 전 조회 2,963
11년 전 조회 5,037
11년 전 조회 3,465
11년 전 조회 5,740
11년 전 조회 2,541
11년 전 조회 2,400
11년 전 조회 3,065
11년 전 조회 3,041
11년 전 조회 2,584
11년 전 조회 2,222
11년 전 조회 2,840
11년 전 조회 2,287
11년 전 조회 2,350
11년 전 조회 2,585
11년 전 조회 2,417
11년 전 조회 2,124
11년 전 조회 3,330
11년 전 조회 3,150
11년 전 조회 4,316
11년 전 조회 2,320
11년 전 조회 2,080
11년 전 조회 2,162
11년 전 조회 4,050
11년 전 조회 2,257
11년 전 조회 2,488
11년 전 조회 2,017
11년 전 조회 2,730
11년 전 조회 2,745
11년 전 조회 2,158
11년 전 조회 2,350
11년 전 조회 2,027
11년 전 조회 1,976
11년 전 조회 2,785
11년 전 조회 2,125
11년 전 조회 2,042
11년 전 조회 2,188
11년 전 조회 2,293
11년 전 조회 1,984
11년 전 조회 2,054
11년 전 조회 2,488
11년 전 조회 2,107
11년 전 조회 2,477
11년 전 조회 2,134
11년 전 조회 2,174
11년 전 조회 2,286
11년 전 조회 2,030
11년 전 조회 2,070
11년 전 조회 2,266
11년 전 조회 3,163
11년 전 조회 2,093
11년 전 조회 2,099
11년 전 조회 1,991
11년 전 조회 2,453
11년 전 조회 2,097
11년 전 조회 2,004
11년 전 조회 2,320
11년 전 조회 2,661
11년 전 조회 2,326
11년 전 조회 2,232
11년 전 조회 2,260
11년 전 조회 3,131
11년 전 조회 2,178
11년 전 조회 2,250
11년 전 조회 3,977
11년 전 조회 3,567
11년 전 조회 2,452
11년 전 조회 2,978
11년 전 조회 2,500
11년 전 조회 2,417