Creating generic move element function

R

Rick

I'm looking at some projects in the near-future involving lots of
javascripts where items will be moved or animated in various ways. I
thought in preparation, I should write up a generic sort of "move page
element" function that takes the id of an object, a direction (i.e.,
up-and-down or side-to-side) and the number of pixels to move. That
function would use that information to move something on-screen. This
would avoid me having to write lots of redundant code to move things
around.

What I've written doesn't work at all. Curiously, I'm also getting no
errors to give me any clue. I'm a little rusty in terms of writing
Javascript that moves things around, but I have done it before. If
anyone can point me in the right direction to help figure this out, I'd
appreciate it. Here's the function with a couple of example calls
before it so you can understand what I'm trying to do (the alerts in
the function are part of me trying to figure out what's going on, btw.)

movePageElement('v',2,'greensquare');
// this would tell the function to move object with the id
"greensquare" 2 pixels vertically (i.e, down.)

movePageElement('h',-5,'greensquare');
// this would tell the function to move object with the id
"greensquare" -5 pixels horizontally (i.e, to the left.)


function movePageElement(theDirection,theDistance,elementName)
{
if (document.all) // IE
{
//alert ("ie");
topPos = document.all[elementName].style.pixelTop;
leftPos = document.all[elementName].style.pixelLeft;
alert(topPos + "..." +leftPos);
if (theDirection == "v") // we're moving up and down, vertical
{
document.all[elementName].style.pixelTop = topPos + theDistance;
}
if (theDirection == "h") // we're moving side-to-side, horizontal
{
document.all[elementName].style.pixelLeft = leftPos + theDistance;
}
alert(document.all[elementName].style.pixelTop + "..."
+document.all[elementName].style.pixelLeft);
}
else
{
//alert ("ns");
topPos = document.getElementById(elementName).top;
leftPos = document.getElementById(elementName).left;

if (theDirection == "v") // we're moving up and down, vertical
{
document.getElementById(elementName).style.top = topPos +
theDistance;
}
if (theDirection == "h") // we're moving side-to-side, horizontal
{
document.getElementById(elementName).style.left = leftPos +
theDistance;
}
}
}

--Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top