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에서 해주시기 바랍니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5602 |
잘살아보자
|
11년 전 | 3056 | |
| 5601 |
잘살아보자
|
11년 전 | 2392 | |
| 5600 |
잘살아보자
|
11년 전 | 5717 | |
| 5599 |
잘살아보자
|
11년 전 | 3370 | |
| 5598 |
잘살아보자
|
11년 전 | 1804 | |
| 5597 |
잘살아보자
|
11년 전 | 2070 | |
| 5596 |
잘살아보자
|
11년 전 | 1323 | |
| 5595 |
잘살아보자
|
11년 전 | 1287 | |
| 5594 |
잘살아보자
|
11년 전 | 1445 | |
| 5593 | 11년 전 | 2490 | ||
| 5592 |
잘살아보자
|
11년 전 | 2311 | |
| 5591 |
잘살아보자
|
11년 전 | 2323 | |
| 5590 |
잘살아보자
|
11년 전 | 2411 | |
| 5589 |
잘살아보자
|
11년 전 | 1394 | |
| 5588 |
잘살아보자
|
11년 전 | 1874 | |
| 5587 |
잘살아보자
|
11년 전 | 1210 | |
| 5586 |
잘살아보자
|
11년 전 | 1019 | |
| 5585 | 11년 전 | 3552 | ||
| 5584 | 11년 전 | 1161 | ||
| 5583 | 11년 전 | 2398 | ||
| 5582 | 11년 전 | 1431 | ||
| 5581 | 11년 전 | 2845 | ||
| 5580 | 11년 전 | 1712 | ||
| 5579 | 11년 전 | 1679 | ||
| 5578 | 11년 전 | 1247 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기