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 8920451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:16:23+00:00 2026-06-15T06:16:23+00:00

I found this div follows mouse animation: JSFiddle that belongs to the answer posted

  • 0

I found this “div follows mouse” animation:
JSFiddle that belongs to the answer posted in: How to animate following the mouse in jquery.

var mouseX = 0, mouseY = 0;
$(document).mousemove(function(e){
   mouseX = e.pageX;
   mouseY = e.pageY; 
});

// cache the selector
var follower = $("#follower");
var xp = 0, yp = 0;
var loop = setInterval(function(){
// change 12 to alter damping higher is slower
xp += (mouseX - xp) / 12;
yp += (mouseY - yp) / 12;
follower.css({left:xp, top:yp});

}, 30);

​

I got quite intrigued by the inner workings and I tried to understand what the code is doing. I am currently learning javascript and I tried to replicate the “point to point” animation. I found this: Point to point animation algorithm, but it feels that the code of the original poster is pretty long and repetitive.

My current basic level of javascript is being an obstacle to fully understand the jQuery version (i still didn’t enter into the jQuery realm). Yet, I think that do this kind of animation in javascript might be possible, but the only way I can think about is with a code similar to the “point to point” link.

Can something like this be done with a code as short as the jQuery version? Or to do just javascript requires much longer routine?

Thanks for any suggestion or comment that you might have!

  • 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-15T06:16:24+00:00Added an answer on June 15, 2026 at 6:16 am

    Can something like this be done with a code as short as the jQuery version?

    In this case, nearly, yes, because what’s being done is fairly simple and only a small amount of jQuery’s functionality is used. Specifically:

    • It’s hooking the mousemove event on document, which can easily be done with addEventListener (or attachEvent on older versions of IE).

    • It’s locating an element by id ($("#follower")), which can easily be done with getElementById.

    • It’s setting the left and top of an element via jQuery’s css function. This can easily be done by setting the left and top properties on the element’s style property.

    Putting that all together: Fiddle

    var mouseX = 0, mouseY = 0;
    if (document.addEventListener) {
        document.addEventListener('mousemove', mouseMoveHandler, false);
    }
    else if (document.attachEvent) {
        document.attachEvent('onmousemove', mouseMoveHandler);
    }
    else {
        document.onmousemove = function(event) {
            mouseMoveHandler(event || window.event);
        };
    }
    
    function mouseMoveHandler(e) {
       mouseX = e.pageX;
       mouseY = e.pageY; 
    }
    
    // cache the element
    var follower = document.getElementById("follower");
    var xp = 0, yp = 0;
    var loop = setInterval(function(){
    // change 12 to alter damping higher is slower
    xp += (mouseX - xp) / 12;
    yp += (mouseY - yp) / 12;
    follower.style.left = xp + "px";
    follower.style.top  = yp + "px";
    
    }, 30);
    

    As you can see, the biggest issue is hooking the event in a way that plays nicely with others. Although you could just use the document.onmousemove version, that would clobber any other mousemove handlers on document, which is why we first look for addEventListener or attachEvent.

    Some handy references, in roughly chronological order (latter ones update/supercede/add to earlier ones):

    • DOM2 Core
    • DOM2 HTML
    • DOM2 Events
    • DOM3 Core
    • DOM3 Events
    • Other DOM specs
    • HTML5 Web Application APIs

    None of which should be taken to say that you can’t use a good library like jQuery, there are several good reasons to (the events thing above is just one example). But it’s always best to understand the basics as well, even if you use a library to avoid having to deal with them all that much.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found this question on StackOverflow Adding div below images in colorbox But it
in some CSS code I found out this type of selector div#someid Is this
Using this code var html='<div class=somediv></div>'; var target=document.getElementById('contentArea'); target.appendChild(html); I'm getting Uncaught Error: NOT_FOUND_ERR:
found this little code snippet that seems to do what i want, but im
This question follows on from a previous question which can be found here: Need
I found this article and I decided that I liked the way they styled
I've been following every tip that I've found in the web but... I haven't
Basically, I referred to some sources, and found this: Yah, that's easy, just create
Found this while reading the Neo4j manual, specifically here , I found the sentence:
Found This: tyty stack, Social Icons not working with Infinite Scrolling on Wordpress I'm

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.