开发者

Best Performance Javascript on IE6 for finding top and left using offsetTop and offsetLeft

I want to get the top and left values for a div on the screen.

I have been using the code to calculate the top and left values.

var total1 = 0;
var total2 = 0;
while(element){
total1+=element.offsetTop;
total2+=element.offsetLeft;
try{
element=element.offsetParent;
}catch(E){
break;
}
}

For the same DOM TREE this code is giving a performance reading of 30msec in IE8 and 80 to 200msec in IE6. I want to gain a considerable performance impro开发者_JAVA百科vement in IE6.

I am open to all ideas.

By the way I also tried

    var total1 = 0;
    var total2 = 0;
do{
  total1+=element.offsetTop;
    total2+=element.offsetLeft;
}while(element=element.offsetParent); 

after reading a blog entry or two. The performace is similar.

I found that the bulk of the time is always spent in getting property value rather than parsing the DOM TREE which less constantly from my logs.(I might be wrong)

Also the code I have put here is on the fly....might have made mistakes.

Am looking at performance gain. The floor is open to try anything only constraint is just javascript, jquery, prototype etc are not an option.

Thanks in advance!! :)


I seriously doubt you will be able to come up with anything faster than jQuery's offset calculations. IE6 is painfully slow and only so much can be done to improve it.

Consider reading jQuery's implementation and including/porting it stand-alone.


I recommend you ask this question on comp.lang.javascript. Also you might post there why you are finding this to be a performance problem. If this calculation is being repeated there may be a way to avoid the repeat calculation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜