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>
게시글 목록
| 번호 | 제목 |
|---|---|
| 11832 |
jQuery
remove()를 이용하여 엘리먼트 삭제하기
|
| 11829 | |
| 11826 |
jQuery
동일 엘리먼트 순번 알아내기
2
|
| 11823 | |
| 11822 | |
| 11821 | |
| 11820 |
jQuery
position() 메서드 객체에 대한 좌표값
|
| 11819 | |
| 20318 | |
| 11818 | |
| 11817 | |
| 11816 | |
| 11814 | |
| 11813 | |
| 11811 | |
| 11810 |
jQuery
class가 있는지 없는지 체크하기
|
| 11809 |
PHP
배열에서 중복 값 없애기
|
| 11808 | |
| 11807 |
JavaScript
str_pad 자릿수만큼 특정문자로 채우기
|
| 11806 | |
| 11805 | |
| 11804 | |
| 11803 | |
| 11802 | |
| 11801 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기