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

· 11년 전 · 3118
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년 전 조회 1,075
11년 전 조회 3,452
11년 전 조회 2,658
11년 전 조회 3,208
11년 전 조회 706
11년 전 조회 2,480
11년 전 조회 2,452
11년 전 조회 2,525
11년 전 조회 1,509
11년 전 조회 1,991
11년 전 조회 1,316
11년 전 조회 1,116
11년 전 조회 1,701
11년 전 조회 1,018
11년 전 조회 3,908
11년 전 조회 3,683
11년 전 조회 1,315
11년 전 조회 2,556
11년 전 조회 969
11년 전 조회 1,775
11년 전 조회 3,385
11년 전 조회 3,687
11년 전 조회 4,612
11년 전 조회 1,001
11년 전 조회 1,564
11년 전 조회 2,968
11년 전 조회 1,147
11년 전 조회 1,140
11년 전 조회 1,753
11년 전 조회 1,010
11년 전 조회 2,286
11년 전 조회 1,788
11년 전 조회 3,872
11년 전 조회 2,326
11년 전 조회 4,572
11년 전 조회 1,343
11년 전 조회 1,214
11년 전 조회 1,853
11년 전 조회 1,820
11년 전 조회 1,391
11년 전 조회 1,039
11년 전 조회 1,676
11년 전 조회 1,057
11년 전 조회 1,163
11년 전 조회 1,370
11년 전 조회 1,190
11년 전 조회 939
11년 전 조회 2,127
11년 전 조회 1,951
11년 전 조회 3,119
11년 전 조회 1,090
11년 전 조회 847
11년 전 조회 947
11년 전 조회 2,821
11년 전 조회 1,070
11년 전 조회 1,267
11년 전 조회 789
11년 전 조회 1,570
11년 전 조회 1,555
11년 전 조회 967
11년 전 조회 1,143
11년 전 조회 832
11년 전 조회 788
11년 전 조회 1,616
11년 전 조회 950
11년 전 조회 847
11년 전 조회 966
11년 전 조회 1,127
11년 전 조회 798
11년 전 조회 843
11년 전 조회 1,324
11년 전 조회 884
11년 전 조회 1,349
11년 전 조회 873
11년 전 조회 1,008
11년 전 조회 1,067
11년 전 조회 813
11년 전 조회 839
11년 전 조회 1,065
11년 전 조회 2,000
11년 전 조회 847
11년 전 조회 859
11년 전 조회 789
11년 전 조회 1,214
11년 전 조회 863
11년 전 조회 777
11년 전 조회 1,085
11년 전 조회 1,430
11년 전 조회 1,061
11년 전 조회 985
11년 전 조회 1,031
11년 전 조회 1,867
11년 전 조회 931
11년 전 조회 990
11년 전 조회 2,769
11년 전 조회 2,312
11년 전 조회 1,187
11년 전 조회 1,680
11년 전 조회 1,291
11년 전 조회 1,224
🐛 버그신고