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

· 11년 전 · 3991
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,823
11년 전 조회 4,222
11년 전 조회 3,474
11년 전 조회 3,965
11년 전 조회 1,463
11년 전 조회 3,267
11년 전 조회 3,278
11년 전 조회 3,331
11년 전 조회 2,316
11년 전 조회 2,782
11년 전 조회 2,120
11년 전 조회 1,911
11년 전 조회 2,514
11년 전 조회 1,812
11년 전 조회 4,708
11년 전 조회 4,475
11년 전 조회 2,128
11년 전 조회 3,372
11년 전 조회 1,763
11년 전 조회 2,573
11년 전 조회 4,171
11년 전 조회 4,478
11년 전 조회 5,402
11년 전 조회 1,751
11년 전 조회 2,345
11년 전 조회 3,754
11년 전 조회 1,934
11년 전 조회 1,937
11년 전 조회 2,516
11년 전 조회 1,816
11년 전 조회 3,036
11년 전 조회 2,573
11년 전 조회 4,630
11년 전 조회 3,079
11년 전 조회 5,345
11년 전 조회 2,135
11년 전 조회 2,023
11년 전 조회 2,680
11년 전 조회 2,638
11년 전 조회 2,202
11년 전 조회 1,794
11년 전 조회 2,469
11년 전 조회 1,972
11년 전 조회 1,963
11년 전 조회 2,162
11년 전 조회 2,072
11년 전 조회 1,732
11년 전 조회 2,906
11년 전 조회 2,860
11년 전 조회 3,992
11년 전 조회 2,004
11년 전 조회 1,756
11년 전 조회 1,752
11년 전 조회 3,668
11년 전 조회 1,887
11년 전 조회 2,206
11년 전 조회 1,724
11년 전 조회 2,376
11년 전 조회 2,358
11년 전 조회 1,770
11년 전 조회 1,942
11년 전 조회 1,771
11년 전 조회 1,700
11년 전 조회 2,411
11년 전 조회 1,834
11년 전 조회 1,756
11년 전 조회 1,915
11년 전 조회 1,904
11년 전 조회 1,608
11년 전 조회 1,795
11년 전 조회 2,185
11년 전 조회 1,825
11년 전 조회 2,098
11년 전 조회 1,823
11년 전 조회 1,800
11년 전 조회 2,004
11년 전 조회 1,751
11년 전 조회 1,777
11년 전 조회 1,873
11년 전 조회 2,782
11년 전 조회 1,801
11년 전 조회 1,809
11년 전 조회 1,626
11년 전 조회 2,067
11년 전 조회 1,793
11년 전 조회 1,614
11년 전 조회 2,055
11년 전 조회 2,272
11년 전 조회 2,034
11년 전 조회 1,879
11년 전 조회 1,990
11년 전 조회 2,859
11년 전 조회 1,789
11년 전 조회 1,847
11년 전 조회 3,624
11년 전 조회 3,292
11년 전 조회 2,170
11년 전 조회 2,673
11년 전 조회 2,123
11년 전 조회 2,066