CodingStupid JS bug

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Peter (BOUGHTONP)  
 To:  ALL
31343.1 
I think I might be going insane here.

readLeft is working fine.
readTop isn't working fine - it is apparently giving the wrong numbers for some fields, but I can't figure it out why - both functions look the same?

JavaScript code:
function readTop(element,amount)
{
	if (element.parentNode)
	{
		amount = amount + readTop(element.parentNode, amount);
	}
	if (element.offsetTop)
	{
		return amount + element.offsetTop;
	}
	else
	{
		return amount;
	}
}
 
function readLeft(element,amount)
{
	if (element.parentNode)
	{
		amount = amount + readLeft(element.parentNode, amount);
	}
	if (element.offsetLeft)
	{
		return amount + element.offsetLeft;
	}
	else
	{
		return amount;
	}
}


Oh shit. I've just tested it in Firefox, and its not working there at all.

Ok, Firefox just needed the 'px' manually added on when assigning the top/left of the moving element.

It's still buggering about producing the wrong tops though.

Why why why why why? :'(
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ALL
31343.2 In reply to 31343.1 
I've got it working in IE by subtracting the final offset:

JavaScript code:
function readTop(element,amount)
	{
		if (element.parentNode)
		{
			amount = amount + readTop(element.parentNode, amount);
		}
		if (element.offsetTop)
		{
			amount = amount + element.offsetTop;
		}
 
		return amount;
	}
 
	function readLeft(element,amount)
	{
		if (element.parentNode)
		{
			amount = amount + readLeft(element.parentNode, amount);
		}
		if (element.offsetLeft)
		{
			return amount + element.offsetLeft;
		}
		else
		{
			return amount;
		}
	}
 
 
 
 
elem('error_message_box').style.top = (readTop(elem(id),0) - elem(id).offsetTop)+ 'px';
elem('error_message_box').style.left = (readLeft(elem(id),0) + elem(id).offsetWidth) + 'px';



Works-ish in Firefox, but box is offset roughly 1em down and right, for no apparent reason.

Hmph. Will have to put up with that now because I'm on a deadline, but if anyone has any better solutions they would be greatly appreciated!
0/0
 Reply   Quote More 

Reply to All    
 

1–2

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats