8

I have the following line of code:

var newLeftPos = $('span#s' + i).position().left - parseInt($('span#s' + i).css('width'), 10);

It works great in ie6 and upwards but I also need to get it to work for ie5.5. (Let's not argue about that now - I know - but I have no option)

I'm certain it falls on the .position() while I'm testing on ie5.5 with jquery 1.2

How could I do the same in plain javascript? Could "offsetParent" help me here? Apparently ie5.5 supports that.

Thanks in advance.

7
  • 2
    What have you tried so far? Have you examined the source of jquery.js to see how the position() function works? Commented Sep 12, 2011 at 14:29
  • 2
    I'm really curious who in their right mind is still surfing the internet with IE 5.5, because that means they're probably using Windows 2000, or worse Windows Me. Commented Sep 12, 2011 at 14:53
  • 1
    QuirksMode has an article on finding element positions quirksmode.org/js/findpos.html Commented Sep 12, 2011 at 14:58
  • Did you have a look at getBoundingClientRect() method? Commented Sep 12, 2011 at 14:58
  • jQuery will actually allow you to define cssHooks to allow it to work properly. So if you do firgure it out, you can use it easily. Commented Sep 12, 2011 at 15:00

1 Answer 1

4

You are looking for offsetParent, offsetLeft and offsetRight.

As you can see in the link, looks like they are supported even by old granny IE5.5.

Check this text page to see if they are really supported by your browser first.

Then your function should be something like

var span = document.getElementById('s' + i);

var newLeftPos = span.offsetWidth - parseInt(span.style.width);
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thanks a whole lot! That certainly does exactly what I asked for. So simple. It was "offsetLeft" which I used, and that gave the right value. For some reason span.style.width returns null, but I'm sure that's much easier problem to solve. Thanks.
@Tuomas Ahh yes I forgot offsetLeft! :) Anyway while testing I also noticed the same problem. It return the right value only if the style is inline in the html, but not if it's in a separated css file... don't know why at the moment.
it's not that simple, if for example transform: translate(-100px, 0) is used additionaly, you're lost...as I am right now. You can use this: stackoverflow.com/questions/52733352/… but things will get nasty quickly.

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.