offset() 메서드를 이용하여 left, top 값 set, get 하기
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>
먼저 요소의 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>
게시판 목록
팁게시판
디자인과 관련된 유용한 정보를 공유하세요.
질문은 상단의 QA에서 해주시기 바랍니다.
질문은 상단의 QA에서 해주시기 바랍니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5527 |
돌아온깡통
|
11년 전 | 717 | |
| 5526 |
돌아온깡통
|
11년 전 | 642 | |
| 5525 |
돌아온깡통
|
11년 전 | 967 | |
| 5524 |
돌아온깡통
|
11년 전 | 834 | |
| 5523 |
돌아온깡통
|
11년 전 | 634 | |
| 5522 |
돌아온깡통
|
11년 전 | 789 | |
| 5521 |
돌아온깡통
|
11년 전 | 803 | |
| 5520 |
돌아온깡통
|
11년 전 | 819 | |
| 5519 |
돌아온깡통
|
11년 전 | 915 | |
| 5518 |
돌아온깡통
|
11년 전 | 562 | |
| 5517 |
돌아온깡통
|
11년 전 | 609 | |
| 5516 |
돌아온깡통
|
11년 전 | 787 | |
| 5515 |
돌아온깡통
|
11년 전 | 629 | |
| 5514 |
돌아온깡통
|
11년 전 | 659 | |
| 5513 |
돌아온깡통
|
11년 전 | 644 | |
| 5512 |
돌아온깡통
|
11년 전 | 565 | |
| 5511 |
돌아온깡통
|
11년 전 | 628 | |
| 5510 |
돌아온깡통
|
11년 전 | 1242 | |
| 5509 |
돌아온깡통
|
11년 전 | 573 | |
| 5508 |
돌아온깡통
|
11년 전 | 890 | |
| 5507 |
돌아온깡통
|
11년 전 | 788 | |
| 5506 |
돌아온깡통
|
11년 전 | 536 | |
| 5505 |
돌아온깡통
|
11년 전 | 559 | |
| 5504 |
돌아온깡통
|
11년 전 | 648 | |
| 5503 |
돌아온깡통
|
11년 전 | 650 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기