PDA

View Full Version : onMouseOver JavaScript Function Question



Exorcist464
Sep 13th, 2007, 04:24 AM
Ok... so im trying to make a simple javascript function that takes an image as a parameter and then moves it around a bit. I copied some code from an online source and have watched it work on one of my pages, however I am trying to call this function on a MouseOver but it is not working. Any help would be extremely nice! Thanks!

======= CODE ===============

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Barbara Shannon Consulting</title>

<script language="JavaScript">

Hmove=-100;
function moveObjRight(obj)
{
obj.style.left=Hmove;
Hmove+=2;
if(Hmove<100)
window.setTimeout("moveObjRight(" +obj.id+ ");", 0);
}

</script>

</head>

<link href="style/style.css" rel="stylesheet" type="text/css"/>

<body>
<div id="wrapper">
<div id="container">
<div id="topBar">

<a href="#" onMouseOver="moveObjRight('JS')">
<img src="images/about.png" name="JS" hspace=0 border=0 align=baseline id=JS style="z-index: 0; left: 400px; position: absolute; top: 50px">
</a>

</div>
</div>
</div>

</body>
</html>

Fang
Sep 13th, 2007, 09:06 AM
The function argument is an ID not an object reference.
Incorrect quoting in setTimeout.
It will move left not right.

Hmove=-100;
function moveObjRight(objID)
{
var obj=document.getElementById(objID);
obj.style.left=Hmove;
Hmove+=2;
if(Hmove<100)
window.setTimeout("moveObjRight('" +obj.id+ "');", 0);
}

Exorcist464
Sep 13th, 2007, 09:36 AM
Sooo... do i delete the id? or what. I know it goes right cuz this method works on another file i have, and it does go right... left. is the amount of pixels it is shifted away from the LEFT side, pushing it to the right. What exactly should i change?

Fang
Sep 13th, 2007, 10:16 AM
Just use the corrected function I gave.

Exorcist464
Sep 14th, 2007, 02:40 AM
Thanx... it kinda worked a lil better... im getting used to JavaScript, ive only known it now for a week. Anyways, what i want to do is make the image jump up a couple pixels and then jump back down, as if it was bouncing. For this i am going to need to get the images top: xxx px as send it to my function as a parameter, is this possible? thanx again! :p