Web programming >> JavaScript Moving Image
Posted by ophline on 22:00:00 10-29-2002
I have a... here (This isn't all):

<style>
#xball { position: absolute; left: 28 ; top: 192; }
#xbarr { position: absolute; left: 60 ; top: 192; }
</style>

<script language = "JavaScript">
function MouseOver(location) {
if (location > xball.top) {
while ( document.all.xball.top < location )
{
xball.top++;
}
}
else
while ( document.all.xball.top > location)
{
xball.top--;
}
}
</script>

<body>

<div id = "xbarr" onmouseover = "MouseOver(192);">
<img src = "imags/lyout/ball_20_20.png">
</div>
</body>
Any help? I want the ball to move to the location of where the mouse is over.. like if it is over a button in the sidebar called "xbarr" it will move to 192. If it was say "blink" then it would be something like 322. Help Please!

[ This Message was edited by: ophline on 2002-10-29 22:00 ]
Posted by dxprog on 23:19:00 10-29-2002
Well from what I can see you are using IE. You have xball.top and xball.left, but they should be (I'm using left for my example) :

xball.style.pixelLeft or
xball.style.posLeft

Also to be on the safe side I would put: document.all["xball"] etc. instead of the actual name of the image. I hope this helps. [addsig]
Posted by ophline on 08:57:00 10-30-2002
Gracias senor. I will have to try it out. You will here he11 from me if it doesn't work.. hehe
Posted by dxprog on 09:26:00 10-30-2002
I have had more trouble with the left and top things than I care to say. It was only a few days ago that I figured it out. [addsig]
Posted by ophline on 12:32:00 10-31-2002
Hm... that didn't work..
Posted by dxprog on 12:45:00 10-31-2002
Another way, if I understand properly, is to use individual images and keep them hidden until the mouse moves over the button. Example:

<html>
<head>
<script language="JavaScript">
function moveFish(id)
{
document.all["f1"].style.visibility = "hidden";
document.all["f2"].style.visibility = "hidden";
document.all[id].style.visibility = "visible";
}
</script>
<style type="text/css">
.fish
{
visibility: hidden;
}
</style>
</head>
<body>
<table>
<tr><td><img src="http://www26.brinkster.com/mpxp/images/fish.gif" class="fish" id="f1"></td><td onMouseOver="moveFish('f1');">Link 1</td></tr>
<tr><td><img src="http://www26.brinkster.com/mpxp/images/fish.gif" class="fish" id="f2"></td><td onMouseOver="moveFish('f2');">Link 2</td></tr>
</table>
</body>
</html>

You won't get the moving effect, but it turns out about the same. I will look into that a little more later tonight. [addsig]
Posted by dxprog on 12:58:00 10-31-2002
I've figured out the problems. I will give you a detailed report and sample source tommorow morning. [addsig]
Posted by dxprog on 04:05:00 11-01-2002
Okay, here's the code.

<html>
<head>
<style type="text/css">
.ball
{
position: absolute;
left: 10;
top: 50;
}
</style>
<script language="JavaScript">
var object = "link1";
var speed = 20;

function moveBall()
{

var top = document.all[object].offsetTop;
var balltop = document.all["ball"].offsetTop - 15;

if (balltop > top) {
document.all["ball"].style.pixelTop -= 5;
}
else if (balltop < top) {
document.all["ball"].style.pixelTop += 5;
}

window.setTimeout("moveBall()", speed);
}

</script>
<title>New Document</title>
</head>
<body>
<table border=0 cellpadding=0 cellspacing=0>
<tr><td width=20> </td><td onMouseOver="object='link1';" id="link1">This is shows</td></tr>
<tr><td></td><td onMouseOver="object='link2';" id="link2">how to animate</td></tr>
<tr><td></td><td onMouseOver="object='link3';" id="link3">a little ball.</td></tr>
</table>
<img src="ball.bmp" class="ball" id="ball">
<script language="JavaScript">moveBall();</script>
</body>
</html>

And here's what was wrong with yours:

<style>
#xball { position: absolute; left: 28 ; top: 192; }
#xbarr { position: absolute; left: 60 ; top: 192; }
</style>

<script language = "JavaScript">
function MouseOver(location) {
if (location > xball.top) { <!-- need .style.offsetTop to retrieve Top value -->
while ( document.all.xball.top < location ) <!-- need .style.offsetTop to retrieve Top value -->
{
xball.top++; <!-- need to use .style.pixelTop to set location -->
}
}
else
while ( document.all.xball.top > location) <!-- need .style.offsetTop to retrieve Top value -->
{
xball.top--; <!-- need to use .style.pixelTop to set location -->

}
}
</script>

<body>

<div id = "xbarr" onmouseover = "MouseOver(192);">
<img src="imags/lyout/ball_20_20.png"> <!-- Never applied "xball" style so position is relative -->
</div>
</body>

I hope this helps you. Maybe I need to start writing some JScript tutorials.


_________________
When I got VB, i could have flown without thrusters and shot down TIE Interceptors just by spitting at them.

[ This Message was edited by: dxprog on 2002-11-01 04:07 ]

[ This Message was edited by: dxprog on 2002-12-18 10:24 ]
Posted by dxprog on 10:24:00 12-18-2002
I just now realized I forgot to disable HTML on this post [addsig]
Posted by KaGez on 23:02:00 12-18-2002
<uselessmsg>
JS is eeeeeeeeeeeeeeeeeeevil!
</uselessmsg>
[addsig]
Posted by dxprog on 00:01:00 12-19-2002
Then you're saying C is eeeeeeeeeeeevil, because that's basically what it is. [addsig]
Posted by MoX on 15:59:00 12-19-2002
nonono...do not compare C to JavaScript!

JavaScript is an evil, browser-dependent, performance-sucking, design-destroying web-programmer's nightmare.

Well, actually you can do _some_ very nice things with javascript and it gets better every day. If they would achieve (which is very unlikely) a true javascript standard, working on all browsers, then JavaScript would become the nice tool it was probably intended to be. [addsig]
Posted by KaGez on 17:22:00 12-19-2002
yeah, js itself is awsome, but in it's current state, it's more like a nightmare than anything else...
[addsig]
Posted by dxprog on 11:38:00 12-20-2002
I have to agree there. Writing cross-platform JS scripts is a nightmare and a half. That's why I don't bother [addsig]
Posted by ophline on 08:49:00 01-11-2003
Whoa, accidentally forgot about this post! Thank you verrry much dx, excellent work.
Posted by dxprog on 20:03:00 01-11-2003
Glad to be of service, but remember I am only human [addsig]