3

I found the following creative accepted answer

jQuery('.get-close-to').hover(function() {
    var offset = jQuery(this).css('offset');
    alert( 'Left: ' + offset.left + '\nTop: ' + offset.top );
});

I wanted to know how .css('offset') is working so I made a jsfiddle but it's alerting "undefined".

Can anyone describes about this, working and is correct way?


Comment:


I know to use .offset() but I don't mean to use this, but my question regards how the accepted answer's code is working ....... with .css('offset')? That's all.

3
  • offset is null which causes offset.left tobe undefined Commented Aug 25, 2013 at 9:14
  • 1
    I have a hard time believing that the answer you link to actually works or was tested. Yet the people asking and answering the question don't seem to be new to SO over anything (although in 2009, when the question was asked, who knows). Just knowing what $.css() returns (a jQuery object), there is no way you're going to have a left or right property on that. Interested to see if I'm missing something here. Commented Aug 25, 2013 at 9:43
  • This is an obvious bug. There is no CSS property called "offset" so the code will fail as you demonstrated. Commented Aug 25, 2013 at 9:46

4 Answers 4

3

There's no offset property in CSS. with jQuery.css(propertyName) you can only access properties that exist. Everything else will return null.

for example:

jQuery.css('myImaginativePropertyname'); // returns null
jQuery.css('border'); // would return '0px none rgb(0, 0, 0)'

However

You can access the event.target (DOM element) like this:

jQuery('.get-close-to').hover(function(e) {
    var elem = e.target;
    alert( 'Left: ' + elem.offsetLeft + '\nTop: ' + elem.offsetTop );
}, function(e){});

I added the second function so that the code won't be executed twice. If you have only single function as input on jQuery.hover(), it will execute both in hover and blur. If you add a second function as a parameter, the first one will be executed on hover, while the second will be executed on blur of the element.

Here's the fiddle: http://jsfiddle.net/JLAK4/2/

Some people may argue to use jQuery(this).offset() instead, but why waste cpu cycles for yet another method call while you already have your DOM element populated and at your disposal? jQuery is a nice compatibility layer, I give you that. But abusing and overusing it makes no sense at all.

Sign up to request clarification or add additional context in comments.

1 Comment

Like the comment on not using $.offset() if you're just going to read the current positioning properties of the element, at least in this particular example / the question fiddle. Does anyone know how/if SO handles "broken" answers like the one we're dealing with in the question? It may have worked at the time, but as @MarcAudet wrote, could have been some sort of bug.
0

Do you want to be using .offset() instead?

jQuery('.get-close-to').hover(function() {
    var offset = jQuery(this).offset();
    alert( 'Left: ' + offset.left + '\nTop: ' + offset.top );
});

1 Comment

I know the offset() to use. But I think you misread the question.
0

Try this:

jQuery('.get-close-to').hover(function() {
    var offset = jQuery(this).offset();
    alert( 'Left: ' + offset.left + '\nTop: ' + offset.top );
});

3 Comments

Yeah, we could do that, but I think you misread the question.
@C-Link I know you said that for whatever reason you don't want to use .offset(). But as your demo has demonstrated, and the many comments and answers show that .css('offset') cannot work as it's not a CSS property. So .offset() is the closest you can get. Maybe it worked in an older version of jQuery.
@C-Link: I agree with Andy. CSS doesn't have an offset property.
0

I think he wanted to say like this: working fiddle

jQuery('.get-close-to').hover(function() {
    var offset = jQuery(this).offset();
    alert( 'Left: ' + offset.left + '\nTop: ' + offset.top );
});

But mistakenly he has typed .css('offset') may be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.