COMING SOON 🚀

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

· 11년 전 · 4279
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,162
11년 전 조회 4,549
11년 전 조회 3,815
11년 전 조회 4,300
11년 전 조회 1,828
11년 전 조회 3,627
11년 전 조회 3,617
11년 전 조회 3,701
11년 전 조회 2,678
11년 전 조회 3,128
11년 전 조회 2,477
11년 전 조회 2,254
11년 전 조회 2,891
11년 전 조회 2,191
11년 전 조회 5,057
11년 전 조회 4,809
11년 전 조회 2,464
11년 전 조회 3,703
11년 전 조회 2,118
11년 전 조회 2,877
11년 전 조회 4,503
11년 전 조회 4,826
11년 전 조회 5,735
11년 전 조회 2,103
11년 전 조회 2,691
11년 전 조회 4,134
11년 전 조회 2,280
11년 전 조회 2,283
11년 전 조회 2,865
11년 전 조회 2,162
11년 전 조회 3,400
11년 전 조회 2,935
11년 전 조회 4,995
11년 전 조회 3,434
11년 전 조회 5,719
11년 전 조회 2,510
11년 전 조회 2,363
11년 전 조회 3,042
11년 전 조회 3,014
11년 전 조회 2,559
11년 전 조회 2,188
11년 전 조회 2,816
11년 전 조회 2,253
11년 전 조회 2,314
11년 전 조회 2,548
11년 전 조회 2,387
11년 전 조회 2,086
11년 전 조회 3,294
11년 전 조회 3,112
11년 전 조회 4,280
11년 전 조회 2,288
11년 전 조회 2,041
11년 전 조회 2,130
11년 전 조회 4,013
11년 전 조회 2,230
11년 전 조회 2,451
11년 전 조회 1,985
11년 전 조회 2,700
11년 전 조회 2,718
11년 전 조회 2,132
11년 전 조회 2,308
11년 전 조회 1,993
11년 전 조회 1,938
11년 전 조회 2,748
11년 전 조회 2,087
11년 전 조회 2,011
11년 전 조회 2,160
11년 전 조회 2,264
11년 전 조회 1,948
11년 전 조회 2,020
11년 전 조회 2,456
11년 전 조회 2,072
11년 전 조회 2,449
11년 전 조회 2,096
11년 전 조회 2,147
11년 전 조회 2,243
11년 전 조회 2,001
11년 전 조회 2,033
11년 전 조회 2,232
11년 전 조회 3,136
11년 전 조회 2,063
11년 전 조회 2,069
11년 전 조회 1,975
11년 전 조회 2,422
11년 전 조회 2,063
11년 전 조회 1,969
11년 전 조회 2,298
11년 전 조회 2,631
11년 전 조회 2,288
11년 전 조회 2,204
11년 전 조회 2,235
11년 전 조회 3,101
11년 전 조회 2,142
11년 전 조회 2,222
11년 전 조회 3,956
11년 전 조회 3,542
11년 전 조회 2,421
11년 전 조회 2,942
11년 전 조회 2,480
11년 전 조회 2,396