Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

jscript

  Asked By: Alex    Date: Dec 09    Category: Asp.net    Views: 425
  

I have plonked an image in a webpage.

I don't want to use a style tag with positioning.

I want to be able to grab that images position with respect to the page.

If this means perhaps using relative postioning and maybe keeping track of it's
containers position relative to the page then hiswould be ok, I think.

Well I've tried that last question and it's no good beause it returns it's own
relative postion not a bubble up its containers to it's position relative to the
page.

The reason ... my page stretches and so forth, so the image actually moves ... I
need to know it's position relative to the document edges.

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Emily Brown     Answered On: Dec 09

instead of using a style  then just make a layer - has more
functionality (document) and can get edges and localise events too.

 
Answer #2    Answered By: Jarrod Williams     Answered On: Dec 09

or wrap a div around it ...............

 
Answer #3    Answered By: Evelyn Hughes     Answered On: Dec 09

go thru all parents... like this (works on IE as well as on NN):


function _elementTop(el)
{
var et = 0;
while (el)
{
et += el.offsetTop;
el = el.offsetParent;
}
return et;
}

function _elementLeft(el)
{
var et = 0;
while (el)
{
et += el.offsetLeft;
el = el.offsetParent;
}
return et;
}

both functions return pixel number top and left respectively.

 
Answer #4    Answered By: Douglas Sullivan     Answered On: Dec 09

offsetTop ... thats what I was looking for.

 
Answer #5    Answered By: Cambria Lopez     Answered On: Dec 09

Yes. and to tell you it's even better than sytle.top, while offset has only a
number. style  is a string with measurement abbreviation like px... And if the
style is not set with the STYLE attribute on the control it's empty. Offset
always has a value...

No problem. Glad I can help. Had same problems once...

 
Answer #6    Answered By: Topaz Ramirez     Answered On: Dec 09

I know - I came across that one - this was my fix.
With no intellisense jscript can be a pain, I have examples in mybook that
simply do not hold true for NN or ie - dunno who wrote it they must have
guessed at half of it - or MS have altered IE over time, and altered
existing models not just added stuff to them.

Anyway


function WeirdFunction(){
//Check this out peeps
//Luckily for me it just so happened that I was subtracting in OpenWin()
//Look ::
var item = document.images.im;
var locW = 0;
locW = item.style.left.substring(0,item.style.left.length-2); // all I could
think to do to rid me of the px on the end.
var totaddition = 0;
var totsubtraction = 0;
var totdoubleneg = 0;
var totaddition = locW + 7 ;
var totsubtraction = locW - 7;
var totdoubleneg = locW - (-7);
alert("totaddition is:" + totaddition + " totsubtraction is:" +
totsubtraction + " totdoubleneg is:" + totdoubleneg);
}

If locW was 40px then the above alert displays 407 for the addition, then
33, then 47.
The proper way is :

function getTopPos(el) {
if (ie5) {
if (el.currentStyle.top == "auto")
return 0;
else
return parseInt(el.currentStyle.top);
}
else {
return el.style.pixelTop;
}
}

 
Answer #7    Answered By: Angel Watkins     Answered On: Dec 09

Some "more" knowldege for you as if you didn't have enough.

I have mousecaptured and all that I kept getting a 1 as I rolled into my
image and I couldn't figure out where it was coming from.

It's not like it matters its only 1 px.
And then I found I'd forgotton about about the scroll position, and there's
that little bit at the top -->> oh no there isn't ... back to square one.
Blamed it on mouse speed.
Blamed it blurring through windows repaint.
Blamed it fuzzy grahics - or was that just my eyesight failing in the mist.
But kept thinking ....
Here's what it is ... by chance I'd managed to set a class for the body tag.
I had a solid border of 1 px around it that I didn't even see. Those
functions you gave me can't get to it. I haven't tried appending the
getTopPos() functs (i.e. the one I sent you) to them yet but it should sort
them (if it's worth the effort even.)

 
Answer #8    Answered By: Burkett Bernard     Answered On: Dec 09

Yes you may get into some obstacles using DOM. I'am lucky while I almost never
user borders and if I can I always try to use other techniques instead of JS
positioning. First I had to use those functions, but later I was able to avoid
them easily...

You'll also find that width is reported differently in IE and NN. One counts the
borders (or is it padding - forgot about that) into it, the other doesn't...

 
Didn't find what you were looking for? Find more on jscript Or get search suggestion and latest updates.

Related Topics:



Tagged:  

 

Related Post