Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:13:45+00:00 2026-06-05T17:13:45+00:00

I have a function to follow the object after the mouse, and I want

  • 0

I have a function to follow the object after the mouse,
and I want to be able to stop and start following at will, without hiding the object.

It almost works as I wanted, and is following the mouse indeed, but I cannot make it move initial position without actually moving the mouse.

E.G. When I trigger the function, the object is still somewhere in another place, until I move the mouse, but what I’m trying to do is to move it the initial position first, before attaching the mousemove event.

Here is how I want to trigger the function:

showtrail();

function showtrail(shit){
//this is how I tried to set the initial position first, but this get me an error:..
//followmouse(); 
document.onmousemove=followmouse; //and this is how I attach the event.
}

This is a part of the actual function to move the object,
but, I can’t get the coordinates if I try to initilize/imitate the first movement.

    function followmouse(e){
var xcoord=offsetfrommouse[0]
var ycoord=offsetfrommouse[1]
if (typeof e != "undefined"){  //This- if triggered by mousemove, and it works
xcoord+=e.pageX
ycoord+=e.pageY
}
else {   //this was meant for the initial call, but... for some reason
xcoord+=document.body.scrollLeft+event.clientX // it triggers an error,
ycoord+=document.body.scrollTop+event.clientY // saying event.clientX undefined.
}
}

So the event.clientX never seems to work, and I cannot figure out how to get the actual mouse position otherwise..

Please guide..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:13:46+00:00Added an answer on June 5, 2026 at 5:13 pm

    Alright that’s how I done it.
    The best Idea was to always capture a move to set position in a global var.
    Now I have an option to display it fixed at any specific place (if I pass coords to showtrail)
    or to actually follow the mouse;
    I also added an event listener, so if the mouse gets outside the window while following- it will be hidden.
    So far it works exactly as I wanted:

    var trailimage=["scripts/loading_mouse.gif", 24, 24] //image path, plus width and height
    var offsetfrommouse=[2,10] //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
    var global_coord=[0,0]
    var follow=false;
    
    if (document.getElementById || document.all)
    document.write('<div id="trailimageid" style="z-index: 10;position:absolute;display:none;left:0px;top:0px;width:1px;height:1px"><img src="'+trailimage[0]+'" border="0" width="'+trailimage[1]+'px" height="'+trailimage[2]+'px"></div>')
    
    function gettrailobj(){
    if (document.getElementById)
    return document.getElementById("trailimageid").style
    else if (document.all)
    return document.all.trailimagid.style
    }
    
    function hidett(){  gettrailobj().display="none";   }
    function showtt(){  gettrailobj().display="block";  }
    
    function truebody(){    return (document.body||document.documentElement);   }
    
    function showtrail(shit){
        if (typeof shit == "undefined"){    //Follow Mouse
            follow=true;
            setmousepos(global_coord[0],global_coord[1]);
        }
        else {              //Set fixed in specific place
            follow=false;
            showtt()
            gettrailobj().left=shit.left+6+"px"
            gettrailobj().top=shit.top-5+"px"
        }
    }
    
    function hidetrail(){
        hidett()
        follow=false;
    }
    
    function setcoord(e){
    var xcoord=offsetfrommouse[0]
    var ycoord=offsetfrommouse[1]
    var xxcoord = e.pageX||(e.clientX+truebody().scrollLeft);
    var yycoord = e.pageY||(e.clientY+truebody().scrollTop);
    if (typeof xxcoord != "undefined"&&typeof yycoord != "undefined"){
        xcoord+=xxcoord;
        ycoord+=yycoord;
        global_coord=[xcoord,ycoord];
        if (follow) setmousepos(xcoord,ycoord);
    }}
    
    function setmousepos(xcoord,ycoord){
        var docwidth=truebody().scrollLeft+truebody().clientWidth   
        var docheight=Math.max(truebody().scrollHeight, truebody().clientHeight)
        if ((xcoord+trailimage[1]+3>docwidth || ycoord+trailimage[2]> docheight ||!follow)){
            hidett()
        }
        else{
            showtt();
            gettrailobj().left=xcoord+"px"
            gettrailobj().top=ycoord+"px"
        }
    }
    
    window.addEventListener("mouseout",
        function(e){
            mouseX = e.pageX;
            mouseY = e.pageY;
            if ((mouseY >= 0 && mouseY <= window.innerHeight)
            && (mouseX >= 0 && mouseX <= window.innerWidth)){
                return false;
            }else{
                if (follow) hidett()
            }
        },
        false);
    
    document.onmousemove=setcoord;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a trailing object following after the mouse. When the mouse gets out
I have function some_func_1 which will create an object of type some_type and will
I have function getCartItems in cart.js and I want to call that function in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have to register an object in a container upon its creation. Without smart
I have the following code in my codebehind Page_Load function that sets the default
My question (which will follow after this, sorry about the long intro, the question
Using Django 1.3 with PostgreSQL 9.0, I have a multi-step object creation function/view, where:
I have the follow (sample) code: class Foo { public function __construct() { $this->bar
i have a function defined as follows: void AddHeadCode(std::ofstream &ostream, size_t length){ ostream.write((char*)length, sizeof(length));

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.