Log in

View Full Version : Follow mouse script gone bad...



pcbrainbuster
04-17-2007, 03:45 PM
Hello guys :),

This time I have a problem with one of my test scripts... It is supposed to make the ball follow your mouse but makes it go down at a angle... -

<html>
<body onmousemove="keepup()">
<img src="ball.gif" style="position: absolute;" id="followingo">
<script>
var call = document.getElementById('followingo')
var ol = call.offsetLeft
var ot = call.offsetTop
function keepup() {
if (fl!==event.clientX) {
fl++
call.style.left=fl}
if (ft!==event.clientY) {
ft++
call.style.top=ft}
}
</script>
</body>
</html>

Whats wrong?

pcbrainbuster
04-17-2007, 06:05 PM
Please :)

riptide
04-17-2007, 06:14 PM
I think the problem is somewhere around here.

{
if (fl!==event.clientX) {
fl++
call.style.left=fl}
if (ft!==event.clientY) {
ft++
call.style.top=ft}


like what is fl

pcbrainbuster
04-17-2007, 06:17 PM
Oh crud I edited it by accidently earlier on, this is what it was supposed to be -

<html>
<body onmousemove="keepup()">
<img src="ball.gif" style="position: absolute;" id="followingo">
<script>
var call = document.getElementById('followingo')
var fl = call.offsetLeft
var ft = call.offsetTop
function keepup() {
if (fl!==event.clientX) {
fl++
call.style.left=fl}
if (ft!==event.clientY) {
ft++
call.style.top=ft}
}
</script>
</body>
</html>

riptide
04-17-2007, 06:19 PM
then I have no idea wha't wrong. but i have seen scripts like this before

pcbrainbuster
04-17-2007, 07:48 PM
Well atleast you said something, thanks :)

riptide
04-17-2007, 07:56 PM
try something like this


// Simple follow the mouse script

var divName = 'mydiv'; // div that is to follow the mouse
// (must be position:absolute)
var offX = 15; // X offset from mouse position
var offY = 15; // Y offset from mouse position

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function follow(evt) {if (document.getElementById) {var obj = document.getElementById(divName).style; obj.visibility = 'visible';
obj.left = (parseInt(mouseX(evt))+offX) + 'px';
obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}
document.onmousemove = follow;

pcbrainbuster
04-17-2007, 08:04 PM
I could have done that if I wanted, but that his only half of what I want (the ball is supposed to go to the mouse slowly), but thanks for the post anyway :)...

riptide
04-17-2007, 08:27 PM
this works slower


<BODY>

<SCRIPT LANGUAGE="JavaScript1.2">
<!-- Original: demigod@psxexchange.com -->
<!-- Web Site: http://www.psxexchange.com -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var brOK = false, mie = false;
if (document.layers || document.all) brOK = true;
if (document.all) mie = true;
var ex = 0, ey = 0;
var ae, le, re, x0, y0, tid, realx, realy;
function navMove(e) {
ex = e.pageX;
ey = e.pageY;
moveeye()
return routeEvent(e);
}
function mieMove() {
ex = document.body.scrollLeft + event.x;
ey = document.body.scrollTop + event.y;
moveeye();
}
function moveeye() {
dy = ey - y0 - 20;
dx1 = ex - x0 - 20;
dx2 = ex - x0 - 60;
r = Math.sqrt(dx1 * dx1 + dy * dy);
if (r < 20) r = 20;
dx1 = dx1 * 10 / r + x0 + 10;
dy1 = dy * 10 / r + y0 + 10;
r = Math.sqrt(dx2 * dx2 + dy * dy);
if (r < 20) r = 20;
dx2 = dx2 * 10 / r + x0 + 50;
ae.left = x0;
ae.top = y0;
le.left = dx1;
le.top = dy1;
re.left = dx2;
re.top = dy1;
}
function setHandlers() {
if (!mie) {
y0 = document.eyeballs.top;
x0 = document.eyeballs.left;
ae = document.eyeballs;
le = document.lefteye;
re = document.righteye;
window.captureEvents(Event.MOUSEMOVE);
window.onMouseMove = navMove;
}
else {
y0 = document.all.eyeballs.style.pixelTop;
x0 = document.all.eyeballs.style.pixelLeft;
ae = document.all.eyeballs.style;
le = document.all.lefteye.style;
re = document.all.righteye.style;
window.document.onmousemove = mieMove;
}
realx = x0 + 0.1;
realy = y0 + 0.1;
moveall();
}
function moveall() {
rx = realx + 40;
ry = realy + 40;
rx += (ex - rx) * 0.1;
ry += (ey - ry) * 0.1;
realx = rx - 40;
realy = ry - 40;
x0 = Math.round(realx);
y0 = Math.round(realy);
moveeye();
tid = setTimeout('moveall()', 100);
}
function placeeyes(x, y) {
if (brOK) {
ex = x + 40;
ey = y + 40;
s = '<DIV ID ="dummy" STYLE="position:absolute; ' +
'top:'+y+'; left:'+x+'; width:10; height:10;"> </DIV>';
s += '<DIV ID="eyeballs" STYLE="position:absolute; ' +
'top:'+y+'; left:'+x+'; width:80; height:40;"><IMG SRC=' +
'"whites.gif" border=0></DIV>';
s += '<DIV ID="lefteye" STYLE="position:absolute; ' +
'top:'+(y+10)+'; left:'+(x+10)+'; width:20; height:20;">' +
'<IMG SRC="pupil.gif" border=0></DIV>';
s += '<DIV ID="righteye" STYLE="position:absolute; ' +
'top:'+(y+10)+'; left:'+(x+50)+'; width:20; height:20;">' +
'<IMG SRC="pupil.gif" border=0></DIV>';
document.writeln(s);
}
}
function clearEyes() {
if (tid) clearTimeout(tid);
}
placeeyes(200, 100);
window.onload = setHandlers;
window.onunload = clearEyes;
// End -->
</script>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

pcbrainbuster
04-17-2007, 08:32 PM
Yup I remember that script, but its not what I want :)... Everything is correct except that I did not need 3 images and ALL that code...
It should work with the code I made (I wonder why it doesn't...) !!!

Thanks Again Though :)...

riptide
04-17-2007, 08:41 PM
yeah these are objects and not functions. maybe you should take the other images out.... I hate having to use objects and make methods. I get lost in the code when there is an easier way

pcbrainbuster
04-17-2007, 09:09 PM
I'm not really doing this as a project or anything, I ask these questions for future reference...

pcbrainbuster
04-18-2007, 06:47 AM
Thaks anyway but I have finished the code -

<html>
<body onmousemove="keepup()">
<img src="ball.gif" style="position: absolute;" id="followingo">
<hr id="linex" style="position: absolute; left: -25px; width: 1px;" color="black">
<hr id="liney" style="position: absolute; top: -25px; width: 2px;" color="black">
<script>
var call = document.getElementById('followingo')
var callx = document.getElementById('linex')
var cally = document.getElementById('liney')
var fl = call.offsetLeft
var ft = call.offsetTop
function keepup() {
callx.style.width=call.offsetLeft+30
callx.style.top=call.offsetTop+20
cally.style.left=call.offsetLeft+19
cally.style.height=call.offsetTop+30

if (fl>=event.clientX) {
fl-=2
call.style.left=fl
}

else if (fl<=event.clientX) {
fl+=2
call.style.left=fl
}

if (ft>=event.clientY) {
ft-=2
call.style.top=ft
}

else if (ft<=event.clientY) {
ft+=2
call.style.top=ft
}

}
</script>
</body>
</html>

Except there is one problem :), as you would pobably see the liney div does not keep its height to ball size and I cant figure out why...

pcbrainbuster
04-19-2007, 07:14 PM
Well Why is This?