When hovering over a span I would like to get the offsetLeft and offsetTop values so I can make something hover near it. When I do this I get 0 for both values.
What is a better way to tackle this? I am using jQuery.
Assume I am starting with (looped by server-side scripting):
<span onmouseover="hoverNearMe(this.offsetLeft,this.offsetTop);">some username</span><br />
FINAL THOUGHTS:
I'm giving the the answer rep out based on "code leverage"/DRY.
The longer function you could use over and over in your own js library.
The second short answer however is 100% correct too.
$(this).offset().left
and $(this).offset().top
I think you should be able to do this:
HTML
<span class="get-close-to">some username</span><br />
jQuery
jQuery('.get-close-to').hover(function() {
var offset = jQuery(this).css('offset');
alert( 'Left: ' + offset.left + '\nTop: ' + offset.top );
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With