From: Peter Michaux on 28 Dec 2009 23:18 On Dec 28, 6:36 pm, David Mark <dmark.cins...(a)gmail.com> wrote: > On Dec 28, 1:01 pm, Peter Michaux <petermich...(a)gmail.com> wrote: > > > On Dec 28, 11:32 am, moha297 <moha...(a)gmail.com> wrote: > > > > I want to get the top and left values for a div on the screen. > > > physical screen or upper-left corner of the page (which may be out of > > view if the page is scrolled.)? > > > As Richard Cornford has mentioned here many times, this problem is not > > solved in general. If your div has parents that scroll, have table > > elements, is a button, etc, then the calculation of the div's upper- > > left corner relative to the upper-left corner of the page is complex. > > It's been done. I've never seen such code and whoever claims "it does everything" is probably not correct. > It's just not an advisable cross-browser design to > rely on such complex code The implication of that statement is that the code likely doesn't do everything and so code that does everything doesn't really exist. > when simpler options are available. Agreed. Peter
From: Peter Michaux on 28 Dec 2009 23:26 On Dec 28, 3:37 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > moha297 wrote: > > 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){ > > Should be `e' as it does not refer to a constructor. If we are now off topic, some folks like to write globals in all capitals. Since that looks like it might be an (implied) global then captial 'E' may be appropriate. > > break; > > } > > } > > > For the same DOM TREE this code is giving a performance reading of > > 30msec in IE8 and 80 to 200msec in IE6. > > That is unsurprising since that try...catch statement does not do anything > useful here as the assignment is not going to fail: The assignment will not fail but getting a host object's property may. > In most cases you do not need to determine the absolute position of an > element in the first place. That is absolutely ;-) correct. I think I've only needed absolute position once in the past couple years. Peter
From: David Mark on 28 Dec 2009 23:26 On Dec 28, 11:18 pm, Peter Michaux <petermich...(a)gmail.com> wrote: > On Dec 28, 6:36 pm, David Mark <dmark.cins...(a)gmail.com> wrote: > > > On Dec 28, 1:01 pm, Peter Michaux <petermich...(a)gmail.com> wrote: > > > > On Dec 28, 11:32 am, moha297 <moha...(a)gmail.com> wrote: > > > > > I want to get the top and left values for a div on the screen. > > > > physical screen or upper-left corner of the page (which may be out of > > > view if the page is scrolled.)? > > > > As Richard Cornford has mentioned here many times, this problem is not > > > solved in general. If your div has parents that scroll, have table > > > elements, is a button, etc, then the calculation of the div's upper- > > > left corner relative to the upper-left corner of the page is complex. > > > It's been done. > > I've never seen such code and whoever claims "it does everything" is > probably not correct. I'm not saying the last one I wrote does it all, but it covers most (if not all) of the issues mentioned. > > > It's just not an advisable cross-browser design to > > rely on such complex code > > The implication of that statement is that the code likely doesn't do > everything and so code that does everything doesn't really exist. Not exactly. I mean that even if it does work 100%, the code is quite complex with multiple forks (shouldn't a first choice for a design).
From: Peter Michaux on 28 Dec 2009 23:28 On Dec 28, 10:26 pm, Peter Michaux <petermich...(a)gmail.com> wrote: > On Dec 28, 3:37 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> > wrote: > > > moha297 wrote: > > > 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){ > > > Should be `e' as it does not refer to a constructor. > > If we are now off topic, some folks like to write globals in all > capitals. Since that looks like it might be an (implied) global then > captial 'E' may be appropriate. Nevermind. I really don't use try-catch enough. Peter
From: Peter Michaux on 28 Dec 2009 23:30 On Dec 28, 10:26 pm, David Mark <dmark.cins...(a)gmail.com> wrote: > On Dec 28, 11:18 pm, Peter Michaux <petermich...(a)gmail.com> wrote: > > > > > On Dec 28, 6:36 pm, David Mark <dmark.cins...(a)gmail.com> wrote: > > > > On Dec 28, 1:01 pm, Peter Michaux <petermich...(a)gmail.com> wrote: > > > > > On Dec 28, 11:32 am, moha297 <moha...(a)gmail.com> wrote: > > > > > > I want to get the top and left values for a div on the screen. > > > > > physical screen or upper-left corner of the page (which may be out of > > > > view if the page is scrolled.)? > > > > > As Richard Cornford has mentioned here many times, this problem is not > > > > solved in general. If your div has parents that scroll, have table > > > > elements, is a button, etc, then the calculation of the div's upper- > > > > left corner relative to the upper-left corner of the page is complex. > > > > It's been done. > > > I've never seen such code and whoever claims "it does everything" is > > probably not correct. > > I'm not saying the last one I wrote does it all, but it covers most > (if not all) of the issues mentioned. The case that Richard Cornford refers to is the case where the code truly does it all. Even the full "etc" I mentioned. Peter
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: DOM ready, window.onload, etc. Next: Chrome, canvas, PNG, Save Image As |