/    Sign up×
Community /Pin to ProfileBookmark

Offset VS Position (Jquery again)

[CODE]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Test</title>

<script src=”//code.jquery.com/jquery-1.11.0.min.js”></script>

<style>
*{
box-sizing:border-box
}
body{
padding: 0;
margin: 0;
}
div{
height: 100px;
padding: 15px;
width: 250px;
}

div:nth-child(odd){
box-shadow: inset 1px 1px 10px 1px rgba(0,0,0,.5);
}

div:nth-child(even){
background-color: #eee;
box-shadow: 1px 1px 10px 1px rgba(0,0,0,.5);
}

#find{
margin-left: 250px;
}

</style>

</head>
<body>

<div>

</div>

<div id=”find”>

</div>

<script type=”text/javascript”>

$(document).ready(function(){
var temp = $(‘#find’).offset().left;
console.log(‘offset is ‘ + temp);
});

</script>

</body>
</html>
[/CODE]

when i am using the offset() method it correctly logs “offset is 250” , (check the above snippet)

when i use the position() method i correctly or incorrectly logs ‘offset is 0’ , .. why ??? (check the code below)

[CODE]

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Test</title>

<script src=”//code.jquery.com/jquery-1.11.0.min.js”></script>

<style>
*{
box-sizing:border-box
}
body{
padding: 0;
margin: 0;
}
div{
height: 100px;
padding: 15px;
width: 250px;
}

div:nth-child(odd){
box-shadow: inset 1px 1px 10px 1px rgba(0,0,0,.5);
}

div:nth-child(even){
background-color: #eee;
box-shadow: 1px 1px 10px 1px rgba(0,0,0,.5);
}

#find{
margin-left: 250px;
}

</style>

</head>
<body>

<div>

</div>

<div id=”find”>

</div>

<script type=”text/javascript”>

$(document).ready(function(){
var temp = $(‘#find’).position().left;
console.log(‘position is ‘ + temp);
});

</script>

</body>
</html>
[/CODE]

the Jquery documentation says the following about the position() property :

[QUOTE]

The .position() method allows us to retrieve the current position of an element [COLOR=”#FF0000″]relative to the offset[/COLOR] parent. Contrast this with .offset(), which retrieves the current position relative to the document. When positioning a new element near another one and within the same containing DOM element, .position() is the more useful.

[/QUOTE]

[URL=”http://api.jquery.com/position/”]http://api.jquery.com/position/[/URL]

when i read

[QUOTE]

relative to the offset

[/QUOTE]

i just don’t get it , can somebody explain in a couple of sentences wht am i missing ??

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowOct 31.2014 — You removed a word from your understanding... It's "relative to the offset [b]parent[/b]" -- though that's the type of "let's leave out some grammar" crap I expect from halfwit bloated nonsense like jQuery.

Simple change to the wording:

"relative to the offset of the parent element"

Makes it clear as day.

.offset() is the relative to BODY's upper right corner, .position() is relative to the parent element's upper right corner.

The problem here is, you have no non-body parent element, so it's reporting off itself.

Really in either case it's an excellent example of just why jQuery is such idiotic bull, since there's SO much wrong with Element.getBoundingClientRect(), Element.offsetLeft and Element.offsetTop or they had to go and make a new methods to replicate the exact same behaviors :/

NOT that there's much legitimate reason to be reading either value unless you're doing something that has no business on a website in the first place!
×

Success!

Help @gautamz07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...