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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:48:15+00:00 2026-06-01T14:48:15+00:00

I have a bunch of span elements in random positions enclosed inside a parent

  • 0

I have a bunch of span elements in random positions enclosed inside a parent div called ‘.background’. These are generated with Javascript. Like this:

<span class="circle" style="width: 54px; height: 54px; background: #5061cf; top: 206px; left: 306px"></span>

I want them to move away (or repel) as the mouse draws near, but I have no idea how to do this! How would I go about accomplishing this in jQuery?

I imagine you’d have to search for spans that were nearby, and then change their position if they were inside a certain radius surrounding the mouse, but I really don’t know where to start. Any help is appreciated!

  • 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-01T14:48:16+00:00Added an answer on June 1, 2026 at 2:48 pm

    A simple approach would be to wrap each span in another, larger span. Make it larger on each side by the minimal distance you want the mouse to be able to approach the inner spans. Bind a function (evade) that moves each wrapper to mouseover on the wrappers. This approach gives you a square border, so if the graphical elements in the inner spans aren’t square, the distance from the mouse to the graphical element’s border won’t be constant, but is easy to implement.

    Alternatively, use the bumper for a rough proximity test. Instead of binding the evade function to mouseover, bind a function (beginEvade) that binds evade on mousemove. Also, bind a function to mouseout that unbinds evade. Your evade can then perform a more precise proximity test.

    First, find a good geometry library that provides a vector type. In absence of one, here’s a sample implementation:

    Math.Vector = function (x,y) {
        this.x = x;
        this.y = y;
    }
    Math.Vector.prototype = {
        clone: function () {
            return new Math.Vector(this.x, this.y);
        },
        negate: function () {
            this.x = -this.x;
            this.y = -this.y;
            return this;
        },
        neg: function () {
            return this.clone().negate();
        },
        addeq: function (v) {
            this.x += v.x;
            this.y += v.y;
            return this;
        },
        subeq: function (v) {
            return this.addeq(v.neg());
        },
        add: function (v) {
            return this.clone().addeq(v);
        },
        sub: function (v) {
            return this.clone().subeq(v);
        },
        multeq: function (c) {
            this.x *= c;
            this.y *= c;
            return this;
        },
        diveq: function (c) {
            this.x /= c;
            this.y /= c;
            return this;
        },
        mult: function (c) {
            return this.clone().multeq(c);
        },
        div: function (c) {
            return this.clone().diveq(c);
        },
    
        dot: function (v) {
            return this.x * v.x + this.y * v.y;
        },
        length: function () {
            return Math.sqrt(this.dot(this));
        },
        normal: function () {
            return this.clone().diveq(this.length());
        }
    };
    

    Next, a sample circular evasion function (which is the simplest to implement). Outline:

    1. calculate the bumper’s center (the bumper’s corner plus the outer dimensions divided in half)
    2. calculate the mouse offset vector (from the mouse cursor to the element’s center)
    3. proximity test: if the distance is >= the minimum allowed distance, then return early.
    4. calculate delta: The distance to the mouse cursor is too small, so we need the vector from where the bumper is to where it should be (the delta). Lengthening the offset vector so it’s the minimum allowed distance gives where the bumper’s center should be, relative to the mouse’s position. Subtracting the offset vector from that gives the delta from the proximity edge to the mouse, which also happens to be the delta.
    5. calculate new position:
      1. add the delta to the current position.
      2. bounds checking: keep all borders of the circle within the document.
    6. move the bumper

    In code:

    function evade(evt) {
        var $this = $(this),
            corner = $this.offset(),
            center = {x: corner.left + $this.outerWidth() / 2, y: corner.top + $this.outerHeight() / 2},
            dist = new Math.Vector(center.x - evt.pageX, center.y - evt.pageY),
            closest = $this.outerWidth() / 2;
    
        // proximity test
        if (dist.length() >= closest) {
            return;
        }
    
        // calculate new position
        var delta = dist.normal().multeq(closest).sub(dist),
            newCorner = {left: corner.left + delta.x, top: corner.top + delta.y};
    
        // bounds check
        var padding = parseInt($this.css('padding-left'));
        if (newCorner.left < -padding) {
            newCorner.left = -padding;
        } else if (newCorner.left + $this.outerWidth() - padding > $(document).width()) {
            newCorner.left = $(document).width() - $this.outerWidth() + padding;
        }
        if (newCorner.top < -padding) {
            newCorner.top = -padding;
        } else if (newCorner.top + $this.outerHeight() - padding > $(document).height()) {
            newCorner.top = $(document).height() - $this.outerHeight() + padding;
        }
    
        // move bumper
        $this.offset(newCorner);
    }
    

    After that, all that’s left are functions to bind/unbind evade, and the calls to set everything up.

    function beginEvade() {
        $(this).bind('mousemove', evade);
    }
    
    function endEvade() {
       $(this).unbind('mousemove', evade);
    }
    
    $(function () {
        // you can also wrap the elements when creating them.
        $('.circle').wrap('<span class="bumper" />')
    
        $('.bumper').bind('mouseover', beginEvade);
        $('.bumper').bind('mouseout', endEvade);
    });
    

    You can preview this in jsFiddle

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

Sidebar

Related Questions

week DKK id=radio2 value=75 /> I have bunch of these div entry generated by
I have a bunch of elements like this: <div></div> <span></span> <table></table> <div></div> <span></span> <div></div>
I have a bunch of elements that look like this: <span class='tags' id='html'>html</span> <span
I have a bunch of child elements that are uniquely identified within a parent
I have a bunch of divs which I nest arbitrarily: <div> <div> <div>Apple</div> <div>
I have a div with a unique ID. Under that div are a bunch
I have a page that dynamically generates any number of divs.  Inside the div
I have a bunch on elements with the same name that i am trying
I have a bunch of javascript files I need to parse via PHP (I
I have a bunch of elements with a class name red , but I

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.