Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Follow mouse script gone bad...

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Follow mouse script gone bad...

    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?

  2. #2
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Please

  3. #3
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    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

  4. #4
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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>

  5. #5
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    then I have no idea wha't wrong. but i have seen scripts like this before

  6. #6
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well atleast you said something, thanks

  7. #7
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    try something like this

    Code:
    // 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;

  8. #8
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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 ...

  9. #9
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    this works slower

    Code:
    <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>

  10. #10
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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 ...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •